Skip to content

Commit 2c2b519

Browse files
committed
feat: add solutions to lc problem: No.1686
No.1686.Stone Game VI
1 parent 19fadde commit 2c2b519

File tree

6 files changed

+264
-2
lines changed

6 files changed

+264
-2
lines changed

Diff for: solution/1600-1699/1686.Stone Game VI/README.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,116 @@ Bob 会获胜。
7070

7171
<!-- 这里可写通用的实现逻辑 -->
7272

73+
**方法一:贪心 + 排序**
74+
75+
选取石头的最优化的策略是,让自己得分最高,同时让对手失分最多。因此,我们创建一个数组 `arr`,其中 `arr[i] = aliceValues[i] + bobValues[i]`,然后对 `arr` 进行降序排序。然后,我们从 `arr` 中取出石头,每次取出两个石头,分别给 Alice 和 Bob,直到 `arr` 中没有石头为止。最后,我们比较 Alice 和 Bob 的得分,得分高的人获胜。
76+
77+
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `aliceValues``bobValues` 的长度。
78+
7379
<!-- tabs:start -->
7480

7581
### **Python3**
7682

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

7985
```python
80-
86+
class Solution:
87+
def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
88+
arr = [(a + b, i)
89+
for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
90+
arr.sort(reverse=True)
91+
a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
92+
b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
93+
if a > b:
94+
return 1
95+
if a < b:
96+
return -1
97+
return 0
8198
```
8299

83100
### **Java**
84101

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

87104
```java
105+
class Solution {
106+
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
107+
int n = aliceValues.length;
108+
int[][] arr = new int[n][2];
109+
for (int i = 0; i < n; ++i) {
110+
arr[i] = new int[] {aliceValues[i] + bobValues[i], i};
111+
}
112+
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
113+
int a = 0, b = 0;
114+
for (int i = 0; i < n; ++i) {
115+
int j = arr[i][1];
116+
if (i % 2 == 0) {
117+
a += aliceValues[j];
118+
} else {
119+
b += bobValues[j];
120+
}
121+
}
122+
if (a == b) {
123+
return 0;
124+
}
125+
return a > b ? 1 : -1;
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
136+
int n = aliceValues.size();
137+
vector<pair<int, int>> arr(n);
138+
for (int i = 0; i < n; ++i) {
139+
arr[i] = {aliceValues[i] + bobValues[i], i};
140+
}
141+
sort(arr.rbegin(), arr.rend());
142+
int a = 0, b = 0;
143+
for (int i = 0; i < n; ++i) {
144+
int j = arr[i].second;
145+
if (i % 2 == 0) {
146+
a += aliceValues[j];
147+
} else {
148+
b += bobValues[j];
149+
}
150+
}
151+
if (a == b) return 0;
152+
return a > b ? 1 : -1;
153+
}
154+
};
155+
```
88156
157+
### **Go**
158+
159+
```go
160+
func stoneGameVI(aliceValues []int, bobValues []int) int {
161+
arr := make([][]int, len(aliceValues))
162+
for i, a := range aliceValues {
163+
b := bobValues[i]
164+
arr[i] = []int{a + b, i}
165+
}
166+
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
167+
a, b := 0, 0
168+
for i, v := range arr {
169+
if i%2 == 0 {
170+
a += aliceValues[v[1]]
171+
} else {
172+
b += bobValues[v[1]]
173+
}
174+
}
175+
if a == b {
176+
return 0
177+
}
178+
if a > b {
179+
return 1
180+
}
181+
return -1
182+
}
89183
```
90184

91185
### **...**

Diff for: solution/1600-1699/1686.Stone Game VI/README_EN.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,101 @@ Bob wins.
6969
### **Python3**
7070

7171
```python
72-
72+
class Solution:
73+
def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
74+
arr = [(a + b, i)
75+
for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
76+
arr.sort(reverse=True)
77+
a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
78+
b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
79+
if a > b:
80+
return 1
81+
if a < b:
82+
return -1
83+
return 0
7384
```
7485

7586
### **Java**
7687

