forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-number-of-groups-with-increasing-length.cpp
136 lines (128 loc) · 3.98 KB
/
maximum-number-of-groups-with-increasing-length.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Time: O(n)
// Space: O(n)
// constructive algorithms, counting sort, greedy
class Solution {
public:
int maxIncreasingGroups(vector<int>& usageLimits) {
const auto& inplace_counting_sort = [](vector<int> *nums, bool is_reverse) {
if (empty(*nums)) {
return;
}
const int max_num = *max_element(cbegin(*nums), cend(*nums));
vector<int> count(max_num + 1);
for (const auto& num : *nums) {
++count[num];
}
for (int i = 1; i < size(count); ++i) {
count[i] += count[i - 1];
}
for (int i = size(*nums) - 1; i >= 0; --i) { // inplace but unstable sort
while ((*nums)[i] >= 0) {
--count[(*nums)[i]];
const int j = count[(*nums)[i]];
tie((*nums)[i], (*nums)[j]) = pair((*nums)[j], ~(*nums)[i]);
}
}
for (auto& num : *nums) {
num = ~num; // restore values
}
if (is_reverse) { // unstable sort
reverse(begin(*nums), end(*nums));
}
};
for (auto& x : usageLimits) {
x = min(x, static_cast<int>(size(usageLimits)));
}
inplace_counting_sort(&usageLimits, false);
int result = 0;
int64_t curr = 0;
for (const auto& x : usageLimits) {
curr += x;
if (curr >= result + 1) {
curr -= result + 1;
++result;
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(1)
// constructive algorithms, sort, greedy
class Solution2 {
public:
int maxIncreasingGroups(vector<int>& usageLimits) {
sort(begin(usageLimits), end(usageLimits));
int result = 0;
int64_t curr = 0;
for (const auto& x : usageLimits) {
curr += x;
if (curr >= result + 1) {
curr -= result + 1;
++result;
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(1)
// constructive algorithms, sort, binary search, greedy
class Solution3 {
public:
int maxIncreasingGroups(vector<int>& usageLimits) {
const auto& check = [&](int l) {
int64_t curr = 0;
for (int i = 0; i < l; ++i) {
curr += usageLimits[(size(usageLimits) - 1) - i] - (l - i);
curr = min(curr, static_cast<int64_t>(0));
}
for (int i = 0; i < size(usageLimits) - l; ++i) {
curr += usageLimits[i];
}
return curr >= 0;
};
sort(begin(usageLimits), end(usageLimits));
int left = 1, right = size(usageLimits);
while (left <= right) {
const int mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
};
// Time: O(nlogn)
// Space: O(n)
// constructive algorithms, sort, binary search, greedy, prefix sum
class Solution4 {
public:
int maxIncreasingGroups(vector<int>& usageLimits) {
sort(begin(usageLimits), end(usageLimits));
vector<int64_t> prefix(size(usageLimits) + 1);
for (int i = 0; i < size(usageLimits); ++i) {
prefix[i + 1] = prefix[i] + usageLimits[i];
}
const auto& check = [&](int l) {
for (int i = 1; i <= l; ++i) {
if (static_cast<int64_t>(i + 1) * i / 2 > prefix[size(usageLimits) - (l - i)]) {
return false;
}
}
return true;
};
int left = 1, right = size(usageLimits);
while (left <= right) {
const int mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
};