Skip to content

Commit 289ac47

Browse files
authored
Create divide-array-in-sets-of-k-consecutive-numbers.cpp
1 parent f1ece26 commit 289ac47

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
bool isPossibleDivide(vector<int>& nums, int k) {
7+
map<int, int> count;
8+
for (const auto& num : nums) {
9+
++count[num];
10+
}
11+
for (const auto [num, c] : count) {
12+
if (!c) {
13+
continue;
14+
}
15+
for (int i = num; i < num + k; ++i) {
16+
if (count[i] < c) {
17+
return false;
18+
}
19+
count[i] -= c;
20+
}
21+
}
22+
return true;
23+
}
24+
};

0 commit comments

Comments
 (0)