Divide Array in Sets of K Consecutive Numbers LeetCode solution in C++

1
class Solution {
2
public:
3
bool isPossibleDivide(vector<int> &nums, int k) {
4
if (nums.size() % k != 0) {
5
return false;
6
}
7

8
unordered_map<int, int> map;
9
for (int num : nums) {
10
map[num]++;
11
}
12

13
sort(nums.begin(), nums.end());
14

15
for (int num : nums) {
16
if (map[num] > 0) {
17
for (int i = num + 1; i < num + k; i++) {
18
if (map[i] == 0) {
19
return false;
20
}
21
map[i]--;
22
}
23
map[num]--;
24
}
25
}
26

27
return true;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0