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 c6328f0

Browse files
committed
feat: add solutions to lc problem: No.1033
No.1033.Moving Stones Until Consecutive
1 parent 81add0e commit c6328f0

File tree

5 files changed

+201
-2
lines changed

5 files changed

+201
-2
lines changed

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

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,99 @@
4747

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

50+
**方法一:脑筋急转弯**
51+
52+
- 若 3ドル$ 个数已经相邻,则不用移动,直接返回结果 $[0,0]$;
53+
- 若 3ドル$ 个数中存在两数之差小于 3ドル,ドル最小只需移动 1ドル$ 次;
54+
- 其他情况最小只需移动 2ドル$ 次;
55+
- 两边逐个往中间靠,就是最大移动次数 $c - a - 2$。
56+
5057
<!-- tabs:start -->
5158

5259
### **Python3**
5360

5461
<!-- 这里可写当前语言的特殊实现逻辑 -->
5562

5663
```python
57-
64+
class Solution:
65+
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
5876
```
5977

6078
### **Java**
6179

6280
<!-- 这里可写当前语言的特殊实现逻辑 -->
6381

6482
```java
83+
class Solution {
84+
public int[] numMovesStones(int a, int b, int c) {
85+
int x = Math.min(a, Math.min(b, c));
86+
int z = Math.max(a, Math.max(b, c));
87+
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};
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
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);
103+
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;
107+
return {mi, mx};
108+
}
109+
};
110+
```
65111
112+
### **Go**
113+
114+
```go
115+
func numMovesStones(a int, b int, c int) []int {
116+
x := min(min(a, b), c)
117+
z := max(max(a, b), c)
118+
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+
}
127+
return []int{mi, mx}
128+
}
129+
130+
func max(a, b int) int {
131+
if a > b {
132+
return a
133+
}
134+
return b
135+
}
136+
137+
func min(a, b int) int {
138+
if a < b {
139+
return a
140+
}
141+
return b
142+
}
66143
```
67144

68145
### **...**

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,83 @@
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
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
6172
```
6273

6374
### **Java**
6475

6576
```java
77+
class Solution {
78+
public int[] numMovesStones(int a, int b, int c) {
79+
int x = Math.min(a, Math.min(b, c));
80+
int z = Math.max(a, Math.max(b, c));
81+
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};
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
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);
97+
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;
101+
return {mi, mx};
102+
}
103+
};
104+
```
66105
106+
### **Go**
107+
108+
```go
109+
func numMovesStones(a int, b int, c int) []int {
110+
x := min(min(a, b), c)
111+
z := max(max(a, b), c)
112+
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
120+
}
121+
return []int{mi, mx}
122+
}
123+
124+
func max(a, b int) int {
125+
if a > b {
126+
return a
127+
}
128+
return b
129+
}
130+
131+
func min(a, b int) int {
132+
if a < b {
133+
return a
134+
}
135+
return b
136+
}
67137
```
68138

69139
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
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);
6+
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;
10+
return {mi, mx};
11+
}
12+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func numMovesStones(a int, b int, c int) []int {
2+
x := min(min(a, b), c)
3+
z := max(max(a, b), c)
4+
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
12+
}
13+
return []int{mi, mx}
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
21+
}
22+
23+
func min(a, b int) int {
24+
if a < b {
25+
return a
26+
}
27+
return b
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
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

0 commit comments

Comments
(0)

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