7788
```java
89+
class Solution {
90+
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
91+
int n = aliceValues.length;
92+
int[][] arr = new int[n][2];
93+
for (int i = 0; i < n; ++i) {
94+
arr[i] = new int[] {aliceValues[i] + bobValues[i], i};
95+
}
96+
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
97+
int a = 0, b = 0;
98+
for (int i = 0; i < n; ++i) {
99+
int j = arr[i][1];
100+
if (i % 2 == 0) {
101+
a += aliceValues[j];
102+
} else {
103+
b += bobValues[j];
104+
}
105+
}
106+
if (a == b) {
107+
return 0;
108+
}
109+
return a > b ? 1 : -1;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
120+
int n = aliceValues.size();
121+
vector<pair<int, int>> arr(n);
122+
for (int i = 0; i < n; ++i) {
123+
arr[i] = {aliceValues[i] + bobValues[i], i};
124+
}
125+
sort(arr.rbegin(), arr.rend());
126+
int a = 0, b = 0;
127+
for (int i = 0; i < n; ++i) {
128+
int j = arr[i].second;
129+
if (i % 2 == 0) {
130+
a += aliceValues[j];
131+
} else {
132+
b += bobValues[j];
133+
}
134+
}
135+
if (a == b) return 0;
136+
return a > b ? 1 : -1;
137+
}
138+
};
139+
```
78140
141+
### **Go**
142+
143+
```go
144+
func stoneGameVI(aliceValues []int, bobValues []int) int {
145+
arr := make([][]int, len(aliceValues))
146+
for i, a := range aliceValues {
147+
b := bobValues[i]
148+
arr[i] = []int{a + b, i}
149+
}
150+
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
151+
a, b := 0, 0
152+
for i, v := range arr {
153+
if i%2 == 0 {
154+
a += aliceValues[v[1]]
155+
} else {
156+
b += bobValues[v[1]]
157+
}
158+
}
159+
if a == b {
160+
return 0
161+
}
162+
if a > b {
163+
return 1
164+
}
165+
return -1
166+
}
79167
```
80168

81169
### **...**

Diff for: solution/1600-1699/1686.Stone Game VI/Solution.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
4+
int n = aliceValues.size();
5+
vector<pair<int, int>> arr(n);
6+
for (int i = 0; i < n; ++i) {
7+
arr[i] = {aliceValues[i] + bobValues[i], i};
8+
}
9+
sort(arr.rbegin(), arr.rend());
10+
int a = 0, b = 0;
11+
for (int i = 0; i < n; ++i) {
12+
int j = arr[i].second;
13+
if (i % 2 == 0) {
14+
a += aliceValues[j];
15+
} else {
16+
b += bobValues[j];
17+
}
18+
}
19+
if (a == b) return 0;
20+
return a > b ? 1 : -1;
21+
}
22+
};

Diff for: solution/1600-1699/1686.Stone Game VI/Solution.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func stoneGameVI(aliceValues []int, bobValues []int) int {
2+
arr := make([][]int, len(aliceValues))
3+
for i, a := range aliceValues {
4+
b := bobValues[i]
5+
arr[i] = []int{a + b, i}
6+
}
7+
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
8+
a, b := 0, 0
9+
for i, v := range arr {
10+
if i%2 == 0 {
11+
a += aliceValues[v[1]]
12+
} else {
13+
b += bobValues[v[1]]
14+
}
15+
}
16+
if a == b {
17+
return 0
18+
}
19+
if a > b {
20+
return 1
21+
}
22+
return -1
23+
}

Diff for: solution/1600-1699/1686.Stone Game VI/Solution.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
3+
int n = aliceValues.length;
4+
int[][] arr = new int[n][2];
5+
for (int i = 0; i < n; ++i) {
6+
arr[i] = new int[] {aliceValues[i] + bobValues[i], i};
7+
}
8+
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
9+
int a = 0, b = 0;
10+
for (int i = 0; i < n; ++i) {
11+
int j = arr[i][1];
12+
if (i % 2 == 0) {
13+
a += aliceValues[j];
14+
} else {
15+
b += bobValues[j];
16+
}
17+
}
18+
if (a == b) {
19+
return 0;
20+
}
21+
return a > b ? 1 : -1;
22+
}
23+
}

Diff for: solution/1600-1699/1686.Stone Game VI/Solution.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
3+
arr = [(a + b, i)
4+
for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
5+
arr.sort(reverse=True)
6+
a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
7+
b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
8+
if a > b:
9+
return 1
10+
if a < b:
11+
return -1
12+
return 0

0 commit comments

Comments
 (0)