aboutsummaryrefslogtreecommitdiff
path: root/split_array_consec_subseq.cpp
blob: 622e296c7163893d9e2f180ef62e542898966d47 (plain)
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
#include "leetcode.h"

class Solution {
public:
  bool isPossible(vector<int> &nums) {
    unordered_map<int, int> um;
    for (auto &it : nums)
      um[it]++;
    for (auto &it : nums) {
      if (um[it] == 0)
        continue;
      int freq = um[it], curr = it, count = 0;
      while (um.count(curr) && um[curr] >= freq) {
        freq = fmax(freq, um[curr]);
        um[curr]--;
        count++;
        curr++;
      }
      if (count < 3)
        return false;
    }
    return true;
  }
};

int main() {
  Solution obj;
  vector<int> nums = {1, 2, 3, 4, 5};
  if (obj.isPossible(nums))
    cout << "true";
  else
    cout << "false";
}