Skip to content

Commit 678e462

Browse files
committed
add 81 java
1 parent a526911 commit 678e462

File tree

5 files changed

+283
-18
lines changed

5 files changed

+283
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@
860860
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
861861
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)
862862
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)
863-
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)
863+
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)
864864
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)
865865
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)
866866
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)

data/progress.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
20241125: 33
1+
20241125: 33,81
22
20241124: 153
33
20241123: 253
44
20241122: 776,31
5-
20241121: 004(todo),81(todo),34(todo),162(todo),275(todo)
5+
20241121: 004(todo),34(todo),162(todo),275(todo)
66
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
77
20241117: 394
88
20241116: 833,950

data/to_review.txt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
1+
2025-01-19 -> ['33,81']
12
2025-01-17 -> ['253']
23
2025-01-16 -> ['776,31']
3-
2025-01-15 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
4+
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
45
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
56
2025-01-11 -> ['394']
67
2025-01-10 -> ['833,950']
78
2025-01-04 -> ['53,210,207']
89
2025-01-03 -> ['444']
910
2025-01-02 -> ['1188,130,855(again)']
11+
2024-12-29 -> ['33,81']
1012
2024-12-28 -> ['900']
1113
2024-12-27 -> ['253', '26,27', '802,1197,26']
1214
2024-12-26 -> ['776,31']
13-
2024-12-25 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
15+
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
1416
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1517
2024-12-21 -> ['394', '855,846']
1618
2024-12-20 -> ['833,950', '932']
1719
2024-12-18 -> ['951,792']
20+
2024-12-16 -> ['33,81']
1821
2024-12-14 -> ['253', '53,210,207', '163,1048']
1922
2024-12-13 -> ['776,31', '444', '298,729']
20-
2024-12-12 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
23+
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2124
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
22-
2024-12-08 -> ['394', '737']
25+
2024-12-08 -> ['33,81', '394', '737']
2326
2024-12-07 -> ['833,950', '900', '686,734,737']
2427
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2528
2024-12-05 -> ['776,31', '528,334']
26-
2024-12-04 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
27-
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
29+
2024-12-04 -> ['004(todo),34(todo),162(todo),275(todo)']
30+
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']
2831
2024-12-01 -> ['253', '53,210,207']
29-
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
30-
2024-11-29 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
31-
2024-11-28 -> ['253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
32-
2024-11-27 -> ['776,31', '951,792', '524,221,889']
33-
2024-11-26 -> ['253', '004(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
32+
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
33+
2024-11-29 -> ['004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
34+
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)']
35+
2024-11-27 -> ['33,81', '776,31', '951,792', '524,221,889']
36+
2024-11-26 -> ['33,81', '253', '004(todo),34(todo),162(todo),275(todo)', '743,889']
3437
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']
35-
2024-11-24 -> ['253', '776,31', '004(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
36-
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']
37-
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']
38+
2024-11-24 -> ['253', '776,31', '004(todo),34(todo),162(todo),275(todo)', '833,950', '900']
39+
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']
40+
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']
3841
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']
3942
2024-11-20 -> ['394', '939']
4043
2024-11-19 -> ['394', '833,950']
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package LeetCodeJava.BinarySearch;
2+
3+
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
10+
/**
11+
* 81. Search in Rotated Sorted Array II
12+
* Solved
13+
* Medium
14+
* Topics
15+
* Companies
16+
* There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).
17+
* <p>
18+
* 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].
19+
* <p>
20+
* 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.
21+
* <p>
22+
* You must decrease the overall operation steps as much as possible.
23+
* <p>
24+
* <p>
25+
* <p>
26+
* Example 1:
27+
* <p>
28+
* Input: nums = [2,5,6,0,0,1,2], target = 0
29+
* Output: true
30+
* Example 2:
31+
* <p>
32+
* Input: nums = [2,5,6,0,0,1,2], target = 3
33+
* Output: false
34+
* <p>
35+
* <p>
36+
* Constraints:
37+
* <p>
38+
* 1 <= nums.length <= 5000
39+
* -104 <= nums[i] <= 104
40+
* nums is guaranteed to be rotated at some pivot.
41+
* -104 <= target <= 104
42+
* <p>
43+
* <p>
44+
* 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?
45+
*/
46+
public class SearchInRotatedSortedArray2 {
47+
48+
// V0
49+
// IDEA : BINARY SEARCH + SET
50+
// LC 33
51+
public boolean search(int[] nums, int target) {
52+
53+
if (nums.length == 1) {
54+
return nums[0] == target;
55+
}
56+
Set<Integer> set = new HashSet<>();
57+
List<Integer> nonDuplicatedNums = new ArrayList<>();
58+
59+
for (int x : nums) {
60+
if (!set.contains(x)) {
61+
set.add(x);
62+
nonDuplicatedNums.add(x);
63+
}
64+
}
65+
66+
// binary search
67+
int left = 0;
68+
int right = nonDuplicatedNums.size() - 1;
69+
while (right >= left) {
70+
int mid = (left + right) / 2;
71+
if (nonDuplicatedNums.get(mid) == target) {
72+
return true;
73+
}
74+
// if right sub array is ascending
75+
if (nonDuplicatedNums.get(mid) < nonDuplicatedNums.get(right)) {
76+
// if mid < target <= right
77+
if (target > nonDuplicatedNums.get(mid) && target <= nonDuplicatedNums.get(right)) {
78+
left = mid + 1;
79+
}
80+
// else
81+
else {
82+
right = mid - 1;
83+
}
84+
}
85+
// if left sub array is ascending
86+
else {
87+
// if mid > target > left
88+
if (target >= nonDuplicatedNums.get(left) && target < nonDuplicatedNums.get(mid)) {
89+
right = mid - 1;
90+
}
91+
// else
92+
else {
93+
left = mid + 1;
94+
}
95+
96+
}
97+
98+
}
99+
100+
return false;
101+
}
102+
103+
// V1
104+
// IDEA : BINARY SEARCH
105+
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/solutions/3888242/100-binary-search-video-with-rotation-handling-optimal/
106+
public boolean search_1(int[] nums, int target) {
107+
int low = 0, high = nums.length - 1;
108+
109+
while (low <= high) {
110+
int mid = (low + high) / 2;
111+
if (nums[mid] == target) return true;
112+
113+
if (nums[low] == nums[mid]) {
114+
low++;
115+
continue;
116+
}
117+
118+
if (nums[low] <= nums[mid]) {
119+
if (nums[low] <= target && target <= nums[mid]) high = mid - 1;
120+
else low = mid + 1;
121+
} else {
122+
if (nums[mid] <= target && target <= nums[high]) low = mid + 1;
123+
else high = mid - 1;
124+
}
125+
}
126+
return false;
127+
}
128+
129+
// V2
130+
// IDEA : BINARY SEARCH
131+
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/solutions/2689441/java-best-solution-for-understanding/
132+
public boolean search_2(int[] nums, int target) {
133+
int start = 0, end = nums.length - 1;
134+
while(start <= end) {
135+
int mid = start + (end - start) / 2;
136+
if(nums[mid] == target) return true;
137+
138+
//if there are duplicates
139+
if(nums[start] == nums[mid] && nums[mid] == nums[end]) {
140+
start ++;
141+
end --;
142+
}
143+
144+
//left half is sorted
145+
else if(nums[start] <= nums[mid]) {
146+
if(target >= nums[start] && target <= nums[mid])
147+
end = mid - 1;
148+
149+
else
150+
start = mid + 1;
151+
}
152+
153+
//right half is sorted
154+
else {
155+
if(target <= nums[end] && target >= nums[mid])
156+
start = mid + 1;
157+
158+
else
159+
end = mid - 1;
160+
}
161+
}
162+
return false;
163+
}
164+
}

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

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3794,7 +3794,7 @@ public int findMin(int[] nums){
37943794

37953795
// LC 33
37963796
// 7.42 pm - 8.10 pm
3797-
public int search(int[] nums, int target) {
3797+
public int search_(int[] nums, int target) {
37983798
if (nums.length == 1){
37993799
return nums[0] == target ? 0 : -1;
38003800
}
@@ -3835,6 +3835,104 @@ public int search(int[] nums, int target) {
38353835
return -1;
38363836
}
38373837

3838+
// public int search(int[] nums, int target) {
3839+
//
3840+
// if (nums.length == 1){
3841+
// return nums[0] == target ? 0 : -1;
3842+
// }
3843+
//
3844+
// // binary search
3845+
// int left = 0;
3846+
// int right = nums.length-1;
3847+
// while (right >= left){
3848+
// int mid = (left + right) / 2;
3849+
// if (nums[mid] == target){
3850+
// return mid;
3851+
// }
3852+
// // if right sub array is ascending
3853+
// if (nums[mid] < nums[right]){
3854+
// // if mid < target <= right
3855+
// if (target > nums[mid] && target <= nums[right]){
3856+
// left = mid + 1;
3857+
// }
3858+
// // else
3859+
// else{
3860+
// right = mid - 1;
3861+
// }
3862+
// }
3863+
// // if left sub array is ascending
3864+
// else{
3865+
// // if mid > target > left
3866+
// if (target >= nums[left] && target < nums[mid]){
3867+
// right = mid - 1;
3868+
// }
3869+
// // else
3870+
// else{
3871+
// left = mid + 1;
3872+
// }
3873+
//
3874+
// }
3875+
//
3876+
// }
3877+
//
3878+
// return -1;
3879+
// }
3880+
3881+
// LC 81
3882+
// https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
3883+
// 8.07 pm - 8.20 pm
3884+
public boolean search(int[] nums, int target) {
3885+
3886+
if (nums.length == 1){
3887+
return nums[0] == target;
3888+
}
3889+
Set<Integer> set = new HashSet<>();
3890+
List<Integer> nonDuplicatedNums = new ArrayList<>();
3891+
3892+
for (int x : nums){
3893+
if (!set.contains(x)){
3894+
set.add(x);
3895+
nonDuplicatedNums.add(x);
3896+
}
3897+
}
3898+
3899+
// binary search
3900+
int left = 0;
3901+
int right = nonDuplicatedNums.size() - 1;
3902+
while (right >= left){
3903+
int mid = (left + right) / 2;
3904+
if (nonDuplicatedNums.get(mid) == target){
3905+
return true;
3906+
}
3907+
// if right sub array is ascending
3908+
if (nonDuplicatedNums.get(mid) < nonDuplicatedNums.get(right)){
3909+
// if mid < target <= right
3910+
if (target > nonDuplicatedNums.get(mid) && target <= nonDuplicatedNums.get(right)){
3911+
left = mid + 1;
3912+
}
3913+
// else
3914+
else{
3915+
right = mid - 1;
3916+
}
3917+
}
3918+
// if left sub array is ascending
3919+
else{
3920+
// if mid > target > left
3921+
if (target >= nonDuplicatedNums.get(left) && target < nonDuplicatedNums.get(mid)){
3922+
right = mid - 1;
3923+
}
3924+
// else
3925+
else{
3926+
left = mid + 1;
3927+
}
3928+
3929+
}
3930+
3931+
}
3932+
3933+
return false;
3934+
}
3935+
38383936
}
38393937

38403938

0 commit comments

Comments
 (0)