Skip to content

Commit

Permalink
add 169 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 20, 2024
1 parent ae39c46 commit ccdfe25
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@
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)
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)
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)
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)
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/)| [Python](./leetcode_python/Array/missing-ranges.py),
[Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MissingRanges.java) | _O(n)_ | _O(1)_ | Medium| two pointer, 🔒, `google`, `amazon` | OK******* (4)
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*
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)
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)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-10-20
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-10-19
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20241020: 163
20241019: 298,729
20241018: 1146
20241014: 737
Expand Down
6 changes: 3 additions & 3 deletions data/to_review.txt
Original file line number Diff line number Diff line change
Expand Up @@ -829,12 +829,12 @@
2020-07-14 -> ['130', '066,271', '711,046,126,127']
2020-07-13 -> ['346,686', '738', '063,064,120,0146']
2020-07-12 -> ['210,261', '396', '675,297,138']
2020-07-11 -> ['066,271', '163', '361,393,133,207', '482,127,102,107']
2020-07-11 -> ['163', '066,271', '361,393,133,207', '482,127,102,107']
2020-07-10 -> ['734,737', '388', '836,860,863']
2020-07-09 -> ['066,271', '694']
2020-07-08 -> ['066,271', '163', '646', '663']
2020-07-08 -> ['163', '066,271', '646', '663']
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
2020-07-06 -> ['163', '130', '361,393,133,207', '669,682,739,763']
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
Expand Down
93 changes: 93 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Array/MissingRanges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/missing-ranges/description/
// https://leetcode.ca/all/163.html

import java.util.ArrayList;
import java.util.List;

/**
* 163. Missing Ranges
* Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
* <p>
* Example:
* <p>
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
* Output: ["2", "4->49", "51->74", "76->99"]
* Difficulty:
* Medium
* Lock:
* Prime
* Company:
* Amazon Facebook Google Oracle
* Problem Solution
*/
public class MissingRanges {

// V0
// IDEA : ARRAY + BOUNDARY HANDLING (fix by gpt)
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {

List<List<Integer>> res = new ArrayList<>();

// Edge case: if nums is empty, return the whole range
if (nums.length == 0) {
addRange(res, lower, upper);
return res;
}

// Check for missing range before the first element
if (nums[0] > lower) {
addRange(res, lower, nums[0] - 1);
}

// Find missing ranges between the numbers in nums
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i - 1] + 1) {
addRange(res, nums[i - 1] + 1, nums[i] - 1);
}
}

// Check for missing range after the last element
if (nums[nums.length - 1] < upper) {
addRange(res, nums[nums.length - 1] + 1, upper);
}

return res;
}

// Helper method to add the range to the result list
private void addRange(List<List<Integer>> res, int start, int end) {
List<Integer> range = new ArrayList<>();
range.add(start);
if (start != end) {
range.add(end);
}
res.add(range);
}


// V1
// https://leetcode.ca/2016-05-11-163-Missing-Ranges/
// public List<List<Integer>> findMissingRanges_1(int[] nums, int lower, int upper) {
// int n = nums.length;
// if (n == 0) {
// return List.of(List.of(lower, upper));
// }
// List<List<Integer>> ans = new ArrayList<>();
// if (nums[0] > lower) {
// ans.add(List.of(lower, nums[0] - 1));
// }
// for (int i = 1; i < n; ++i) {
// if (nums[i] - nums[i - 1] > 1) {
// ans.add(List.of(nums[i - 1] + 1, nums[i] - 1));
// }
// }
// if (nums[n - 1] < upper) {
// ans.add(List.of(nums[n - 1] + 1, upper));
// }
// return ans;
// }

// V2
}
71 changes: 71 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,77 @@ public boolean book(int start, int end) {
// }
// }

// LC 163
// https://leetcode.ca/all/163.html
// 3.51 pm - 4.20 pm
/**
* Given a sorted integer array nums, where the range of
* elements are in the inclusive range [lower, upper],
* return its missing ranges.
*
*
* exp 1:
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
* Output: ["2", "4->49", "51->74", "76->99"]
*
* -> idea 1
*
* 0,1, [2] 3, [50], [75]
*
* -> "2", "4,49", "51,74", "76,99"
*
*
* -> loop over elements, compare prev, and current element
* -> if any missing, then collect, then add to result array
* -> then finally check upper bound and last element
* -> add to result accordingly if there is a missing element
*/
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
if (nums.length == 0){
return null;
}

/**
* Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
* Output: ["2", "4->49", "51->74", "76->99"]
*
* nums = [0, 1, 3, 50, 75] res = []
* x
* x res = []
* x res = [[2]]
* x res = [[2], [4,49]]
* x res = [[2], [4,49], [51,74]]
*
* res = [[2], [4,49], [51,74], [76,99]]
*/
List<List<Integer>> res = new ArrayList<>();
for (int i = 1; i < nums.length; i++){
// case 1: nums = [1, 3]
if (nums[i] == nums[i-1]+2){
List<Integer> missingPeriod = new ArrayList<>();
missingPeriod.add(nums[i-1]+1);
res.add(missingPeriod);
}
// case 2 : nums = [3, 50]
else if (nums[i] != nums[i-1]){
List<Integer> missingPeriod = new ArrayList<>();
missingPeriod.add(nums[i-1]+1);
missingPeriod.add(nums[i]-1);
res.add(missingPeriod);
}
}

// finally, check last element and upper bound
if (upper != nums[nums.length-1]){
List<Integer> missingPeriod = new ArrayList<>();
missingPeriod.add(nums[nums.length-1] + 1);
missingPeriod.add(upper);
res.add(missingPeriod);
}

return res;
}


}

Expand Down

0 comments on commit ccdfe25

Please sign in to comment.