This commit is contained in:
VicRen
2020-12-04 09:14:30 +08:00
parent e16c9db6e6
commit 8e6b84ac8d
2 changed files with 76 additions and 0 deletions

37
20201204/main.go Normal file
View File

@@ -0,0 +1,37 @@
package main
import (
"sort"
)
func main() {
}
func countMove(nums []int) int {
count := 0
l := len(nums)
for {
if isAllSame(nums) {
return count
}
sort.Ints(nums)
for i := 0; i < l-1; i++ {
nums[i] += 1
}
count++
}
}
func isAllSame(nums []int) bool {
if len(nums) == 0 {
return false
}
base := nums[0]
for _, n := range nums {
if base != n {
return false
}
}
return true
}

39
20201204/main_test.go Normal file
View File

@@ -0,0 +1,39 @@
package main
import (
"testing"
)
func Test_countMove(t *testing.T) {
type args struct {
src []int
}
tests := []struct {
name string
args args
want int
}{
{
"test1",
args{[]int{1, 2, 3}},
3,
},
{
"test2",
args{[]int{1, 1, 1}},
0,
},
{
"test3",
args{[]int{-100, 0, 100}},
300,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := countMove(tt.args.src); got != tt.want {
t.Errorf("countMove() = %v, want %v", got, tt.want)
}
})
}
}