Skip to content

Commit

Permalink
add 81 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 26, 2024
1 parent a526911 commit 678e462
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@
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)
074| [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Python](./leetcode_python/Binary_Search/search-a-2d-matrix.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchA2DMatrix.java) | _O(logm + logn)_ | _O(1)_ | Medium |binary search in 2D array, flatten matrix,`amazon`| OK**** (BUT AGAIN)(3)
081| [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) | [Python](./leetcode_python/Binary_Search/search-in-rotated-sorted-array-ii.py) | _O(logn)_ | _O(1)_ | Medium |`binary search`,check `# 33 Search in Rotated Sorted Array` first,`array`, `fb`, amazon, linkedin| OK***** (6)
081| [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) | [Python](./leetcode_python/Binary_Search/search-in-rotated-sorted-array-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray2.java) | _O(logn)_ | _O(1)_ | Medium |`binary search`,check `# 33 Search in Rotated Sorted Array` first,`array`, `fb`, amazon, linkedin| OK***** (6)
153| [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)| [Python](./leetcode_python/Binary_Search/find-minimum-in-rotated-sorted-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindMinimumInRotatedSortedArray.java)| _O(logn)_ | _O(1)_ | Medium |Curated Top 75, binary search, ascending array, good basic,`amazon`| AGAIN********** (5)(MUST)
162| [Find Peak Element](https://leetcode.com/problems/find-peak-element/) | [Python](./leetcode_python/Binary_Search/find-peak-element.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindPeakElement.java) | _O(logn)_ | _O(1)_ | Medium |good trick, recursive, iterative binary search,`M$`, `google`, `fb`| AGAIN********** (6)
222| [Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/) | [Python](./leetcode_python/Binary_Search/count-complete-tree-nodes.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/CountCompleteTreeNodes.java) | _O((logn)^2)_ | _O(1)_| Easy|google, tree| OK (again)
Expand Down
4 changes: 2 additions & 2 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
20241125: 33
20241125: 33,81
20241124: 153
20241123: 253
20241122: 776,31
20241121: 004(todo),81(todo),34(todo),162(todo),275(todo)
20241121: 004(todo),34(todo),162(todo),275(todo)
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
20241117: 394
20241116: 833,950
Expand Down
31 changes: 17 additions & 14 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
2025-01-19 -> ['33,81']
2025-01-17 -> ['253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['394']
2025-01-10 -> ['833,950']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-29 -> ['33,81']
2024-12-28 -> ['900']
2024-12-27 -> ['253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
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-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-16 -> ['33,81']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['776,31', '444', '298,729']
2024-12-12 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
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-08 -> ['394', '737']
2024-12-08 -> ['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-04 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-04 -> ['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 -> ['253', '53,210,207']
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-27 -> ['776,31', '951,792', '524,221,889']
2024-11-26 -> ['253', '004(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-27 -> ['33,81', '776,31', '951,792', '524,221,889']
2024-11-26 -> ['33,81', '253', '004(todo),34(todo),162(todo),275(todo)', '743,889']
2024-11-25 -> ['253', '776,31', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
2024-11-24 -> ['253', '776,31', '004(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
2024-11-23 -> ['776,31', '004(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
2024-11-22 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
2024-11-24 -> ['253', '776,31', '004(todo),34(todo),162(todo),275(todo)', '833,950', '900']
2024-11-23 -> ['776,31', '004(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
2024-11-22 -> ['004(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
2024-11-21 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '833,950', '1188,130,855(again)', '1146']
2024-11-20 -> ['394', '939']
2024-11-19 -> ['394', '833,950']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package LeetCodeJava.BinarySearch;

// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 81. Search in Rotated Sorted Array II
* Solved
* Medium
* Topics
* Companies
* There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).
* <p>
* Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].
* <p>
* Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums.
* <p>
* You must decrease the overall operation steps as much as possible.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* Input: nums = [2,5,6,0,0,1,2], target = 0
* Output: true
* Example 2:
* <p>
* Input: nums = [2,5,6,0,0,1,2], target = 3
* Output: false
* <p>
* <p>
* Constraints:
* <p>
* 1 <= nums.length <= 5000
* -104 <= nums[i] <= 104
* nums is guaranteed to be rotated at some pivot.
* -104 <= target <= 104
* <p>
* <p>
* Follow up: This problem is similar to Search in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why?
*/
public class SearchInRotatedSortedArray2 {

// V0
// IDEA : BINARY SEARCH + SET
// LC 33
public boolean search(int[] nums, int target) {

if (nums.length == 1) {
return nums[0] == target;
}
Set<Integer> set = new HashSet<>();
List<Integer> nonDuplicatedNums = new ArrayList<>();

for (int x : nums) {
if (!set.contains(x)) {
set.add(x);
nonDuplicatedNums.add(x);
}
}

// binary search
int left = 0;
int right = nonDuplicatedNums.size() - 1;
while (right >= left) {
int mid = (left + right) / 2;
if (nonDuplicatedNums.get(mid) == target) {
return true;
}
// if right sub array is ascending
if (nonDuplicatedNums.get(mid) < nonDuplicatedNums.get(right)) {
// if mid < target <= right
if (target > nonDuplicatedNums.get(mid) && target <= nonDuplicatedNums.get(right)) {
left = mid + 1;
}
// else
else {
right = mid - 1;
}
}
// if left sub array is ascending
else {
// if mid > target > left
if (target >= nonDuplicatedNums.get(left) && target < nonDuplicatedNums.get(mid)) {
right = mid - 1;
}
// else
else {
left = mid + 1;
}

}

}

return false;
}

// V1
// IDEA : BINARY SEARCH
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/solutions/3888242/100-binary-search-video-with-rotation-handling-optimal/
public boolean search_1(int[] nums, int target) {
int low = 0, high = nums.length - 1;

while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] == target) return true;

if (nums[low] == nums[mid]) {
low++;
continue;
}

if (nums[low] <= nums[mid]) {
if (nums[low] <= target && target <= nums[mid]) high = mid - 1;
else low = mid + 1;
} else {
if (nums[mid] <= target && target <= nums[high]) low = mid + 1;
else high = mid - 1;
}
}
return false;
}

// V2
// IDEA : BINARY SEARCH
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/solutions/2689441/java-best-solution-for-understanding/
public boolean search_2(int[] nums, int target) {
int start = 0, end = nums.length - 1;
while(start <= end) {
int mid = start + (end - start) / 2;
if(nums[mid] == target) return true;

//if there are duplicates
if(nums[start] == nums[mid] && nums[mid] == nums[end]) {
start ++;
end --;
}

//left half is sorted
else if(nums[start] <= nums[mid]) {
if(target >= nums[start] && target <= nums[mid])
end = mid - 1;

else
start = mid + 1;
}

//right half is sorted
else {
if(target <= nums[end] && target >= nums[mid])
start = mid + 1;

else
end = mid - 1;
}
}
return false;
}
}
100 changes: 99 additions & 1 deletion leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3794,7 +3794,7 @@ public int findMin(int[] nums){

// LC 33
// 7.42 pm - 8.10 pm
public int search(int[] nums, int target) {
public int search_(int[] nums, int target) {
if (nums.length == 1){
return nums[0] == target ? 0 : -1;
}
Expand Down Expand Up @@ -3835,6 +3835,104 @@ public int search(int[] nums, int target) {
return -1;
}

// public int search(int[] nums, int target) {
//
// if (nums.length == 1){
// return nums[0] == target ? 0 : -1;
// }
//
// // binary search
// int left = 0;
// int right = nums.length-1;
// while (right >= left){
// int mid = (left + right) / 2;
// if (nums[mid] == target){
// return mid;
// }
// // if right sub array is ascending
// if (nums[mid] < nums[right]){
// // if mid < target <= right
// if (target > nums[mid] && target <= nums[right]){
// left = mid + 1;
// }
// // else
// else{
// right = mid - 1;
// }
// }
// // if left sub array is ascending
// else{
// // if mid > target > left
// if (target >= nums[left] && target < nums[mid]){
// right = mid - 1;
// }
// // else
// else{
// left = mid + 1;
// }
//
// }
//
// }
//
// return -1;
// }

// LC 81
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
// 8.07 pm - 8.20 pm
public boolean search(int[] nums, int target) {

if (nums.length == 1){
return nums[0] == target;
}
Set<Integer> set = new HashSet<>();
List<Integer> nonDuplicatedNums = new ArrayList<>();

for (int x : nums){
if (!set.contains(x)){
set.add(x);
nonDuplicatedNums.add(x);
}
}

// binary search
int left = 0;
int right = nonDuplicatedNums.size() - 1;
while (right >= left){
int mid = (left + right) / 2;
if (nonDuplicatedNums.get(mid) == target){
return true;
}
// if right sub array is ascending
if (nonDuplicatedNums.get(mid) < nonDuplicatedNums.get(right)){
// if mid < target <= right
if (target > nonDuplicatedNums.get(mid) && target <= nonDuplicatedNums.get(right)){
left = mid + 1;
}
// else
else{
right = mid - 1;
}
}
// if left sub array is ascending
else{
// if mid > target > left
if (target >= nonDuplicatedNums.get(left) && target < nonDuplicatedNums.get(mid)){
right = mid - 1;
}
// else
else{
left = mid + 1;
}

}

}

return false;
}

}


0 comments on commit 678e462

Please sign in to comment.