Skip to content

Commit 8d2a07b

Browse files
committed
feat: update solutions to lc problem: No.1033
No.1033.Moving Stones Until Consecutive
1 parent 0d0465d commit 8d2a07b

File tree

6 files changed

+98
-87
lines changed

6 files changed

+98
-87
lines changed

solution/1000-1099/1033.Moving Stones Until Consecutive/README.md

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
**方法一:脑筋急转弯**
50+
**方法一:分类讨论**
5151

52-
- 若 $3$ 个数已经相邻,则不用移动,直接返回结果 $[0,0]$;
53-
- 若 $3$ 个数中存在两数之差小于 $3$,最小只需移动 $1$ 次;
54-
- 其他情况最小只需移动 $2$ 次;
55-
- 两边逐个往中间靠,就是最大移动次数 $c - a - 2$。
52+
我们先将 $a, b, c$ 排序,记为 $x, y, z$,即 $x \lt y \lt z$。
53+
54+
接下来分情况讨论:
55+
56+
1. 如果 $z - x \leq 2$,说明 $3$ 个数已经相邻,不用移动,结果为 $[0, 0]$;
57+
1. 否则,如果 $y - x \lt 3$,或者 $z - y \lt 3$,说明有两个数只间隔一个位置,我们只需要把另一个数移动到这两个数的中间,最小移动次数为 $1$;其他情况,最小移动次数为 $2$;
58+
1. 最大移动次数就是两边的数字逐个往中间靠,最多移动 $z - x - 2$ 次。
59+
60+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5661

5762
<!-- tabs:start -->
5863

@@ -63,16 +68,13 @@
6368
```python
6469
class Solution:
6570
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
66-
a, b, c = sorted([a, b, c])
67-
ans = [0] * 2
68-
if c - a == 2:
69-
return ans
70-
if b - a < 3 or c - b < 3:
71-
ans[0] = 1
72-
else:
73-
ans[0] = 2
74-
ans[1] = c - a - 2
75-
return ans
71+
x, z = min(a, b, c), max(a, b, c)
72+
y = a + b + c - x - z
73+
mi = mx = 0
74+
if z - x > 2:
75+
mi = 1 if y - x < 3 or z - y < 3 else 2
76+
mx = z - x - 2
77+
return [mi, mx]
7678
```
7779

