Skip to content

Commit 810b3cc

Browse files
authored
Create minimum-operations-to-make-array-elements-zero.cpp
1 parent 14a6412 commit 810b3cc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(qlogr)
2+
// Space: O(1)
3+
4+
// greedy
5+
class Solution {
6+
public:
7+
long long minOperations(vector<vector<int>>& queries) {
8+
int64_t result = 0;
9+
for (const auto &q : queries) {
10+
const int64_t l = q[0], r = q[1];
11+
int64_t total = 0;
12+
for (int64_t base = 1, i = 1; base <= r; base *= 4, ++i) {
13+
const int nl = max(l, base), nr = min(r, base * 4 - 1);
14+
if (nl <= nr) {
15+
total += i * (nr - nl + 1);
16+
}
17+
}
18+
result += (total + 1) / 2;
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)