Skip to content

Commit 8efaa1e

Browse files
authored
feat: add solutions to lc problem: No.1686 (doocs#2295)
No.1686.Stone Game VI
1 parent 4d8a34c commit 8efaa1e

File tree

7 files changed

+182
-103
lines changed

7 files changed

+182
-103
lines changed

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

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,21 @@ Bob 会获胜。
7070

7171
### 方法一:贪心 + 排序
7272

73-
选取石头的最优化的策略是,让自己得分最高,同时让对手失分最多。因此,我们创建一个数组 `arr`,其中 `arr[i] = aliceValues[i] + bobValues[i]`,然后对 `arr` 进行降序排序。然后,我们从 `arr` 中取出石头,每次取出两个石头,分别给 Alice 和 Bob,直到 `arr` 中没有石头为止。最后,我们比较 Alice 和 Bob 的得分,得分高的人获胜
73+
选取石头的最优化的策略是,让自己得分最高,同时让对手失分最多。因此,我们创建一个数组 $vals$,其中 $vals[i] = (aliceValues[i] + bobValues[i], i)$,表示第 $i$ 个石头的总价值和编号。然后我们对 $vals$ 按照总价值降序排序
7474

75-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `aliceValues``bobValues` 的长度。
75+
然后我们按照 $vals$ 的顺序,让 Alice 和 Bob 交替选取石头。Alice 选取 $vals$ 中的偶数位置的石头,Bob 选取 $vals$ 中的奇数位置的石头。最后比较 Alice 和 Bob 的得分,返回对应的结果。
76+
77+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$,其中 $n$ 为数组 `aliceValues``bobValues` 的长度。
7678

7779
<!-- tabs:start -->
7880

7981
```python
8082
class Solution:
8183
def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
82-
arr = [(a + b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
83-
arr.sort(reverse=True)
84-
a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
85-
b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
84+
vals = [(a + b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
85+
vals.sort(reverse=True)
86+
a = sum(aliceValues[i] for _, i in vals[::2])
87+
b = sum(bobValues[i] for _, i in vals[1::2])
8688
if a > b:
8789
return 1
8890
if a < b:
@@ -94,18 +96,18 @@ class Solution:
9496
class Solution {
9597
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
9698
int n = aliceValues.length;
97-
int[][] arr = new int[n][2];
99+
int[][] vals = new int[n][0];
98100
for (int i = 0; i < n; ++i) {
99-
arr[i] = new int[] {aliceValues[i] + bobValues[i], i};
101+
vals[i] = new int[] {aliceValues[i] + bobValues[i], i};
100102
}
101-
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
103+
Arrays.sort(vals, (a, b) -> b[0] - a[0]);
102104
int a = 0, b = 0;
103-
for (int i = 0; i < n; ++i) {
104-
int j = arr[i][1];
105-
if (i % 2 == 0) {
106-
a += aliceValues[j];
105+
for (int k = 0; k < n; ++k) {
106+
int i = vals[k][1];
107+
if (k % 2 == 0) {
108+
a += aliceValues[i];
107109
} else {
108-
b += bobValues[j];
110+
b += bobValues[i];
109111
}
110112
}
111113
if (a == b) {
@@ -120,50 +122,76 @@ class Solution {
120122
class Solution {
121123
public:
122124
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
125+
vector<pair<int, int>> vals;
123126
int n = aliceValues.size();
124-
vector<pair<int, int>> arr(n);
125127
for (int i = 0; i < n; ++i) {
126-
arr[i] = {aliceValues[i] + bobValues[i], i};
128+
vals.emplace_back(aliceValues[i] + bobValues[i], i);
127129
}
128-
sort(arr.rbegin(), arr.rend());
130+
sort(vals.rbegin(), vals.rend());
129131
int a = 0, b = 0;
130-
for (int i = 0; i < n; ++i) {
131-
int j = arr[i].second;
132-
if (i % 2 == 0) {
133-
a += aliceValues[j];
132+
for (int k = 0; k < n; ++k) {
133+
int i = vals[k].second;
134+
if (k % 2 == 0) {
135+
a += aliceValues[i];
134136
} else {
135-
b += bobValues[j];
137+
b += bobValues[i];
136138
}
137139
}
138-
if (a == b) return 0;
140+
if (a == b) {
141+
return 0;
142+
}
139143
return a > b ? 1 : -1;
140144
}
141145
};
142146
```
143147
144148
```go
145149
func stoneGameVI(aliceValues []int, bobValues []int) int {
146-
arr := make([][]int, len(aliceValues))
150+
vals := make([][2]int, len(aliceValues))
147151
for i, a := range aliceValues {
148-
b := bobValues[i]
149-
arr[i] = []int{a + b, i}
152+
vals[i] = [2]int{a + bobValues[i], i}
150153
}
151-
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
154+
slices.SortFunc(vals, func(a, b [2]int) int { return b[0] - a[0] })
152155
a, b := 0, 0
153-
for i, v := range arr {
154-
if i%2 == 0 {
155-
a += aliceValues[v[1]]
156+
for k, v := range vals {
157+
i := v[1]
158+
if k%2 == 0 {
159+
a += aliceValues[i]
156160
} else {
157-
b += bobValues[v[1]]
161+
b += bobValues[i]
158162
}
159163
}
160-
if a == b {
161-
return 0
162-
}
163164
if a > b {
164165
return 1
165166
}
166-
return -1
167+
if a < b {
168+
return -1
169+
}
170+
return 0
171+
}
172+
```
173+
174+
```ts
175+
function stoneGameVI(aliceValues: number[], bobValues: number[]): number {
176+
const n = aliceValues.length;
177+
const vals: number[][] = [];
178+
for (let i = 0; i < n; ++i) {
179+
vals.push([aliceValues[i] + bobValues[i], i]);
180+
}
181+
vals.sort((a, b) => b[0] - a[0]);
182+
let [a, b] = [0, 0];
183+
for (let k = 0; k < n; ++k) {
184+
const i = vals[k][1];
185+
if (k % 2 == 0) {
186+
a += aliceValues[i];
187+
} else {
188+
b += bobValues[i];
189+
}
190+
}
191+
if (a === b) {
192+
return 0;
193+
}
194+
return a > b ? 1 : -1;
167195
}
168196
```
169197

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

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,21 @@ Bob wins.
6666

6767
### Solution 1: Greedy + Sorting
6868

69-
The optimal strategy for picking stones is to maximize one's own score while making the opponent lose as much as possible. Therefore, we create an array `arr`, where `arr[i] = aliceValues[i] + bobValues[i]`, and then sort `arr` in descending order. Then, we take stones from `arr`, taking two stones each time, one for Alice and one for Bob, until there are no stones left in `arr`. Finally, we compare the scores of Alice and Bob, and the person with the higher score wins.
69+
The optimal strategy for picking stones is to maximize one's own score while making the opponent lose as much as possible. Therefore, we create an array $vals$, where $vals[i] = (aliceValues[i] + bobValues[i], i)$ represents the total value and index of the $i$-th stone. Then we sort $vals$ in descending order by total value.
7070

71-
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the arrays `aliceValues` and `bobValues`.
71+
Next, we let Alice and Bob pick stones alternately according to the order of $vals$. Alice picks the stones at even positions in $vals$, and Bob picks the stones at odd positions in $vals$. Finally, we compare the scores of Alice and Bob and return the corresponding result.
72+
73+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$, where $n$ is the length of the arrays `aliceValues` and `bobValues`.
7274

7375
<!-- tabs:start -->
7476

7577
```python
7678
class Solution:
7779
def stoneGameVI(self, aliceValues: List[int], bobValues: List[int]) -> int:
78-
arr = [(a + b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
79-
arr.sort(reverse=True)
80-
a = sum(aliceValues[v[1]] for i, v in enumerate(arr) if i % 2 == 0)
81-
b = sum(bobValues[v[1]] for i, v in enumerate(arr) if i % 2 == 1)
80+
vals = [(a + b, i) for i, (a, b) in enumerate(zip(aliceValues, bobValues))]
81+
vals.sort(reverse=True)
82+
a = sum(aliceValues[i] for _, i in vals[::2])
83+
b = sum(bobValues[i] for _, i in vals[1::2])
8284
if a > b:
8385
return 1
8486
if a < b:
@@ -90,18 +92,18 @@ class Solution:
9092
class Solution {
9193
public int stoneGameVI(int[] aliceValues, int[] bobValues) {
9294
int n = aliceValues.length;
93-
int[][] arr = new int[n][2];
95+
int[][] vals = new int[n][0];
9496
for (int i = 0; i < n; ++i) {
95-
arr[i] = new int[] {aliceValues[i] + bobValues[i], i};
97+
vals[i] = new int[] {aliceValues[i] + bobValues[i], i};
9698
}
97-
Arrays.sort(arr, (a, b) -> b[0] - a[0]);
99+
Arrays.sort(vals, (a, b) -> b[0] - a[0]);
98100
int a = 0, b = 0;
99-
for (int i = 0; i < n; ++i) {
100-
int j = arr[i][1];
101-
if (i % 2 == 0) {
102-
a += aliceValues[j];
101+
for (int k = 0; k < n; ++k) {
102+
int i = vals[k][1];
103+
if (k % 2 == 0) {
104+
a += aliceValues[i];
103105
} else {
104-
b += bobValues[j];
106+
b += bobValues[i];
105107
}
106108
}
107109
if (a == b) {
@@ -116,50 +118,76 @@ class Solution {
116118
class Solution {
117119
public:
118120
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
121+
vector<pair<int, int>> vals;
119122
int n = aliceValues.size();
120-
vector<pair<int, int>> arr(n);
121123
for (int i = 0; i < n; ++i) {
122-
arr[i] = {aliceValues[i] + bobValues[i], i};
124+
vals.emplace_back(aliceValues[i] + bobValues[i], i);
123125
}
124-
sort(arr.rbegin(), arr.rend());
126+
sort(vals.rbegin(), vals.rend());
125127
int a = 0, b = 0;
126-
for (int i = 0; i < n; ++i) {
127-
int j = arr[i].second;
128-
if (i % 2 == 0) {
129-
a += aliceValues[j];
128+
for (int k = 0; k < n; ++k) {
129+
int i = vals[k].second;
130+
if (k % 2 == 0) {
131+
a += aliceValues[i];
130132
} else {
131-
b += bobValues[j];
133+
b += bobValues[i];
132134
}
133135
}
134-
if (a == b) return 0;
136+
if (a == b) {
137+
return 0;
138+
}
135139
return a > b ? 1 : -1;
136140
}
137141
};
138142
```
139143
140144
```go
141145
func stoneGameVI(aliceValues []int, bobValues []int) int {
142-
arr := make([][]int, len(aliceValues))
146+
vals := make([][2]int, len(aliceValues))
143147
for i, a := range aliceValues {
144-
b := bobValues[i]
145-
arr[i] = []int{a + b, i}
148+
vals[i] = [2]int{a + bobValues[i], i}
146149
}
147-
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
150+
slices.SortFunc(vals, func(a, b [2]int) int { return b[0] - a[0] })
148151
a, b := 0, 0
149-
for i, v := range arr {
150-
if i%2 == 0 {
151-
a += aliceValues[v[1]]
152+
for k, v := range vals {
153+
i := v[1]
154+
if k%2 == 0 {
155+
a += aliceValues[i]
152156
} else {
153-
b += bobValues[v[1]]
157+
b += bobValues[i]
154158
}
155159
}
156-
if a == b {
157-
return 0
158-
}
159160
if a > b {
160161
return 1
161162
}
162-
return -1
163+
if a < b {
164+
return -1
165+
}
166+
return 0
167+
}
168+
```
169+
170+
```ts
171+
function stoneGameVI(aliceValues: number[], bobValues: number[]): number {
172+
const n = aliceValues.length;
173+
const vals: number[][] = [];
174+
for (let i = 0; i < n; ++i) {
175+
vals.push([aliceValues[i] + bobValues[i], i]);
176+
}
177+
vals.sort((a, b) => b[0] - a[0]);
178+
let [a, b] = [0, 0];
179+
for (let k = 0; k < n; ++k) {
180+
const i = vals[k][1];
181+
if (k % 2 == 0) {
182+
a += aliceValues[i];
183+
} else {
184+
b += bobValues[i];
185+
}
186+
}
187+
if (a === b) {
188+
return 0;
189+
}
190+
return a > b ? 1 : -1;
163191
}
164192
```
165193

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
class Solution {
22
public:
33
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
4+
vector<pair<int, int>> vals;
45
int n = aliceValues.size();
5-
vector<pair<int, int>> arr(n);
66
for (int i = 0; i < n; ++i) {
7-
arr[i] = {aliceValues[i] + bobValues[i], i};
7+
vals.emplace_back(aliceValues[i] + bobValues[i], i);
88
}
9-
sort(arr.rbegin(), arr.rend());
9+
sort(vals.rbegin(), vals.rend());
1010
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];
11+
for (int k = 0; k < n; ++k) {
12+
int i = vals[k].second;
13+
if (k % 2 == 0) {
14+
a += aliceValues[i];
1515
} else {
16-
b += bobValues[j];
16+
b += bobValues[i];
1717
}
1818
}
19-
if (a == b) return 0;
19+
if (a == b) {
20+
return 0;
21+
}
2022
return a > b ? 1 : -1;
2123
}
2224
};
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
func stoneGameVI(aliceValues []int, bobValues []int) int {
2-
arr := make([][]int, len(aliceValues))
2+
vals := make([][2]int, len(aliceValues))
33
for i, a := range aliceValues {
4-
b := bobValues[i]
5-
arr[i] = []int{a + b, i}
4+
vals[i] = [2]int{a + bobValues[i], i}
65
}
7-
sort.Slice(arr, func(i, j int) bool { return arr[i][0] > arr[j][0] })
6+
slices.SortFunc(vals, func(a, b [2]int) int { return b[0] - a[0] })
87
a, b := 0, 0
9-
for i, v := range arr {
10-
if i%2 == 0 {
11-
a += aliceValues[v[1]]
8+
for k, v := range vals {
9+
i := v[1]
10+
if k%2 == 0 {
11+
a += aliceValues[i]
1212
} else {
13-
b += bobValues[v[1]]
13+
b += bobValues[i]
1414
}
1515
}
16-
if a == b {
17-
return 0
18-
}
1916
if a > b {
2017
return 1
2118
}
22-
return -1
19+
if a < b {
20+
return -1
21+
}
22+
return 0
2323
}

0 commit comments

Comments
 (0)