forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-operations-to-form-subsequence-with-target-sum.cpp
137 lines (131 loc) · 3.79 KB
/
minimum-operations-to-form-subsequence-with-target-sum.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
137
// Time: O(n)
// Space: O(logn)
// codeforces, https://codeforces.com/problemset/problem/1303/D
// counting sort, greedy
class Solution {
public:
int minOperations(vector<int>& nums, int target) {
const auto& floor_log2_x = [](int x) {
return 8 * sizeof(int) - __builtin_clz(x) - 1;
};
int64_t total = accumulate(begin(nums), end(nums), 0ll);
if (total < target) {
return -1;
}
vector<int> cnt(floor_log2_x(*max_element(cbegin(nums), cend(nums))) + 1);
for (const auto& x : nums) {
++cnt[floor_log2_x(x)];
}
int result = 0;
for (int i = size(cnt) - 1; i >= 0; --i) {
const int x = 1 << i;
for (int _ = 0; _ < cnt[i]; ++_) {
if (x <= target) {
target -= x;
total -= x;
} else if (total - x >= target) {
total -= x;
} else {
cnt[i - 1] += 2;
++result;
}
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(1)
// codeforces, https://codeforces.com/problemset/problem/1303/D
// sort, greedy
class Solution2 {
public:
int minOperations(vector<int>& nums, int target) {
int64_t total = accumulate(begin(nums), end(nums), 0ll);
if (total < target) {
return -1;
}
sort(begin(nums), end(nums));
int result = 0;
while (target) {
const int x = nums.back(); nums.pop_back();
if (x <= target) {
target -= x;
total -= x;
} else if (total - x >= target) {
total -= x;
} else {
nums.emplace_back(x / 2);
nums.emplace_back(x / 2);
++result;
}
}
return result;
}
};
// Time: O(nlogn)
// Space: O(n)
// codeforces, https://codeforces.com/problemset/problem/1303/D
// heap, greedy
class Solution3 {
public:
int minOperations(vector<int>& nums, int target) {
int64_t total = accumulate(begin(nums), end(nums), 0ll);
if (total < target) {
return -1;
}
int result = 0;
priority_queue<int> max_heap(cbegin(nums), cend(nums));
while (target) {
const int x = max_heap.top(); max_heap.pop();
if (x <= target) {
target -= x;
total -= x;
} else if (total - x >= target) {
total -= x;
} else {
max_heap.emplace(x / 2);
max_heap.emplace(x / 2);
++result;
}
}
return result;
}
};
// Time: O(nlogr)
// Space: O(logr)
// codeforces, https://codeforces.com/problemset/problem/1303/D
// bitmasks, greedy
class Solution4 {
public:
int minOperations(vector<int>& nums, int target) {
const auto& floor_log2_x = [](int x) {
return 8 * sizeof(int) - __builtin_clz(x) - 1;
};
if (accumulate(begin(nums), end(nums), 0ll) < target) {
return -1;
}
vector<int> cnt(floor_log2_x(*max_element(cbegin(nums), cend(nums))) + 1);
for (const auto& x : nums) {
++cnt[floor_log2_x(x)];
}
int result = 0, i = 0;
while (i < size(cnt)) {
if (target & (1 << i)) {
if (!cnt[i]) {
for (; i < size(cnt) && !cnt[i]; ++i) {
++result;
}
--cnt[i];
continue;
}
--cnt[i];
}
if (i + 1 < size(cnt)) {
cnt[i + 1] += cnt[i] / 2;
}
++i;
}
return result;
}
};