Skip to content

Commit c0b239e

Browse files
add 908
1 parent 0bd5f12 commit c0b239e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Your ideas/fixes/algorithms are more than welcome!
3939
|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | O(n) | O(1) | |Easy|
4040
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
4141
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | O(n) | O(n) | |Easy|
42+
|908|[Smallest Range I](https://leetcode.com/problems/smallest-range-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_908.java) | O(nlogn) | O(1) | |Easy|
4243
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
4344
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | O(n) | O(n) | |Easy| DFS, recursion
4445
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 908. Smallest Range I
7+
*
8+
* Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i].
9+
*
10+
* After this process, we have some array B.
11+
*
12+
* Return the smallest possible difference between the maximum value of B and the minimum value of B.
13+
*
14+
*
15+
*
16+
* Example 1:
17+
*
18+
* Input: A = [1], K = 0
19+
* Output: 0
20+
* Explanation: B = [1]
21+
* Example 2:
22+
*
23+
* Input: A = [0,10], K = 2
24+
* Output: 6
25+
* Explanation: B = [2,8]
26+
* Example 3:
27+
*
28+
* Input: A = [1,3,6], K = 3
29+
* Output: 0
30+
* Explanation: B = [3,3,3] or B = [4,4,4]
31+
*
32+
*
33+
* Note:
34+
*
35+
* 1 <= A.length <= 10000
36+
* 0 <= A[i] <= 10000
37+
* 0 <= K <= 10000
38+
*/
39+
public class _908 {
40+
public static class Solution1 {
41+
public int smallestRangeI(int[] A, int K) {
42+
Arrays.sort(A);
43+
int smallestPlus = A[0] + K;
44+
int biggestMinus = A[A.length - 1] - K;
45+
int diff = biggestMinus - smallestPlus;
46+
if (diff > 0) {
47+
return diff;
48+
} else {
49+
return 0;
50+
}
51+
}
52+
}
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._908;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _908Test {
10+
private static _908.Solution1 solution1;
11+
private static int[] A;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _908.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
A = new int[] {1};
21+
assertEquals(0, solution1.smallestRangeI(A, 0));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
A = new int[] {0, 10};
27+
assertEquals(6, solution1.smallestRangeI(A, 2));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
A = new int[] {1, 3, 6};
33+
assertEquals(0, solution1.smallestRangeI(A, 3));
34+
}
35+
}

0 commit comments

Comments
 (0)