7880
### **Java**
@@ -85,9 +87,12 @@ class Solution {
8587
int x = Math.min(a, Math.min(b, c));
8688
int z = Math.max(a, Math.max(b, c));
8789
int y = a + b + c - x - z;
88-
int max = z - x - 2;
89-
int min = y - x == 1 && z - y == 1 ? 0 : y - x <= 2 || z - y <= 2 ? 1 : 2;
90-
return new int[] {min, max};
90+
int mi = 0, mx = 0;
91+
if (z - x > 2) {
92+
mi = y - x < 3 || z - y < 3 ? 1 : 2;
93+
mx = z - x - 2;
94+
}
95+
return new int[]{mi, mx};
9196
}
9297
}
9398
```
@@ -98,12 +103,14 @@ class Solution {
98103
class Solution {
99104
public:
100105
vector<int> numMovesStones(int a, int b, int c) {
101-
int x = min(min(a, b), c);
102-
int z = max(max(a, b), c);
106+
int x = min({a, b, c});
107+
int z = max({a, b, c});
103108
int y = a + b + c - x - z;
104-
if (z - x == 2) return {0, 0};
105-
int mx = z - x - 2;
106-
int mi = y - x < 3 || z - y < 3 ? 1 : 2;
109+
int mi = 0, mx = 0;
110+
if (z - x > 2) {
111+
mi = y - x < 3 || z - y < 3 ? 1 : 2;
112+
mx = z - x - 2;
113+
}
107114
return {mi, mx};
108115
}
109116
};
@@ -113,16 +120,16 @@ public:
113120
114121
```go
115122
func numMovesStones(a int, b int, c int) []int {
116-
x := min(min(a, b), c)
117-
z := max(max(a, b), c)
123+
x := min(a, min(b, c))
124+
z := max(a, max(b, c))
118125
y := a + b + c - x - z
119-
if z-x == 2 {
120-
return []int{0, 0}
121-
}
122-
mx := z - x - 2
123-
mi := 2
124-
if y-x < 3 || z-y < 3 {
125-
mi = 1
126+
mi, mx := 0, 0
127+
if z-x > 2 {
128+
mi = 2
129+
if y-x < 3 || z-y < 3 {
130+
mi = 1
131+
}
132+
mx = z - x - 2
126133
}
127134
return []int{mi, mx}
128135
}

solution/1000-1099/1033.Moving Stones Until Consecutive/README_EN.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,13 @@
5959
```python
6060
class Solution:
6161
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
62-
a, b, c = sorted([a, b, c])
63-
ans = [0] * 2
64-
if c - a == 2:
65-
return ans
66-
if b - a < 3 or c - b < 3:
67-
ans[0] = 1
68-
else:
69-
ans[0] = 2
70-
ans[1] = c - a - 2
71-
return ans
62+
x, z = min(a, b, c), max(a, b, c)
63+
y = a + b + c - x - z
64+
mi = mx = 0
65+
if z - x > 2:
66+
mi = 1 if y - x < 3 or z - y < 3 else 2
67+
mx = z - x - 2
68+
return [mi, mx]
7269
```
7370

7471
### **Java**
@@ -79,9 +76,12 @@ class Solution {
7976
int x = Math.min(a, Math.min(b, c));
8077
int z = Math.max(a, Math.max(b, c));
8178
int y = a + b + c - x - z;
82-
int max = z - x - 2;
83-
int min = y - x == 1 && z - y == 1 ? 0 : y - x <= 2 || z - y <= 2 ? 1 : 2;
84-
return new int[] {min, max};
79+
int mi = 0, mx = 0;
80+
if (z - x > 2) {
81+
mi = y - x < 3 || z - y < 3 ? 1 : 2;
82+
mx = z - x - 2;
83+
}
84+
return new int[]{mi, mx};
8585
}
8686
}
8787
```
@@ -92,12 +92,14 @@ class Solution {
9292
class Solution {
9393
public:
9494
vector<int> numMovesStones(int a, int b, int c) {
95-
int x = min(min(a, b), c);
96-
int z = max(max(a, b), c);
95+
int x = min({a, b, c});
96+
int z = max({a, b, c});
9797
int y = a + b + c - x - z;
98-
if (z - x == 2) return {0, 0};
99-
int mx = z - x - 2;
100-
int mi = y - x < 3 || z - y < 3 ? 1 : 2;
98+
int mi = 0, mx = 0;
99+
if (z - x > 2) {
100+
mi = y - x < 3 || z - y < 3 ? 1 : 2;
101+
mx = z - x - 2;
102+
}
101103
return {mi, mx};
102104
}
103105
};
@@ -107,16 +109,16 @@ public:
107109
108110
```go
109111
func numMovesStones(a int, b int, c int) []int {
110-
x := min(min(a, b), c)
111-
z := max(max(a, b), c)
112+
x := min(a, min(b, c))
113+
z := max(a, max(b, c))
112114
y := a + b + c - x - z
113-
if z-x == 2 {
114-
return []int{0, 0}
115-
}
116-
mx := z - x - 2
117-
mi := 2
118-
if y-x < 3 || z-y < 3 {
119-
mi = 1
115+
mi, mx := 0, 0
116+
if z-x > 2 {
117+
mi = 2
118+
if y-x < 3 || z-y < 3 {
119+
mi = 1
120+
}
121+
mx = z - x - 2
120122
}
121123
return []int{mi, mx}
122124
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
class Solution {
22
public:
33
vector<int> numMovesStones(int a, int b, int c) {
4-
int x = min(min(a, b), c);
5-
int z = max(max(a, b), c);
4+
int x = min({a, b, c});
5+
int z = max({a, b, c});
66
int y = a + b + c - x - z;
7-
if (z - x == 2) return {0, 0};
8-
int mx = z - x - 2;
9-
int mi = y - x < 3 || z - y < 3 ? 1 : 2;
7+
int mi = 0, mx = 0;
8+
if (z - x > 2) {
9+
mi = y - x < 3 || z - y < 3 ? 1 : 2;
10+
mx = z - x - 2;
11+
}
1012
return {mi, mx};
1113
}
1214
};

solution/1000-1099/1033.Moving Stones Until Consecutive/Solution.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
func numMovesStones(a int, b int, c int) []int {
2-
x := min(min(a, b), c)
3-
z := max(max(a, b), c)
2+
x := min(a, min(b, c))
3+
z := max(a, max(b, c))
44
y := a + b + c - x - z
5-
if z-x == 2 {
6-
return []int{0, 0}
7-
}
8-
mx := z - x - 2
9-
mi := 2
10-
if y-x < 3 || z-y < 3 {
11-
mi = 1
5+
mi, mx := 0, 0
6+
if z-x > 2 {
7+
mi = 2
8+
if y-x < 3 || z-y < 3 {
9+
mi = 1
10+
}
11+
mx = z - x - 2
1212
}
1313
return []int{mi, mx}
1414
}

solution/1000-1099/1033.Moving Stones Until Consecutive/Solution.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ public int[] numMovesStones(int a, int b, int c) {
33
int x = Math.min(a, Math.min(b, c));
44
int z = Math.max(a, Math.max(b, c));
55
int y = a + b + c - x - z;
6-
int max = z - x - 2;
7-
int min = y - x == 1 && z - y == 1 ? 0 : y - x <= 2 || z - y <= 2 ? 1 : 2;
8-
return new int[] {min, max};
6+
int mi = 0, mx = 0;
7+
if (z - x > 2) {
8+
mi = y - x < 3 || z - y < 3 ? 1 : 2;
9+
mx = z - x - 2;
10+
}
11+
return new int[]{mi, mx};
912
}
10-
}
13+
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
class Solution:
22
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
3-
a, b, c = sorted([a, b, c])
4-
ans = [0] * 2
5-
if c - a == 2:
6-
return ans
7-
if b - a < 3 or c - b < 3:
8-
ans[0] = 1
9-
else:
10-
ans[0] = 2
11-
ans[1] = c - a - 2
12-
return ans
3+
x, z = min(a, b, c), max(a, b, c)
4+
y = a + b + c - x - z
5+
mi = mx = 0
6+
if z - x > 2:
7+
mi = 1 if y - x < 3 or z - y < 3 else 2
8+
mx = z - x - 2
9+
return [mi, mx]

0 commit comments

Comments
 (0)