File tree 2 files changed +42
-1
lines changed
2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,772 LeetCode solutions in JavaScript
1
+ # 1,773 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1639
1639
2135|[ Count Words Obtained After Adding a Letter] ( ./solutions/2135-count-words-obtained-after-adding-a-letter.js ) |Medium|
1640
1640
2136|[ Earliest Possible Day of Full Bloom] ( ./solutions/2136-earliest-possible-day-of-full-bloom.js ) |Hard|
1641
1641
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|
1642
1643
2140|[ Solving Questions With Brainpower] ( ./solutions/2140-solving-questions-with-brainpower.js ) |Medium|
1643
1644
2145|[ Count the Hidden Sequences] ( ./solutions/2145-count-the-hidden-sequences.js ) |Medium|
1644
1645
2154|[ Keep Multiplying Found Values by Two] ( ./solutions/2154-keep-multiplying-found-values-by-two.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments