File tree 2 files changed +11
-36
lines changed
2 files changed +11
-36
lines changed Original file line number Diff line number Diff line change 1
1
package easy ;
2
2
3
- import java .util .Comparator ;
4
- import java .util .PriorityQueue ;
5
- import java .util .Queue ;
6
-
7
3
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.*/
9
7
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 ;
39
14
}
40
15
41
16
public static void main (String ...args ){
Original file line number Diff line number Diff line change 1
1
# fishercoderLeetcode
2
2
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
3
3
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
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
6
6
| 441| [ Arranging Coins] ( https://leetcode.com/problems/arrange-coins/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/ArrangingCoins.java ) | O(n)| O(1) | Easy|
7
7
|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
8
8
|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
You can’t perform that action at this time.
0 commit comments