forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_908.java
33 lines (28 loc) · 817 Bytes
/
_908.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fishercoder.solutions;
import java.util.Arrays;
public class _908 {
public static class Solution1 {
public int smallestRangeI(int[] A, int K) {
Arrays.sort(A);
int smallestPlus = A[0] + K;
int biggestMinus = A[A.length - 1] - K;
int diff = biggestMinus - smallestPlus;
if (diff > 0) {
return diff;
} else {
return 0;
}
}
}
public static class Solution2 {
public int smallestRangeI(int[] A, int K) {
int min = A[0];
int max = A[0];
for (int k : A) {
min = Math.min(min, k);
max = Math.max(max, k);
}
return Math.max(max - min - 2 * K, 0);
}
}
}