https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/
这个代码 技巧性太强了 参考了http://www.cnblogs.com/grandyang/p/7525821.html
基本思路就是,up维护数可能存在次数的上限,
class Solution {
public:
bool isPossible(vector<int>& nums) {
unordered_map< int, int > freq, up;
for (auto v : nums) freq[v]++;
for (auto v : nums) {
if (!freq[v])continue;
if ( up[v] > 0 ) {
up[v] --;
up[v+1]++;
} else if ( freq[v + 1] > 0 && freq[v + 2] > 0 ) {
freq[v+1]--;
freq[v+2]--;
up[v+3] ++;
} else return false;
freq[v]--;
}
return true;
}
};