Skip to content

Improve solution for 908 #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Your ideas/fixes/algorithms are more than welcome!
|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|
|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|
|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|
|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|
|908|[Smallest Range I](https://leetcode.com/problems/smallest-range-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_908.java) | O(n) | O(1) | |Easy|
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Medium|
|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
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
Expand Down
36 changes: 25 additions & 11 deletions src/main/java/com/fishercoder/solutions/_908.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,31 @@
* 0 <= K <= 10000
*/
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 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);
}
}
}
}
5 changes: 5 additions & 0 deletions src/test/java/com/fishercoder/_908Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@

public class _908Test {
private static _908.Solution1 solution1;
private static _908.Solution2 solution2;
private static int[] A;

@BeforeClass
public static void setup() {
solution1 = new _908.Solution1();
solution2 = new _908.Solution2();
}

@Test
public void test1() {
A = new int[] {1};
assertEquals(0, solution1.smallestRangeI(A, 0));
assertEquals(0, solution2.smallestRangeI(A, 0));
}

@Test
public void test2() {
A = new int[] {0, 10};
assertEquals(6, solution1.smallestRangeI(A, 2));
assertEquals(6, solution2.smallestRangeI(A, 2));
}

@Test
public void test3() {
A = new int[] {1, 3, 6};
assertEquals(0, solution1.smallestRangeI(A, 3));
assertEquals(0, solution2.smallestRangeI(A, 3));
}
}