Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1ff5996

Browse files
Implement minimum operations to make array elements zero
1 parent 41669d8 commit 1ff5996

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public long minOperations(int[][] queries) {
3+
long result = 0;
4+
for (int[] query : queries) {
5+
long firstCount = countOperations(query[1]);
6+
long secondCount = countOperations(query[0] - 1);
7+
result += (firstCount - secondCount + 1) / 2;
8+
}
9+
return result;
10+
}
11+
12+
private long countOperations(int num) {
13+
long count = 0;
14+
int idx = 1;
15+
int base = 1;
16+
while (base <= num) {
17+
int end = Math.min(base * 2 - 1, num);
18+
count += (long) ((idx + 1) / 2) * (end - base + 1);
19+
idx++;
20+
base *= 2;
21+
}
22+
return count;
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /