Skip to content

Commit 9d028fe

Browse files
add 1228
1 parent afbd26d commit 9d028fe

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ _If you like this project, please leave me a star._ ★
1919
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | | | |Easy||
2020
|1243|[Array Transformation](https://leetcode.com/problems/array-transformation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1243.java) | | | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs)|Easy||
2121
|1232|[Check If It Is a Straight Line](https://leetcode.com/problems/check-if-it-is-a-straight-line/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1232.java) | | | |Easy||
22+
|1228|[Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1228.java) | | | |Easy||
2223
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java) | | | |Easy||
2324
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java) | | | [:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
2425
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy||
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
/**
10+
* 1228. Missing Number In Arithmetic Progression
11+
*
12+
* In some array arr, the values were in arithmetic progression: the values arr[i+1] - arr[i] are all equal for every 0 <= i < arr.length - 1.
13+
* Then, a value from arr was removed that was not the first or last value in the array.
14+
* Return the removed value.
15+
*
16+
* Example 1:
17+
* Input: arr = [5,7,11,13]
18+
* Output: 9
19+
* Explanation: The previous array was [5,7,9,11,13].
20+
*
21+
* Example 2:
22+
* Input: arr = [15,13,12]
23+
* Output: 14
24+
* Explanation: The previous array was [15,14,13,12].
25+
*
26+
* Constraints:
27+
* 3 <= arr.length <= 1000
28+
* 0 <= arr[i] <= 10^5
29+
* */
30+
public class _1228 {
31+
public static class Solution1 {
32+
/**A super verbose and inefficient but working way...*/
33+
public int missingNumber(int[] arr) {
34+
Arrays.sort(arr);
35+
Map<Integer, List<Integer>> map = new HashMap<>();
36+
for (int i = 0; i < arr.length - 1; i++) {
37+
int diff = arr[i + 1] - arr[i];
38+
List<Integer> list = map.getOrDefault(diff, new ArrayList<>());
39+
list.add(i);
40+
map.put(diff, list);
41+
}
42+
int smallDiff = arr[arr.length - 1];
43+
int bigDiff = 0;
44+
for (int key : map.keySet()) {
45+
smallDiff = Math.min(smallDiff, key);
46+
bigDiff = Math.max(bigDiff, key);
47+
}
48+
return arr[map.get(bigDiff).get(0)] + smallDiff;
49+
}
50+
}
51+
52+
public static class Solution2 {
53+
/**credit: https://leetcode.com/problems/missing-number-in-arithmetic-progression/discuss/408474/JavaC%2B%2BPython-Arithmetic-Sum-and-Binary-Search*/
54+
public int missingNumber(int[] arr) {
55+
int min = arr[0];
56+
int max = arr[0];
57+
int sum = 0;
58+
for (int num : arr) {
59+
max = Math.max(max, num);
60+
min = Math.min(min, num);
61+
sum += num;
62+
}
63+
return (max + min) * (arr.length + 1) / 2 - sum;
64+
}
65+
}
66+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1228;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1228Test {
10+
private static _1228.Solution1 solution1;
11+
private static _1228.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1228.Solution1();
16+
solution2 = new _1228.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(9, solution1.missingNumber(new int[]{5, 7, 11, 13}));
22+
assertEquals(9, solution2.missingNumber(new int[]{5, 7, 11, 13}));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(14, solution1.missingNumber(new int[]{15, 13, 12}));
28+
assertEquals(14, solution2.missingNumber(new int[]{15, 13, 12}));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)