Skip to content

Commit e3ee9a1

Browse files
havanagrawalfishercoder1534
authored andcommitted
Improve solution for 908 (#48)
1 parent 4b84d48 commit e3ee9a1

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Your ideas/fixes/algorithms are more than welcome!
6161
|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|
6262
|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|
6363
|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|
64-
|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|
64+
|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|
6565
|900|[RLE Iterator](https://leetcode.com/problems/rle-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Medium|
6666
|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
6767
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|

src/main/java/com/fishercoder/solutions/_908.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,31 @@
3737
* 0 <= K <= 10000
3838
*/
3939
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;
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+
54+
public static class Solution2 {
55+
public int smallestRangeI(int[] A, int K) {
56+
int min = A[0];
57+
int max = A[0];
58+
59+
for (int k : A) {
60+
min = Math.min(min, k);
61+
max = Math.max(max, k);
62+
}
63+
64+
return Math.max(max - min - 2*K, 0);
5065
}
51-
}
5266
}
5367
}

src/test/java/com/fishercoder/_908Test.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,33 @@
88

99
public class _908Test {
1010
private static _908.Solution1 solution1;
11+
private static _908.Solution2 solution2;
1112
private static int[] A;
1213

1314
@BeforeClass
1415
public static void setup() {
1516
solution1 = new _908.Solution1();
17+
solution2 = new _908.Solution2();
1618
}
1719

1820
@Test
1921
public void test1() {
2022
A = new int[] {1};
2123
assertEquals(0, solution1.smallestRangeI(A, 0));
24+
assertEquals(0, solution2.smallestRangeI(A, 0));
2225
}
2326

2427
@Test
2528
public void test2() {
2629
A = new int[] {0, 10};
2730
assertEquals(6, solution1.smallestRangeI(A, 2));
31+
assertEquals(6, solution2.smallestRangeI(A, 2));
2832
}
2933

3034
@Test
3135
public void test3() {
3236
A = new int[] {1, 3, 6};
3337
assertEquals(0, solution1.smallestRangeI(A, 3));
38+
assertEquals(0, solution2.smallestRangeI(A, 3));
3439
}
3540
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy