-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path659.cpp
More file actions
26 lines (26 loc) · 752 Bytes
/
659.cpp
File metadata and controls
26 lines (26 loc) · 752 Bytes
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
class Solution {
public:
bool isPossible(vector<int>& nums) {
unordered_map<int, int> numFreq;
unordered_map<int, int> endFreq;
for (auto& num : nums) numFreq[num]++;
for (auto& num : nums) {
if (numFreq[num] == 0) continue;
if (endFreq[num - 1] > 0) {
numFreq[num]--;
endFreq[num - 1]--;
endFreq[num]++;
}
else if (numFreq[num + 1] > 0 && numFreq[num + 2] > 0) {
numFreq[num]--;
numFreq[num + 1]--;
numFreq[num + 2]--;
endFreq[num + 2]++;
}
else {
return false;
}
}
return true;
}
};