Skip to content

Commit 2143f5c

Browse files
author
openset
committed
Add: Minimum Moves to Equal Array Elements
1 parent 845e868 commit 2143f5c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
package problem453
2+
3+
func minMoves(nums []int) int {
4+
sum, min, n := 0, nums[0], len(nums)
5+
for _, num := range nums {
6+
sum += num
7+
if min > num {
8+
min = num
9+
}
10+
}
11+
return sum - n*min
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
11
package problem453
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in []int
7+
want int
8+
}
9+
10+
func TestMinMoves(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: []int{1, 2, 3},
14+
want: 3,
15+
},
16+
}
17+
for _, tt := range tests {
18+
got := minMoves(tt.in)
19+
if got != tt.want {
20+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)