Skip to content

Commit ccdfe25

Browse files
committed
add 169 java, progress
1 parent ae39c46 commit ccdfe25

File tree

6 files changed

+173
-4
lines changed

6 files changed

+173
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@
222222
119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)| [Python](./leetcode_python/Array/pascals-triangle-ii.py) | _O(n^2)_ | _O(1)_ | Easy |array, check with `# 118 Pascal's Triangle`, `amazon`| OK** (4)
223223
121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [Python](./leetcode_python/Array/best-time-to-buy-and-sell-stock.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java) | _O(n)_ | _O(1)_ | Easy |Curated Top 75, `dp`,`basic`,`greedy`,`UBER`, `M$`, `amazon`, `fb`| OK****** (7) (but again)
224224
157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) | [Python](./leetcode_python/Array/read-n-characters-given-read4.py) | _O(n)_ | _O(1)_ | Easy |🔒, buffer, `google`, `amazon`, `fb`| AGAIN**** (3)
225-
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/)| [Python](./leetcode_python/Array/missing-ranges.py) | _O(n)_ | _O(1)_ | Medium| two pointer, 🔒, `google`, `amazon` | AGAIN******* (3)
225+
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/)| [Python](./leetcode_python/Array/missing-ranges.py),
226+
[Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MissingRanges.java) | _O(n)_ | _O(1)_ | Medium| two pointer, 🔒, `google`, `amazon` | OK******* (4)
226227
169 | [Majority Element](https://leetcode.com/problems/majority-element/) |[Python](./leetcode_python/Array/majority-element.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement.java) | _O(n)_ | _O(1)_ | Easy |`amazon`| OK*
227228
189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [Python](./leetcode_python/Array/rotate-array.py) | _O(n)_ | _O(1)_ | Medium |array, k % len(nums), good basic, `amazon`| OK* (7) (but again)
228229
209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | [Python](./leetcode_python/Array/minimum-size-subarray-sum.py) | _O(n)_ | _O(1)_ | Medium | good basic, sliding window, `Binary Search`, `trick`,`2 pointers`, `fb` | OK********** (5) (but again)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-10-20
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-19
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241020: 163
12
20241019: 298,729
23
20241018: 1146
34
20241014: 737

data/to_review.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,12 +829,12 @@
829829
2020-07-14 -> ['130', '066,271', '711,046,126,127']
830830
2020-07-13 -> ['346,686', '738', '063,064,120,0146']
831831
2020-07-12 -> ['210,261', '396', '675,297,138']
832-
2020-07-11 -> ['066,271', '163', '361,393,133,207', '482,127,102,107']
832+
2020-07-11 -> ['163', '066,271', '361,393,133,207', '482,127,102,107']
833833
2020-07-10 -> ['734,737', '388', '836,860,863']
834834
2020-07-09 -> ['066,271', '694']
835-
2020-07-08 -> ['066,271', '163', '646', '663']
835+
2020-07-08 -> ['163', '066,271', '646', '663']
836836
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
837-
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
837+
2020-07-06 -> ['163', '130', '361,393,133,207', '669,682,739,763']
838838
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
839839
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
840840
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/missing-ranges/description/
4+
// https://leetcode.ca/all/163.html
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* 163. Missing Ranges
11+
* Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
12+
* <p>
13+
* Example:
14+
* <p>
15+
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
16+
* Output: ["2", "4->49", "51->74", "76->99"]
17+
* Difficulty:
18+
* Medium
19+
* Lock:
20+
* Prime
21+
* Company:
22+
* Amazon Facebook Google Oracle
23+
* Problem Solution
24+
*/
25+
public class MissingRanges {
26+
27+
// V0
28+
// IDEA : ARRAY + BOUNDARY HANDLING (fix by gpt)
29+
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
30+
31+
List<List<Integer>> res = new ArrayList<>();
32+
33+
// Edge case: if nums is empty, return the whole range
34+
if (nums.length == 0) {
35+
addRange(res, lower, upper);
36+
return res;
37+
}
38+
39+
// Check for missing range before the first element
40+
if (nums[0] > lower) {
41+
addRange(res, lower, nums[0] - 1);
42+
}
43+
44+
// Find missing ranges between the numbers in nums
45+
for (int i = 1; i < nums.length; i++) {
46+
if (nums[i] > nums[i - 1] + 1) {
47+
addRange(res, nums[i - 1] + 1, nums[i] - 1);
48+
}
49+
}
50+
51+
// Check for missing range after the last element
52+
if (nums[nums.length - 1] < upper) {
53+
addRange(res, nums[nums.length - 1] + 1, upper);
54+
}
55+
56+
return res;
57+
}
58+
59+
// Helper method to add the range to the result list
60+
private void addRange(List<List<Integer>> res, int start, int end) {
61+
List<Integer> range = new ArrayList<>();
62+
range.add(start);
63+
if (start != end) {
64+
range.add(end);
65+
}
66+
res.add(range);
67+
}
68+
69+
70+
// V1
71+
// https://leetcode.ca/2016-05-11-163-Missing-Ranges/
72+
// public List<List<Integer>> findMissingRanges_1(int[] nums, int lower, int upper) {
73+
// int n = nums.length;
74+
// if (n == 0) {
75+
// return List.of(List.of(lower, upper));
76+
// }
77+
// List<List<Integer>> ans = new ArrayList<>();
78+
// if (nums[0] > lower) {
79+
// ans.add(List.of(lower, nums[0] - 1));
80+
// }
81+
// for (int i = 1; i < n; ++i) {
82+
// if (nums[i] - nums[i - 1] > 1) {
83+
// ans.add(List.of(nums[i - 1] + 1, nums[i] - 1));
84+
// }
85+
// }
86+
// if (nums[n - 1] < upper) {
87+
// ans.add(List.of(nums[n - 1] + 1, upper));
88+
// }
89+
// return ans;
90+
// }
91+
92+
// V2
93+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,77 @@ public boolean book(int start, int end) {
17781778
// }
17791779
// }
17801780

1781+
// LC 163
1782+
// https://leetcode.ca/all/163.html
1783+
// 3.51 pm - 4.20 pm
1784+
/**
1785+
* Given a sorted integer array nums, where the range of
1786+
* elements are in the inclusive range [lower, upper],
1787+
* return its missing ranges.
1788+
*
1789+
*
1790+
* exp 1:
1791+
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
1792+
* Output: ["2", "4->49", "51->74", "76->99"]
1793+
*
1794+
* -> idea 1
1795+
*
1796+
* 0,1, [2] 3, [50], [75]
1797+
*
1798+
* -> "2", "4,49", "51,74", "76,99"
1799+
*
1800+
*
1801+
* -> loop over elements, compare prev, and current element
1802+
* -> if any missing, then collect, then add to result array
1803+
* -> then finally check upper bound and last element
1804+
* -> add to result accordingly if there is a missing element
1805+
*/
1806+
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
1807+
if (nums.length == 0){
1808+
return null;
1809+
}
1810+
1811+
/**
1812+
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
1813+
* Output: ["2", "4->49", "51->74", "76->99"]
1814+
*
1815+
* nums = [0, 1, 3, 50, 75] res = []
1816+
* x
1817+
* x res = []
1818+
* x res = [[2]]
1819+
* x res = [[2], [4,49]]
1820+
* x res = [[2], [4,49], [51,74]]
1821+
*
1822+
* res = [[2], [4,49], [51,74], [76,99]]
1823+
*/
1824+
List<List<Integer>> res = new ArrayList<>();
1825+
for (int i = 1; i < nums.length; i++){
1826+
// case 1: nums = [1, 3]
1827+
if (nums[i] == nums[i-1]+2){
1828+
List<Integer> missingPeriod = new ArrayList<>();
1829+
missingPeriod.add(nums[i-1]+1);
1830+
res.add(missingPeriod);
1831+
}
1832+
// case 2 : nums = [3, 50]
1833+
else if (nums[i] != nums[i-1]){
1834+
List<Integer> missingPeriod = new ArrayList<>();
1835+
missingPeriod.add(nums[i-1]+1);
1836+
missingPeriod.add(nums[i]-1);
1837+
res.add(missingPeriod);
1838+
}
1839+
}
1840+
1841+
// finally, check last element and upper bound
1842+
if (upper != nums[nums.length-1]){
1843+
List<Integer> missingPeriod = new ArrayList<>();
1844+
missingPeriod.add(nums[nums.length-1] + 1);
1845+
missingPeriod.add(upper);
1846+
res.add(missingPeriod);
1847+
}
1848+
1849+
return res;
1850+
}
1851+
17811852

17821853
}
17831854

0 commit comments

Comments
 (0)