Skip to content

Commit 0ff59d5

Browse files
committed
feat: 1686. Stone Game VI
Signed-off-by: ashing <axingfly@gmail.com>
1 parent e0c0aa7 commit 0ff59d5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

leetcode/1686/1686. Stone Game VI.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _686
2+
3+
import "sort"
4+
5+
func stoneGameVI(aliceValues []int, bobValues []int) int {
6+
n := len(aliceValues)
7+
totalValues := make([][2]int, n)
8+
9+
for i := 0; i < n; i++ {
10+
totalValues[i] = [2]int{i, aliceValues[i] + bobValues[i]}
11+
}
12+
13+
sort.Slice(totalValues, func(i, j int) bool {
14+
return totalValues[i][1] > totalValues[j][1]
15+
})
16+
17+
aliceScore, bobScore := 0, 0
18+
for i, pair := range totalValues {
19+
if i%2 == 0 {
20+
aliceScore += aliceValues[pair[0]]
21+
} else {
22+
bobScore += bobValues[pair[0]]
23+
}
24+
}
25+
26+
if aliceScore > bobScore {
27+
return 1
28+
} else if aliceScore < bobScore {
29+
return -1
30+
} else {
31+
return 0
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _686
2+
3+
import "testing"
4+
5+
func Test_stoneGameVI(t *testing.T) {
6+
type args struct {
7+
aliceValues []int
8+
bobValues []int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want int
14+
}{
15+
{
16+
name: "one",
17+
args: args{
18+
aliceValues: []int{1, 3},
19+
bobValues: []int{2, 1},
20+
},
21+
want: 1,
22+
},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
if got := stoneGameVI(tt.args.aliceValues, tt.args.bobValues); got != tt.want {
27+
t.Errorf("stoneGameVI() = %v, want %v", got, tt.want)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)