-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-number-of-potholes-that-can-be-fixed.cpp
87 lines (84 loc) · 2.44 KB
/
maximum-number-of-potholes-that-can-be-fixed.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
// Time: O(n)
// Space: O(n)
// counting sort, greedy
class Solution {
public:
int maxPotholes(string road, int budget) {
const auto& inplace_counting_sort = [](auto& 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));
}
};
vector<int> ls;
for (int i = 0, l = 0; i < size(road); ++i) {
++l;
if (i + 1 == size(road) || road[i + 1] != road[i]) {
if (road[i] == 'x') {
ls.emplace_back(l);
}
l = 0;
}
}
inplace_counting_sort(ls, false);
int result = 0;
for (int i = size(ls) - 1; i >= 0; --i) {
const int c = min(ls[i] + 1, budget);
if (c - 1 <= 0) {
break;
}
result += c - 1;
budget -= c;
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// sort, greedy
class Solution2 {
public:
int maxPotholes(string road, int budget) {
vector<int> ls;
for (int i = 0, l = 0; i < size(road); ++i) {
++l;
if (i + 1 == size(road) || road[i + 1] != road[i]) {
if (road[i] == 'x') {
ls.emplace_back(l);
}
l = 0;
}
}
sort(begin(ls), end(ls));
int result = 0;
for (int i = size(ls) - 1; i >= 0; --i) {
const int c = min(ls[i] + 1, budget);
if (c - 1) {
break;
}
result += c - 1;
budget -= c;
}
return result;
}
};