Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita8656bc

Browse files
correct minimum moves to equal array elements
1 parenta7ab087 commita8656bc

File tree

2 files changed

+11
-36
lines changed

2 files changed

+11
-36
lines changed

‎EASY/src/easy/MinimumMovestoEqualArrayElements.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
11
packageeasy;
22

3-
importjava.util.Comparator;
4-
importjava.util.PriorityQueue;
5-
importjava.util.Queue;
6-
73
publicclassMinimumMovestoEqualArrayElements {
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
publicstaticintminMoves(int[]nums) {
10-
Queue<Integer>heap =newPriorityQueue<Integer>(newComparator<Integer>(){
11-
@Override
12-
publicintcompare(Integero1,Integero2) {
13-
if(o1 >o2)return1;
14-
elseif(o1 <o2)return -1;
15-
elsereturn0;
16-
}
17-
});//in ascending order
18-
19-
intmax =Integer.MIN_VALUE;
20-
for(inti =0;i <nums.length;i++){
21-
max =Math.max(max,nums[i]);
22-
heap.offer(nums[i]);
23-
}
24-
25-
intmoves =0;
26-
while(!heap.isEmpty()){
27-
//always increments two elements
28-
intone =heap.poll();
29-
inttwo =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-
returnmoves;
8+
if(nums.length ==0)return0;
9+
intmin =nums[0];
10+
for(intn :nums)min =Math.min(min,n);
11+
intres =0;
12+
for(intn :nums)res +=n -min;
13+
returnres;
3914
}
4015

4116
publicstaticvoidmain(String...args){

‎README.md

Lines changed: 2 additions & 2 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp