Skip to content

Commit a8656bc

Browse files
correct minimum moves to equal array elements
1 parent a7ab087 commit a8656bc

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

‎EASY/src/easy/MinimumMovestoEqualArrayElements.java

+9-34
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
11
package easy;
22

3-
import java.util.Comparator;
4-
import java.util.PriorityQueue;
5-
import java.util.Queue;
6-
73
public class MinimumMovestoEqualArrayElements {
8-
4+
/**Looked at this solution: https://discuss.leetcode.com/topic/66557/java-o-n-solution-short
5+
* i.e. Add 1 to n-1 elements basically equals to subtracting 1 from one element. So the easiest way
6+
* to make all elements in this array equal is to make all of them equal to the minimum element.*/
97
public static int minMoves(int[] nums) {
10-
Queue<Integer> heap = new PriorityQueue<Integer>(new Comparator<Integer>(){
11-
@Override
12-
public int compare(Integer o1, Integer o2) {
13-
if(o1 > o2) return 1;
14-
else if(o1 < o2) return -1;
15-
else return 0;
16-
}
17-
});//in ascending order
18-
19-
int max = Integer.MIN_VALUE;
20-
for(int i = 0; i < nums.length; i++){
21-
max = Math.max(max, nums[i]);
22-
heap.offer(nums[i]);
23-
}
24-
25-
int moves = 0;
26-
while(!heap.isEmpty()){
27-
//always increments two elements
28-
int one = heap.poll();
29-
int two = heap.poll();
30-
one++;
31-
two++;
32-
moves++;
33-
max = Math.max(max, Math.max(one, two));
34-
heap.offer(one);
35-
heap.offer(two);
36-
if(heap.peek() == max) break;
37-
}
38-
return moves;
8+
if(nums.length == 0) return 0;
9+
int min = nums[0];
10+
for(int n : nums) min = Math.min(min, n);
11+
int res = 0;
12+
for(int n : nums) res += n - min;
13+
return res;
3914
}
4015

4116
public static void main(String...args){

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# fishercoderLeetcode
22
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
33
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
4-
|453|[Minimum Moves to Equal Array Elementss](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../blob/master/EASY/src/easy/MinimumMovestoEqualArrayElements.java)| O(n)|O(n) | Easy| BFS
5-
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../../blob/master/EASY/src/easy/NumberofBoomerangs.java)| O(n^2)|O(n) | Easy|
4+
|453|[Minimum Moves to Equal Array Elementss](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../blob/master/EASY/src/easy/MinimumMovestoEqualArrayElements.java)| O(n)|O(1) | Easy|
5+
|447|[Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/)|[Solution](../../blob/master/EASY/src/easy/NumberofBoomerangs.java)| O(n^2)|O(n) | Easy| HashMap
66
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../blob/master/EASY/src/easy/ArrangingCoins.java)| O(n)|O(1) | Easy|
77
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
88
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS

0 commit comments

Comments
 (0)