-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2139-MinimumMovesToReachTargetScore.go
More file actions
91 lines (79 loc) · 2.42 KB
/
2139-MinimumMovesToReachTargetScore.go
File metadata and controls
91 lines (79 loc) · 2.42 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
// 2139. Minimum Moves to Reach Target Score
// You are playing a game with integers.
// You start with the integer 1 and you want to reach the integer target.
// In one move, you can either:
// Increment the current integer by one (i.e., x = x + 1).
// Double the current integer (i.e., x = 2 * x).
// You can use the increment operation any number of times,
// however, you can only use the double operation at most maxDoubles times.
// Given the two integers target and maxDoubles,
// return the minimum number of moves needed to reach target starting with 1.
// Example 1:
// Input: target = 5, maxDoubles = 0
// Output: 4
// Explanation: Keep incrementing by 1 until you reach target.
// Example 2:
// Input: target = 19, maxDoubles = 2
// Output: 7
// Explanation: Initially, x = 1
// Increment 3 times so x = 4
// Double once so x = 8
// Increment once so x = 9
// Double again so x = 18
// Increment once so x = 19
// Example 3:
// Input: target = 10, maxDoubles = 4
// Output: 4
// Explanation: Initially, x = 1
// Increment once so x = 2
// Double once so x = 4
// Increment once so x = 5
// Double again so x = 10
// Constraints:
// 1 <= target <= 10^9
// 0 <= maxDoubles <= 100
import "fmt"
func minMoves(target int, maxDoubles int) int {
res := 0
for target > 0 && maxDoubles > 0 {
if target % 2 == 1 {
target--
} else {
maxDoubles--
target /= 2
}
res++
}
return res + target - 1
}
func main() {
// Example 1:
// Input: target = 5, maxDoubles = 0
// Output: 4
// Explanation: Keep incrementing by 1 until you reach target.
fmt.Println(minMoves(5, 0)) // 4
// Example 2:
// Input: target = 19, maxDoubles = 2
// Output: 7
// Explanation: Initially, x = 1
// Increment 3 times so x = 4
// Double once so x = 8
// Increment once so x = 9
// Double again so x = 18
// Increment once so x = 19
fmt.Println(minMoves(19, 2)) // 7
// Example 3:
// Input: target = 10, maxDoubles = 4
// Output: 4
// Explanation: Initially, x = 1
// Increment once so x = 2
// Double once so x = 4
// Increment once so x = 5
// Double again so x = 10
fmt.Println(minMoves(10, 4)) // 4
fmt.Println(minMoves(1, 0)) // 0
fmt.Println(minMoves(1_000_000_000, 100)) // 41
fmt.Println(minMoves(1_000_000_000, 0)) // 999999999
fmt.Println(minMoves(1, 100)) // 0
}