Skip to content

Commit

Permalink
feat: 1686. Stone Game VI
Browse files Browse the repository at this point in the history
Signed-off-by: ashing <axingfly@gmail.com>
  • Loading branch information
ronething committed Feb 2, 2024
1 parent e0c0aa7 commit 0ff59d5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
33 changes: 33 additions & 0 deletions leetcode/1686/1686. Stone Game VI.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package _686

import "sort"

func stoneGameVI(aliceValues []int, bobValues []int) int {
n := len(aliceValues)
totalValues := make([][2]int, n)

for i := 0; i < n; i++ {
totalValues[i] = [2]int{i, aliceValues[i] + bobValues[i]}
}

sort.Slice(totalValues, func(i, j int) bool {
return totalValues[i][1] > totalValues[j][1]
})

aliceScore, bobScore := 0, 0
for i, pair := range totalValues {
if i%2 == 0 {
aliceScore += aliceValues[pair[0]]
} else {
bobScore += bobValues[pair[0]]
}
}

if aliceScore > bobScore {
return 1
} else if aliceScore < bobScore {
return -1
} else {
return 0
}
}
31 changes: 31 additions & 0 deletions leetcode/1686/1686. Stone Game VI_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package _686

import "testing"

func Test_stoneGameVI(t *testing.T) {
type args struct {
aliceValues []int
bobValues []int
}
tests := []struct {
name string
args args
want int
}{
{
name: "one",
args: args{
aliceValues: []int{1, 3},
bobValues: []int{2, 1},
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stoneGameVI(tt.args.aliceValues, tt.args.bobValues); got != tt.want {
t.Errorf("stoneGameVI() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0ff59d5

Please sign in to comment.