Skip to content

Commit e2b23f1

Browse files
author
Shuo
authored
Merge pull request #739 from openset/develop
Add: Minimum Moves to Equal Array Elements
2 parents 95d38c9 + 7828e29 commit e2b23f1

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <span id="1285">1285</span> | [Find the Start and End Number of Continuous Ranges](https://leetcode.com/problems/find-the-start-and-end-number-of-continuous-ranges) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/find-the-start-and-end-number-of-continuous-ranges) | Medium |
6566
| <span id="1284">1284</span> | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix "转化为全零矩阵的最少反转次数") | [Go](https://github.com/openset/leetcode/tree/master/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix) | Hard |
6667
| <span id="1283">1283</span> | [Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold "使结果不超过阈值的最小除数") | [Go](https://github.com/openset/leetcode/tree/master/problems/find-the-smallest-divisor-given-a-threshold) | Medium |
6768
| <span id="1282">1282</span> | [Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to "用户分组") | [Go](https://github.com/openset/leetcode/tree/master/problems/group-the-people-given-the-group-size-they-belong-to) | Medium |

internal/leetcode/problems_status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ var problemStatus = map[int]bool{
114114
443: true,
115115
445: true,
116116
448: true,
117+
453: true,
117118
455: true,
118119
459: true,
119120
461: true,
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
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+
in: []int{5, 3, 1},
18+
want: 6,
19+
},
20+
}
21+
for _, tt := range tests {
22+
got := minMoves(tt.in)
23+
if got != tt.want {
24+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)