Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b048eeb

Browse files
committed
Add solution #2139
1 parent ec1b489 commit b048eeb

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,772 LeetCode solutions in JavaScript
1+
# 1,773 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1639,6 +1639,7 @@
16391639
2135|[Count Words Obtained After Adding a Letter](./solutions/2135-count-words-obtained-after-adding-a-letter.js)|Medium|
16401640
2136|[Earliest Possible Day of Full Bloom](./solutions/2136-earliest-possible-day-of-full-bloom.js)|Hard|
16411641
2138|[Divide a String Into Groups of Size k](./solutions/2138-divide-a-string-into-groups-of-size-k.js)|Easy|
1642+
2139|[Minimum Moves to Reach Target Score](./solutions/2139-minimum-moves-to-reach-target-score.js)|Medium|
16421643
2140|[Solving Questions With Brainpower](./solutions/2140-solving-questions-with-brainpower.js)|Medium|
16431644
2145|[Count the Hidden Sequences](./solutions/2145-count-the-hidden-sequences.js)|Medium|
16441645
2154|[Keep Multiplying Found Values by Two](./solutions/2154-keep-multiplying-found-values-by-two.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 2139. Minimum Moves to Reach Target Score
3+
* https://leetcode.com/problems/minimum-moves-to-reach-target-score/
4+
* Difficulty: Medium
5+
*
6+
* You are playing a game with integers. You start with the integer 1 and you want to reach
7+
* the integer target.
8+
*
9+
* In one move, you can either:
10+
* - Increment the current integer by one (i.e., x = x + 1).
11+
* - Double the current integer (i.e., x = 2 * x).
12+
*
13+
* You can use the increment operation any number of times, however, you can only use the double
14+
* operation at most maxDoubles times.
15+
*
16+
* Given the two integers target and maxDoubles, return the minimum number of moves needed to
17+
* reach target starting with 1.
18+
*/
19+
20+
/**
21+
* @param {number} target
22+
* @param {number} maxDoubles
23+
* @return {number}
24+
*/
25+
var minMoves = function(target, maxDoubles) {
26+
let moves = 0;
27+
let current = target;
28+
29+
while (current > 1 && maxDoubles > 0) {
30+
if (current % 2 === 0) {
31+
current /= 2;
32+
maxDoubles--;
33+
} else {
34+
current--;
35+
}
36+
moves++;
37+
}
38+
39+
return moves + (current - 1);
40+
};

0 commit comments

Comments
 (0)