zoukankan      html  css  js  c++  java
  • Leetcode-904 Fruit Into Baskets(水果成篮)

     1 class Solution
     2 {
     3     public:
     4         int totalFruit(vector<int>& tree)
     5         {
     6             vector<pair<int,int>> dealList;
     7             int curType = tree[0];
     8             int curSum = 1;
     9             for(int i = 1; i < tree.size(); i ++)
    10             {
    11                 if(tree[i]==curType)
    12                     curSum ++;
    13                 else
    14                 {
    15                     dealList.push_back(make_pair(curType,curSum));
    16                     curSum = 1;
    17                     curType = tree[i];
    18                 }
    19             }
    20             dealList.push_back(make_pair(curType,curSum));
    21 
    22         //    for(auto d:dealList)
    23         //        cout << d.first << " " << d.second << endl;
    24         //    cout << endl;
    25 
    26             int result = dealList[0].second;
    27             int max = 1;
    28             int typeA = dealList[0].first,typeB = -1;
    29             int justI;
    30             for(int i = 1; i < dealList.size(); i ++)
    31             {
    32                 if(dealList[i].first != typeA && typeB == -1)
    33                 {
    34                     typeB = dealList[i].first;
    35                     result += dealList[i].second;
    36                     justI = i;
    37                     //cout << dealList[i].first << endl;
    38                 }
    39                 else if(dealList[i].first == typeA || dealList[i].first==typeB)
    40                 {
    41                     result += dealList[i].second;
    42                     //    cout << dealList[i].first << endl;
    43                 }
    44                 else if(dealList[i].first != typeA && dealList[i].first != typeB)
    45                 {
    46                 //    cout << dealList[i].first << endl;
    47                     result = dealList[justI].second;
    48                     i = justI;
    49                     typeB = -1;
    50                     typeA = dealList[justI].first;
    51                 }
    52                 //    cout << dealList[i].first << endl;
    53                 if(result > max)
    54                     max = result;
    55             }
    56             if(result > max)
    57                 max = result;
    58             return max;
    59         }
    60 };
  • 相关阅读:
    Entity Framework框架 (一)
    webAPI的常用操作
    图片添加水印和生成验证码
    ASP.NET中Page_Load()与Page_Init()的区别
    session常用操作
    非递归解决组合问题
    TemplateDoesNotExist 异常
    [android]不解锁刷机
    论记忆化搜索
    flex builder 4
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9655579.html
Copyright © 2011-2022 走看看