Skip to content

Commit 1529330

Browse files
committed
Minimum moves to equal array elements.
1 parent 9385e17 commit 1529330

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.
3+
4+
In one move, you can increment n - 1 elements of the array by 1.
5+
6+
 
7+
8+
Example 1:
9+
Input: nums = [1,2,3]
10+
Output: 3
11+
Explanation: Only three moves are needed (remember each move increments two elements):
12+
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
13+
14+
Example 2:
15+
Input: nums = [1,1,1]
16+
Output: 0
17+
 
18+
19+
Constraints:
20+
- n == nums.length
21+
- 1 <= nums.length <= 105
22+
- -109 <= nums[i] <= 109
23+
- The answer is guaranteed to fit in a 32-bit integer.
24+
*/
25+
class Solution {
26+
func minMoves(_ nums: [Int]) -> Int {
27+
let newNums = nums.sorted()
28+
if newNums.first! == newNums.last! {
29+
return 0
30+
}
31+
let mini = newNums.first!
32+
var sum = 0
33+
for (i, e) in newNums.enumerated() {
34+
if i > 0 {
35+
sum += (e - mini)
36+
}
37+
}
38+
return sum
39+
}
40+
}
41+
42+
let s = Solution()
43+
let r = s.minMoves([2, 4, 5, 7])
44+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/453.Minimum Moves to Equal Array Elements.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
85. [Add Strings](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/415.Add%20Strings.playground/Contents.swift)
9090
86. [Number of Segments in a String](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/434.Number%20of%20Segments%20in%20a%20String.playground/Contents.swift)
9191
87. [Arranging Coins](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/441.Arranging%20Coins.playground/Contents.swift)
92-
88. [Find All Numbers Disappeared in an Array](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/448.Find%20All%20Numbers%20Disappeared%20in%20an%20Array.playground/Contents.swift)
92+
88. [Find All Numbers Disappeared in an Array](c)
93+
89. [Minimum Moves to Equal Array Elements](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/448.Minimum%20Moves%20toEqual%20Array%20Elements.playground/Contents.swift)
9394

9495
#### Medium
9596

0 commit comments

Comments
 (0)