Skip to content

Commit

Permalink
[Math] Add a solution to Minimum Moves to Equal Array Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jul 5, 2018
1 parent 3c81115 commit ed5f57c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion DP/DungeonGame.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Question Link: https://leetcode.com/problems/dungeon-game/
* Primary idea: Classic DP, current minimum health is decided by right and below minimum health
* Primary idea: Classic DP, current minimum health is decided by right and below minimum health
*
* Time Complexity: O(mn), Space Complexity: O(mn)
*/

Expand Down
15 changes: 15 additions & 0 deletions Math/MinimumMovesEqualArrayElements.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Question Link: https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
* Primary idea: Adding 1 to n - 1 elements is the same as subtracting 1 from one element,
* the best way is to make all the elements equal to the min element.
*
* Time Complexity: O(n), Space Complexity: O(1)
*/

class MinimumMovesEqualArrayElements {
func minMoves(_ nums: [Int]) -> Int {
let minNum = nums.min()!

return nums.reduce(0) { total, num in total + num - minNum }
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -287,6 +287,7 @@
[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| [Swift](./Math/RomanToInteger.swift)| Easy| O(n)| O(n)|
[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [Swift](./Math/IntegerEnglishWords.swift)| Hard| O(n)| O(1)|
[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| [Swift](./Math/RectangleArea.swift)| Easy| O(1)| O(1)|
[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)|
[Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)| [Swift](./Math/TrappingRainWater.swift)| Hard| O(n)| O(n)|
[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [Swift](./Math/ContainerMostWater.swift)| Medium| O(n)| O(1)|
[Counting Bits](https://leetcode.com/problems/counting-bits/)| [Swift](./Math/CountingBits.swift)| Medium| O(n)| O(n)|
Expand Down

0 comments on commit ed5f57c

Please sign in to comment.