-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path3495-minimum-operations-to-make-array-elements-zero.js
More file actions
44 lines (38 loc) · 1.34 KB
/
3495-minimum-operations-to-make-array-elements-zero.js
File metadata and controls
44 lines (38 loc) · 1.34 KB
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
/**
* 3495. Minimum Operations to Make Array Elements Zero
* https://leetcode.com/problems/minimum-operations-to-make-array-elements-zero/
* Difficulty: Hard
*
* You are given a 2D array queries, where queries[i] is of the form [l, r]. Each
* queries[i] defines an array of integers nums consisting of elements ranging
* from l to r, both inclusive.
*
* In one operation, you can:
* - Select two integers a and b from the array.
* - Replace them with floor(a / 4) and floor(b / 4).
*
* Your task is to determine the minimum number of operations required to reduce all
* elements of the array to zero for each query. Return the sum of the results for
* all queries.
*/
/**
* @param {number[][]} queries
* @return {number}
*/
var minOperations = function(queries) {
let result = 0;
for (const [start, end] of queries) {
let operationsNeeded = 0;
for (let depth = 1, previousBound = 1; depth < 17; depth++) {
const currentBound = previousBound * 4;
const intersectionLeft = Math.max(start, previousBound);
const intersectionRight = Math.min(end, currentBound - 1);
if (intersectionRight >= intersectionLeft) {
operationsNeeded += (intersectionRight - intersectionLeft + 1) * depth;
}
previousBound = currentBound;
}
result += Math.ceil(operationsNeeded / 2);
}
return result;
};