diff --git a/20201204/main.go b/20201204/main.go new file mode 100644 index 0000000..d9d50d4 --- /dev/null +++ b/20201204/main.go @@ -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 +} diff --git a/20201204/main_test.go b/20201204/main_test.go new file mode 100644 index 0000000..1fb72c7 --- /dev/null +++ b/20201204/main_test.go @@ -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) + } + }) + } +}