Skip to content

Commit ed5f57c

Browse files
committed
[Math] Add a solution to Minimum Moves to Equal Array Elements
1 parent 3c81115 commit ed5f57c

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

DP/DungeonGame.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Question Link: https://leetcode.com/problems/dungeon-game/
3-
* Primary idea: Classic DP, current minimum health is decided by right and below minimum health
3+
* Primary idea: Classic DP, current minimum health is decided by right and below minimum health
4+
*
45
* Time Complexity: O(mn), Space Complexity: O(mn)
56
*/
67

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
3+
* Primary idea: Adding 1 to n - 1 elements is the same as subtracting 1 from one element,
4+
* the best way is to make all the elements equal to the min element.
5+
*
6+
* Time Complexity: O(n), Space Complexity: O(1)
7+
*/
8+
9+
class MinimumMovesEqualArrayElements {
10+
func minMoves(_ nums: [Int]) -> Int {
11+
let minNum = nums.min()!
12+
13+
return nums.reduce(0) { total, num in total + num - minNum }
14+
}
15+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 263 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 264 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -287,6 +287,7 @@
287287
[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| [Swift](./Math/RomanToInteger.swift)| Easy| O(n)| O(n)|
288288
[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [Swift](./Math/IntegerEnglishWords.swift)| Hard| O(n)| O(1)|
289289
[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| [Swift](./Math/RectangleArea.swift)| Easy| O(1)| O(1)|
290+
[Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)| [Swift](./Math/MinimumMovesEqualArrayElements.swift)| Easy| O(n)| O(1)|
290291
[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)| [Swift](./Math/TrappingRainWater.swift)| Hard| O(n)| O(n)|
291292
[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [Swift](./Math/ContainerMostWater.swift)| Medium| O(n)| O(1)|
292293
[Counting Bits](https://leetcode.com/problems/counting-bits/)| [Swift](./Math/CountingBits.swift)| Medium| O(n)| O(n)|

0 commit comments

Comments
 (0)