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

Commit13dbcd3

Browse files
minimum moves to equal array elements
1 parentaa8b7cf commit13dbcd3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packageeasy;
2+
3+
importjava.util.Comparator;
4+
importjava.util.PriorityQueue;
5+
importjava.util.Queue;
6+
7+
publicclassMinimumMovestoEqualArrayElements {
8+
9+
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;
39+
}
40+
41+
publicstaticvoidmain(String...args){
42+
int[]nums =newint[]{1,2,3};
43+
System.out.println(minMoves(nums));
44+
}
45+
46+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#fishercoderLeetcode
22
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
33
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
4+
|453|[Minimum Moves to Equal Array Elementss](https://leetcode.com/contest/12/problems/minimum-moves-to-equal-array-elements/)|[Solution](../../blob/master/EASY/src/easy/MinimumMovestoEqualArrayElements.java)| O(n)|O(n) | Easy| BFS
45
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../blob/master/EASY/src/easy/ArrangingCoins.java)| O(n)|O(1)| Easy|
56
|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
67
|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