Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64f2a6b

Browse files
committed
Add solution #2749
1 parent 5844ef0 commit 64f2a6b

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,7 @@
24462446
2744|[Find Maximum Number of String Pairs](./solutions/2744-find-maximum-number-of-string-pairs.js)|Easy|
24472447
2745|[Construct the Longest New String](./solutions/2745-construct-the-longest-new-string.js)|Medium|
24482448
2748|[Number of Beautiful Pairs](./solutions/2748-number-of-beautiful-pairs.js)|Easy|
2449+
2749|[Minimum Operations to Make the Integer Zero](./solutions/2749-minimum-operations-to-make-the-integer-zero.js)|Medium|
24492450
2751|[Robot Collisions](./solutions/2751-robot-collisions.js)|Hard|
24502451
2753|[Count Houses in a Circular Street II](./solutions/2753-count-houses-in-a-circular-street-ii.js)|Hard|
24512452
2754|[Bind Function to Context](./solutions/2754-bind-function-to-context.js)|Medium|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 2749. Minimum Operations to Make the Integer Zero
3+
* https://leetcode.com/problems/minimum-operations-to-make-the-integer-zero/
4+
* Difficulty: Medium
5+
*
6+
* You are given two integers num1 and num2.
7+
*
8+
* In one operation, you can choose integer i in the range [0, 60] and subtract 2i + num2 from num1.
9+
*
10+
* Return the integer denoting the minimum number of operations needed to make num1 equal to 0.
11+
*
12+
* If it is impossible to make num1 equal to 0, return -1.
13+
*/
14+
15+
/**
16+
* @param {number} num1
17+
* @param {number} num2
18+
* @return {number}
19+
*/
20+
var makeTheIntegerZero = function(num1, num2) {
21+
for (let operations = 1; operations <= 60; operations++) {
22+
const remaining = num1 - num2 * operations;
23+
24+
if (remaining < operations) {
25+
return -1;
26+
}
27+
28+
if (operations >= countBits(remaining)) {
29+
return operations;
30+
}
31+
}
32+
33+
return -1;
34+
35+
function countBits(n) {
36+
let count = 0;
37+
while (n > 0) {
38+
count += n & 1;
39+
n = Math.floor(n / 2);
40+
}
41+
return count;
42+
}
43+
};

0 commit comments

Comments
 (0)