forked from gongluck/CVIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
904.水果成篮.cpp
41 lines (38 loc) · 825 Bytes
/
904.水果成篮.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem904.h"
using namespace std;
// @before-stub-for-debug-end
/*
* @lc app=leetcode.cn id=904 lang=cpp
*
* [904] 水果成篮
*/
// @lc code=start
class Solution
{
public:
int totalFruit(vector<int> &fruits)
{
int max = INT_MIN;
int begin = 0;
int end = 0;
std::unordered_map<int, int> s;
for (; end < fruits.size(); ++end)
{
++s[fruits[end]];
while (s.size() > 2)
{
if (--s[fruits[begin]] == 0)
{
s.erase(fruits[begin]);
}
++begin;
}
max = std::max(max, end - begin + 1);
}
return max;
}
};
// @lc code=end