-
Notifications
You must be signed in to change notification settings - Fork 0
/
1296.isPossibleDivide.cpp
59 lines (59 loc) · 1.66 KB
/
1296.isPossibleDivide.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
public:
bool isPossibleDivide(vector<int>& nums, int k) {
//special
int n = nums.size();
if(!k || n%k) return false;
sort(nums.begin(),nums.end());
vector<vector<int>> res;
int s;
for(int i = 0; i < n; i++){
bool flag = false;
for(int j = 0; j < res.size(); j++){
s = res[j].size();
if(s < k && (res[j][s-1]+1)== nums[i]){
res[j].push_back(nums[i]);
flag = true;
break;
}
}
if(!flag){
vector<int> item;
item.push_back(nums[i]);
res.push_back(item);
}
}
for(int i = 0; i < res.size(); i++){
if(res[i].size() != k)
return false;
}
return true;
//超时了 5⃣️ 36/39
}
};
//官方题解 缩小了规模
class Solution {
public:
bool isPossibleDivide(vector<int>& nums, int k) {
map<int, int> s;
for (int num: nums) {
++s[num];
}
for (auto iter = s.begin(); iter != s.end(); ++iter) {
int num = iter->first;
int occ = iter->second;
if (occ > 0) {
auto it = next(iter);
for (auto i = 1; i < k; ++i, ++it) {
if (it != s.end() && it->first == num + i && it->second >= occ) {
it->second -= occ;
}
else {
return false;
}
}
}
}
return true;
}
};