File tree Expand file tree Collapse file tree 3 files changed +31
-12
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 3 files changed +31
-12
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ Your ideas/fixes/algorithms are more than welcome!
61
61
| 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|
62
62
| 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|
63
63
| 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|
65
65
| 900| [ RLE Iterator] ( https://leetcode.com/problems/rle-iterator/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_900.java ) | O(n) | O(1) | | Medium|
66
66
|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
67
67
| 896| [ Monotonic Array] ( https://leetcode.com/problems/monotonic-array/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_896.java ) | O(n) | O(1) | | Easy|
Original file line number Diff line number Diff line change 37
37
* 0 <= K <= 10000
38
38
*/
39
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 ;
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 );
50
65
}
51
- }
52
66
}
53
67
}
Original file line number Diff line number Diff line change 8
8
9
9
public class _908Test {
10
10
private static _908 .Solution1 solution1 ;
11
+ private static _908 .Solution2 solution2 ;
11
12
private static int [] A ;
12
13
13
14
@ BeforeClass
14
15
public static void setup () {
15
16
solution1 = new _908 .Solution1 ();
17
+ solution2 = new _908 .Solution2 ();
16
18
}
17
19
18
20
@ Test
19
21
public void test1 () {
20
22
A = new int [] {1 };
21
23
assertEquals (0 , solution1 .smallestRangeI (A , 0 ));
24
+ assertEquals (0 , solution2 .smallestRangeI (A , 0 ));
22
25
}
23
26
24
27
@ Test
25
28
public void test2 () {
26
29
A = new int [] {0 , 10 };
27
30
assertEquals (6 , solution1 .smallestRangeI (A , 2 ));
31
+ assertEquals (6 , solution2 .smallestRangeI (A , 2 ));
28
32
}
29
33
30
34
@ Test
31
35
public void test3 () {
32
36
A = new int [] {1 , 3 , 6 };
33
37
assertEquals (0 , solution1 .smallestRangeI (A , 3 ));
38
+ assertEquals (0 , solution2 .smallestRangeI (A , 3 ));
34
39
}
35
40
}
You can’t perform that action at this time.
0 commit comments