Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f063a1

Browse files
feat: add solutions to lc problem: No.2139 (#1725)
No.2139.Minimum Moves to Reach Target Score
1 parent 794530a commit 7f063a1

File tree

7 files changed

+304
-38
lines changed

7 files changed

+304
-38
lines changed

‎solution/2100-2199/2139.Minimum Moves to Reach Target Score/README.md‎

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
**方法一:倒推 + 贪心**
69+
70+
我们不妨从最终的状态开始倒推,假设最终的状态为 $target,ドル那么我们可以得到 $target$ 的前一个状态为 $target - 1$ 或者 $target / 2,ドル这取决于 $target$ 的奇偶性以及 $maxDoubles$ 的值。
71+
72+
如果 $target=1,ドル那么不需要任何操作,直接返回 0ドル$ 即可。
73+
74+
如果 $maxDoubles=0,ドル那么我们只能使用递增操作,因此我们需要 $target-1$ 次操作。
75+
76+
如果 $target$ 是偶数且 $maxDoubles>0,ドル那么我们可以使用加倍操作,因此我们需要 1ドル$ 次操作,然后递归求解 $target/2$ 和 $maxDoubles-1$。
77+
78+
如果 $target$ 是奇数,那么我们只能使用递增操作,因此我们需要 1ドル$ 次操作,然后递归求解 $target-1$ 和 $maxDoubles$。
79+
80+
时间复杂度 $O(\min(\log target, maxDoubles)),ドル空间复杂度 $O(\min(\log target, maxDoubles))$。
81+
82+
我们也可以将上述过程改为迭代的方式,这样可以避免递归的空间开销。
83+
6884
<!-- tabs:start -->
6985

7086
### **Python3**
@@ -83,6 +99,21 @@ class Solution:
8399
return 1 + self.minMoves(target - 1, maxDoubles)
84100
```
85101

102+
```python
103+
class Solution:
104+
def minMoves(self, target: int, maxDoubles: int) -> int:
105+
ans = 0
106+
while maxDoubles and target > 1:
107+
ans += 1
108+
if target % 2 == 1:
109+
target -= 1
110+
else:
111+
maxDoubles -= 1
112+
target >>= 1
113+
ans += target - 1
114+
return ans
115+
```
116+
86117
### **Java**
87118

88119
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -104,20 +135,65 @@ class Solution {
104135
}
105136
```
106137

138+
```java
139+
class Solution {
140+
public int minMoves(int target, int maxDoubles) {
141+
int ans = 0;
142+
while (maxDoubles > 0 && target > 1) {
143+
++ans;
144+
if (target % 2 == 1) {
145+
--target;
146+
} else {
147+
--maxDoubles;
148+
target >>= 1;
149+
}
150+
}
151+
ans += target - 1;
152+
return ans;
153+
}
154+
}
155+
```
156+
107157
### **C++**
108158

109159
```cpp
110160
class Solution {
111161
public:
112162
int minMoves(int target, int maxDoubles) {
113-
if (target == 1) return 0;
114-
if (maxDoubles == 0) return target - 1;
115-
if (target % 2 == 0 && maxDoubles) return 1 + minMoves(target >> 1, maxDoubles - 1);
163+
if (target == 1) {
164+
return 0;
165+
}
166+
if (maxDoubles == 0) {
167+
return target - 1;
168+
}
169+
if (target % 2 == 0 && maxDoubles > 0) {
170+
return 1 + minMoves(target >> 1, maxDoubles - 1);
171+
}
116172
return 1 + minMoves(target - 1, maxDoubles);
117173
}
118174
};
119175
```
120176
177+
```cpp
178+
class Solution {
179+
public:
180+
int minMoves(int target, int maxDoubles) {
181+
int ans = 0;
182+
while (maxDoubles > 0 && target > 1) {
183+
++ans;
184+
if (target % 2 == 1) {
185+
--target;
186+
} else {
187+
--maxDoubles;
188+
target >>= 1;
189+
}
190+
}
191+
ans += target - 1;
192+
return ans;
193+
}
194+
};
195+
```
196+
121197
### **Go**
122198

123199
```go
@@ -135,12 +211,54 @@ func minMoves(target int, maxDoubles int) int {
135211
}
136212
```
137213

138-
### **TypeScript**
214+
```go
215+
func minMoves(target int, maxDoubles int) (ans int) {
216+
for maxDoubles > 0 && target > 1 {
217+
ans++
218+
if target&1 == 1 {
219+
target--
220+
} else {
221+
maxDoubles--
222+
target >>= 1
223+
}
224+
}
225+
ans += target - 1
226+
return
227+
}
228+
```
139229

140-
<!-- 这里可写当前语言的特殊实现逻辑 -->
230+
### **TypeScript**
141231

142232
```ts
233+
function minMoves(target: number, maxDoubles: number): number {
234+
if (target === 1) {
235+
return 0;
236+
}
237+
if (maxDoubles === 0) {
238+
return target - 1;
239+
}
240+
if (target % 2 === 0 && maxDoubles) {
241+
return 1 + minMoves(target >> 1, maxDoubles - 1);
242+
}
243+
return 1 + minMoves(target - 1, maxDoubles);
244+
}
245+
```
143246

247+
```ts
248+
function minMoves(target: number, maxDoubles: number): number {
249+
let ans = 0;
250+
while (maxDoubles && target > 1) {
251+
++ans;
252+
if (target & 1) {
253+
--target;
254+
} else {
255+
--maxDoubles;
256+
target >>= 1;
257+
}
258+
}
259+
ans += target - 1;
260+
return ans;
261+
}
144262
```
145263

146264
### **...**

‎solution/2100-2199/2139.Minimum Moves to Reach Target Score/README_EN.md‎

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ Double again so x = 10
6161

6262
## Solutions
6363

64+
**Solution 1: Backtracking + Greedy**
65+
66+
Let's start by backtracking from the final state. Assuming the final state is $target,ドル we can get the previous state of $target$ as $target - 1$ or $target / 2,ドル depending on the parity of $target$ and the value of $maxDoubles$.
67+
68+
If $target=1,ドル no operation is needed, and we can return 0ドル$ directly.
69+
70+
If $maxDoubles=0,ドル we can only use the increment operation, so we need $target-1$ operations.
71+
72+
If $target$ is even and $maxDoubles>0,ドル we can use the doubling operation, so we need 1ドル$ operation, and then recursively solve $target/2$ and $maxDoubles-1$.
73+
74+
If $target$ is odd, we can only use the increment operation, so we need 1ドル$ operation, and then recursively solve $target-1$ and $maxDoubles$.
75+
76+
The time complexity is $O(\min(\log target, maxDoubles)),ドル and the space complexity is $O(\min(\log target, maxDoubles))$.
77+
78+
We can also change the above process to an iterative way to avoid the space overhead of recursion.
79+
6480
<!-- tabs:start -->
6581

6682
### **Python3**
@@ -77,6 +93,21 @@ class Solution:
7793
return 1 + self.minMoves(target - 1, maxDoubles)
7894
```
7995

96+
```python
97+
class Solution:
98+
def minMoves(self, target: int, maxDoubles: int) -> int:
99+
ans = 0
100+
while maxDoubles and target > 1:
101+
ans += 1
102+
if target % 2 == 1:
103+
target -= 1
104+
else:
105+
maxDoubles -= 1
106+
target >>= 1
107+
ans += target - 1
108+
return ans
109+
```
110+
80111
### **Java**
81112

82113
```java
@@ -96,20 +127,65 @@ class Solution {
96127
}
97128
```
98129

130+
```java
131+
class Solution {
132+
public int minMoves(int target, int maxDoubles) {
133+
int ans = 0;
134+
while (maxDoubles > 0 && target > 1) {
135+
++ans;
136+
if (target % 2 == 1) {
137+
--target;
138+
} else {
139+
--maxDoubles;
140+
target >>= 1;
141+
}
142+
}
143+
ans += target - 1;
144+
return ans;
145+
}
146+
}
147+
```
148+
99149
### **C++**
100150

101151
```cpp
102152
class Solution {
103153
public:
104154
int minMoves(int target, int maxDoubles) {
105-
if (target == 1) return 0;
106-
if (maxDoubles == 0) return target - 1;
107-
if (target % 2 == 0 && maxDoubles) return 1 + minMoves(target >> 1, maxDoubles - 1);
155+
if (target == 1) {
156+
return 0;
157+
}
158+
if (maxDoubles == 0) {
159+
return target - 1;
160+
}
161+
if (target % 2 == 0 && maxDoubles > 0) {
162+
return 1 + minMoves(target >> 1, maxDoubles - 1);
163+
}
108164
return 1 + minMoves(target - 1, maxDoubles);
109165
}
110166
};
111167
```
112168
169+
```cpp
170+
class Solution {
171+
public:
172+
int minMoves(int target, int maxDoubles) {
173+
int ans = 0;
174+
while (maxDoubles > 0 && target > 1) {
175+
++ans;
176+
if (target % 2 == 1) {
177+
--target;
178+
} else {
179+
--maxDoubles;
180+
target >>= 1;
181+
}
182+
}
183+
ans += target - 1;
184+
return ans;
185+
}
186+
};
187+
```
188+
113189
### **Go**
114190

115191
```go
@@ -127,10 +203,54 @@ func minMoves(target int, maxDoubles int) int {
127203
}
128204
```
129205

206+
```go
207+
func minMoves(target int, maxDoubles int) (ans int) {
208+
for maxDoubles > 0 && target > 1 {
209+
ans++
210+
if target&1 == 1 {
211+
target--
212+
} else {
213+
maxDoubles--
214+
target >>= 1
215+
}
216+
}
217+
ans += target - 1
218+
return
219+
}
220+
```
221+
130222
### **TypeScript**
131223

132224
```ts
225+
function minMoves(target: number, maxDoubles: number): number {
226+
if (target === 1) {
227+
return 0;
228+
}
229+
if (maxDoubles === 0) {
230+
return target - 1;
231+
}
232+
if (target % 2 === 0 && maxDoubles) {
233+
return 1 + minMoves(target >> 1, maxDoubles - 1);
234+
}
235+
return 1 + minMoves(target - 1, maxDoubles);
236+
}
237+
```
133238

239+
```ts
240+
function minMoves(target: number, maxDoubles: number): number {
241+
let ans = 0;
242+
while (maxDoubles && target > 1) {
243+
++ans;
244+
if (target & 1) {
245+
--target;
246+
} else {
247+
--maxDoubles;
248+
target >>= 1;
249+
}
250+
}
251+
ans += target - 1;
252+
return ans;
253+
}
134254
```
135255

136256
### **...**
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
class Solution {
22
public:
33
int minMoves(int target, int maxDoubles) {
4-
if (target == 1) return 0;
5-
if (maxDoubles == 0) return target - 1;
6-
if (target % 2 == 0 && maxDoubles) return 1 + minMoves(target >> 1, maxDoubles - 1);
7-
return 1 + minMoves(target - 1, maxDoubles);
4+
int ans = 0;
5+
while (maxDoubles > 0 && target > 1) {
6+
++ans;
7+
if (target % 2 == 1) {
8+
--target;
9+
} else {
10+
--maxDoubles;
11+
target >>= 1;
12+
}
13+
}
14+
ans += target - 1;
15+
return ans;
816
}
917
};
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
func minMoves(target int, maxDoubles int) int {
2-
if target == 1 {
3-
return 0
1+
func minMoves(target int, maxDoubles int) (ans int) {
2+
for maxDoubles > 0 && target > 1 {
3+
ans++
4+
if target&1 == 1 {
5+
target--
6+
} else {
7+
maxDoubles--
8+
target >>= 1
9+
}
410
}
5-
if maxDoubles == 0 {
6-
return target - 1
7-
}
8-
if target%2 == 0 && maxDoubles > 0 {
9-
return 1 + minMoves(target>>1, maxDoubles-1)
10-
}
11-
return 1 + minMoves(target-1, maxDoubles)
11+
ans += target - 1
12+
return
1213
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /