Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a62a06

Browse files
committed
added task #1675 solution
1 parent a7cba97 commit 3a62a06

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/task_1675/Solution.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package task_1675;
2+
3+
import java.util.TreeSet;
4+
5+
public class Solution {
6+
7+
public int minimumDeviation(int[] nums) {
8+
TreeSet<Integer> set = new TreeSet<>();
9+
for (int i = 0; i < nums.length; i++) {
10+
if (nums[i] % 2 == 0) {
11+
set.add(nums[i]);
12+
} else {
13+
set.add(nums[i] * 2);
14+
}
15+
}
16+
int max = Integer.MIN_VALUE;
17+
int min = Integer.MAX_VALUE;
18+
int minDiff = Integer.MAX_VALUE;
19+
while (true) {
20+
max = set.last();
21+
min = set.first();
22+
minDiff = Math.min(minDiff, max - min);
23+
if (max % 2 == 0) {
24+
set.remove(max);
25+
set.add(max / 2);
26+
} else {
27+
break;
28+
}
29+
}
30+
return minDiff;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)