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

Commit a71680e

Browse files
committed
Time: 692 ms (18.87%), Space: 50.2 MB (73.58%) - LeetHub
1 parent c18aa1c commit a71680e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int minimumDeviation(int[] nums) {
3+
PriorityQueue<Integer> evens = new PriorityQueue<>(Collections.reverseOrder());
4+
int minimum = Integer.MAX_VALUE;
5+
for (int num : nums) {
6+
if (num % 2 == 0) {
7+
evens.offer(num);
8+
minimum = Math.min(minimum, num);
9+
} else {
10+
evens.offer(num * 2);
11+
minimum = Math.min(minimum, num * 2);
12+
}
13+
}
14+
int minDeviation = Integer.MAX_VALUE;
15+
16+
while (!evens.isEmpty()) {
17+
int currentValue = evens.poll();
18+
minDeviation = Math.min(minDeviation, currentValue - minimum);
19+
if (currentValue % 2 == 0) {
20+
evens.offer(currentValue / 2);
21+
minimum = Math.min(minimum, currentValue / 2);
22+
} else {
23+
// if the maximum is odd, break and return
24+
break;
25+
}
26+
}
27+
return minDeviation;
28+
}
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /