Skip to content

Commit

Permalink
add 34 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 30, 2024
1 parent 7947894 commit 4085845
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@
| # | Title | Solution | Time | Space | Difficulty | Tag, Note | Status|
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
033| [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./leetcode_python/Binary_Search/search-in-rotated-sorted-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java) ||| Medium |Curated Top 75, `binary search`,`good basic` ,check `# 81 Search in Rotated Sorted Array II`, binary search, rotation array, `UBER`,`amazon`,`fb`| AGAIN************ (6) (MUST)
034 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Python](./leetcode_python/Binary_Search/find-first-and-last-position-of-element-in-sorted-array.py)| _O(n)_ | _O(1)_ | Medium | binary search, LC top 100 like, `amazon`, `fb`, google, apple, uber | AGAIN************** (4) (MUST)
034 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Python](./leetcode_python/Binary_Search/find-first-and-last-position-of-element-in-sorted-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java)| _O(n)_ | _O(1)_ | Medium | binary search, LC top 100 like, `amazon`, `fb`, google, apple, uber | AGAIN************** (4) (MUST)
034| [Search for a Range](https://leetcode.com/problems/search-for-a-range/) |[Python](./leetcode_python/Binary_Search/search-for-a-range.py) | _O(logn)_ | _O(1)_ | Medium || AGAIN*
035| [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Python](./leetcode_python/Binary_Search/search-insert-position.py) | _O(logn)_ | _O(1)_ | Medium || AGAIN
069| [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Python](./leetcode_python/Binary_Search/sqrtx.py) | _O(logn)_ | _O(1)_ | Medium |math, binary search,`amazon`,`fb`| OK* (4)
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-11-30
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-11-26
- 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 @@
20241130: 34
20241126: 722,380
20241125: 33,81
20241124: 153
Expand Down
16 changes: 9 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2025-01-24 -> ['34']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-17 -> ['253']
Expand All @@ -7,7 +8,7 @@
2025-01-11 -> ['394']
2025-01-10 -> ['833,950']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-03 -> ['34', '444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['33,81']
Expand All @@ -16,23 +17,24 @@
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-21 -> ['394', '855,846']
2024-12-21 -> ['34', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-17 -> ['722,380']
2024-12-16 -> ['33,81']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['776,31', '444', '298,729']
2024-12-13 -> ['34', '776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-09 -> ['722,380']
2024-12-08 -> ['33,81', '394', '737']
2024-12-08 -> ['34', '33,81', '394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2024-12-05 -> ['776,31', '528,334']
2024-12-05 -> ['34', '776,31', '528,334']
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-01 -> ['722,380', '253', '53,210,207']
2024-12-03 -> ['34', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-02 -> ['34']
2024-12-01 -> ['34', '722,380', '253', '53,210,207']
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['722,380', '33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package LeetCodeJava.BinarySearch;

// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
/**
* 34. Find First and Last Position of Element in Sorted Array
* Solved
* Medium
* Topics
* Companies
* Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
*
* If target is not found in the array, return [-1, -1].
*
* You must write an algorithm with O(log n) runtime complexity.
*
*
*
* Example 1:
*
* Input: nums = [5,7,7,8,8,10], target = 8
* Output: [3,4]
* Example 2:
*
* Input: nums = [5,7,7,8,8,10], target = 6
* Output: [-1,-1]
* Example 3:
*
* Input: nums = [], target = 0
* Output: [-1,-1]
*
*
* Constraints:
*
* 0 <= nums.length <= 105
* -109 <= nums[i] <= 109
* nums is a non-decreasing array.
* -109 <= target <= 109
*
*/
public class FindFirstAndLastPositionOfElementInSortedArray {

// V0
// TODO : implement
// public int[] searchRange(int[] nums, int target) {
//
// }

// V1
// IDEA : BINARY SEARCH
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/14734/easy-java-o-logn-solution/
public int[] searchRange_1(int[] nums, int target) {
int[] result = new int[2];
result[0] = findFirst(nums, target);
result[1] = findLast(nums, target);
return result;
}

private int findFirst(int[] nums, int target) {
int idx = -1;
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (nums[mid] >= target) {
end = mid - 1;
} else {
start = mid + 1;
}
if (nums[mid] == target)
idx = mid;
}
return idx;
}

private int findLast(int[] nums, int target) {
int idx = -1;
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (nums[mid] <= target) {
start = mid + 1;
} else {
end = mid - 1;
}
if (nums[mid] == target)
idx = mid;
}
return idx;
}

// V2
// IDEA : Binary Tree
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/14701/a-very-simple-java-solution-with-only-one-binary-search-algorithm/
public int[] searchRange_2(int[] A, int target) {
int start = firstGreaterEqual(A, target);
if (start == A.length || A[start] != target) {
return new int[]{-1, -1};
}
return new int[]{start, firstGreaterEqual(A, target + 1) - 1};
}

//find the first number that is greater than or equal to target.
//could return A.length if target is greater than A[A.length-1].
//actually this is the same as lower_bound in C++ STL.
private static int firstGreaterEqual(int[] A, int target) {
int low = 0, high = A.length;
while (low < high) {
int mid = low + ((high - low) >> 1);
//low <= mid < high
if (A[mid] < target) {
low = mid + 1;
} else {
//should not be mid-1 when A[mid]==target.
//could be mid even if A[mid]>target because mid<high.
high = mid;
}
}
return low;
}

// V3
// IDEA : binary Search
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/5378191/video-binary-search-solution/
public int[] searchRange_3(int[] nums, int target) {
int[] result = { -1, -1 };
int left = binarySearch(nums, target, true);
int right = binarySearch(nums, target, false);
result[0] = left;
result[1] = right;
return result;
}

private int binarySearch(int[] nums, int target, boolean isSearchingLeft) {
int left = 0;
int right = nums.length - 1;
int idx = -1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
idx = mid;
if (isSearchingLeft) {
right = mid - 1;
} else {
left = mid + 1;
}
}
}

return idx;
}
}
83 changes: 81 additions & 2 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3537,6 +3537,7 @@ private String getMultiplyStr(String cur, Integer multiply) {
// IDEA : DFS
TreeNode leftTree = new TreeNode();
TreeNode rightTree = new TreeNode();

public TreeNode[] splitBST(TreeNode root, int target) {
TreeNode[] res = new TreeNode[2];

Expand All @@ -3563,7 +3564,7 @@ private TreeNode[] buildTreeDfs(TreeNode root, int target){
leftTree.left = root.left;
buildTreeDfs(leftTree, target);
}
return null;
return null;
}

// public TreeNode[] splitBST(TreeNode root, int target) {
Expand Down Expand Up @@ -3612,6 +3613,7 @@ private TreeNode buildTree(List<Integer> input){
// LC 31
// https://leetcode.com/problems/next-permutation/description/
// 5.39 pm - 6.15 pm

/**
*
* exp 1) [1,2,3] -> [1,3,2]
Expand Down Expand Up @@ -3666,6 +3668,7 @@ public void nextPermutation(int[] nums) {
// LC 253
// https://leetcode.ca/all/253.html
// 4.57 pm - 5.10 pm

/**
* [[s1,e1],[s2,e2],...]
*
Expand Down Expand Up @@ -3711,6 +3714,7 @@ public String getStatus() {
return status;
}
}

public int minMeetingRooms(int[][] intervals) {
if (intervals.length <= 1){
return intervals.length;
Expand Down Expand Up @@ -3759,6 +3763,7 @@ public int minMeetingRooms(int[][] intervals) {

// LC 153
// 5.07 PM - 5.20 PM

/**
*
* Input : [4,5,6,7,0,1,2]
Expand Down Expand Up @@ -3814,6 +3819,7 @@ public int findMin(int[] nums){

return -1;
}

/**
*
* [4,5,6,7,0,1,2]
Expand Down Expand Up @@ -4010,7 +4016,7 @@ public boolean search(int[] nums, int target) {
}
// else
else{
left = mid + 1;
left = mid + 1;
}

}
Expand All @@ -4023,6 +4029,7 @@ public boolean search(int[] nums, int target) {
// LC 722
// https://leetcode.com/problems/remove-comments/
// 7.41 pm - 8.15 pm

/**
* Idea 1) string op
* a tmp array that collect "Not comment" element,
Expand Down Expand Up @@ -4104,6 +4111,78 @@ public int getRandom() {
}
}

// LC 34
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
// 3.10 - 3.20 pm

/**
* Idea 1) : // binary search
* <p>
* -> find mid == target
* -> then check start, end idx
*/

public int[] searchRange(int[] nums, int target) {

if (nums.length == 0) {
return new int[]{-1, -1};
}

if (nums.length == 1) {
if (nums[0] == target) {
return new int[]{0, 0};
}
return new int[]{-1, -1};
}

// binary search
int left = 0;
int right = nums.length - 1;
int mid = -1;
while (right >= left) {
mid = (left + right) / 2;
if (nums[mid] == target) {
break;
}
if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

if (nums[mid] != target) {
//mid = -1;
return new int[]{-1, -1};
}
System.out.println(">>> mid = " + mid);

// check start, end idx
int[] res = new int[2];
int start = mid;
int end = mid;
while (start >= 0 && nums[start] == target) {
start -= 1;
}

while (end < nums.length - 1 && nums[end] == target) {
end += 1;
}

// todo : optimize
if(nums[start] != target){
start += 1;
}
if (nums[end] != target){
end -= 1;
}

res[0] = start;
res[1] = end;

return res;
}

}


0 comments on commit 4085845

Please sign in to comment.