Skip to content

Commit a526911

Browse files
committed
update 33 java
1 parent 0e15edc commit a526911

File tree

5 files changed

+146
-12
lines changed

5 files changed

+146
-12
lines changed

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-11-25
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-23
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
20241125: 33
12
20241124: 153
23
20241123: 253
34
20241122: 776,31
4-
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
5+
20241121: 004(todo),81(todo),34(todo),162(todo),275(todo)
56
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
67
20241117: 394
78
20241116: 833,950

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
2025-01-17 -> ['253']
22
2025-01-16 -> ['776,31']
3-
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
3+
2025-01-15 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
44
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
55
2025-01-11 -> ['394']
66
2025-01-10 -> ['833,950']
@@ -10,31 +10,31 @@
1010
2024-12-28 -> ['900']
1111
2024-12-27 -> ['253', '26,27', '802,1197,26']
1212
2024-12-26 -> ['776,31']
13-
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
13+
2024-12-25 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
1414
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1515
2024-12-21 -> ['394', '855,846']
1616
2024-12-20 -> ['833,950', '932']
1717
2024-12-18 -> ['951,792']
1818
2024-12-14 -> ['253', '53,210,207', '163,1048']
1919
2024-12-13 -> ['776,31', '444', '298,729']
20-
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
20+
2024-12-12 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2121
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2222
2024-12-08 -> ['394', '737']
2323
2024-12-07 -> ['833,950', '900', '686,734,737']
2424
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2525
2024-12-05 -> ['776,31', '528,334']
26-
2024-12-04 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
26+
2024-12-04 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)']
2727
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2828
2024-12-01 -> ['253', '53,210,207']
2929
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
30-
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
30+
2024-11-29 -> ['004(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
3131
2024-11-28 -> ['253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
3232
2024-11-27 -> ['776,31', '951,792', '524,221,889']
33-
2024-11-26 -> ['253', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
33+
2024-11-26 -> ['253', '004(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
3434
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),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
36-
2024-11-23 -> ['776,31', '004(todo),153(todo),33(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),153(todo),33(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']
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']
3838
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']
3939
2024-11-20 -> ['394', '939']
4040
2024-11-19 -> ['394', '833,950']

leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
package LeetCodeJava.BinarySearch;
22

33
// https://leetcode.com/problems/search-in-rotated-sorted-array/
4-
4+
/**
5+
* 33. Search in Rotated Sorted Array
6+
* Solved
7+
* Medium
8+
* Topics
9+
* Companies
10+
* There is an integer array nums sorted in ascending order (with distinct values).
11+
*
12+
* Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= 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,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
13+
*
14+
* Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
15+
*
16+
* You must write an algorithm with O(log n) runtime complexity.
17+
*
18+
*
19+
*
20+
* Example 1:
21+
*
22+
* Input: nums = [4,5,6,7,0,1,2], target = 0
23+
* Output: 4
24+
* Example 2:
25+
*
26+
* Input: nums = [4,5,6,7,0,1,2], target = 3
27+
* Output: -1
28+
* Example 3:
29+
*
30+
* Input: nums = [1], target = 0
31+
* Output: -1
32+
*
33+
*
34+
* Constraints:
35+
*
36+
* 1 <= nums.length <= 5000
37+
* -104 <= nums[i] <= 104
38+
* All values of nums are unique.
39+
* nums is an ascending array that is possibly rotated.
40+
* -104 <= target <= 104
41+
*
42+
*/
543
public class SearchInRotatedSortedArray {
644

745
// V0
@@ -54,6 +92,7 @@ public int search(int[] nums, int target) {
5492
// Case 1: subarray on mid's left is sorted
5593
/** NOTE !!! we compare mid with left, instead of 0 idx element */
5694
else if (nums[mid] >= nums[l]) {
95+
/** NOTE !!! ">=" */
5796
if (target >= nums[l] && target < nums[mid]) {
5897
r = mid - 1;
5998
} else {
@@ -63,6 +102,7 @@ else if (nums[mid] >= nums[l]) {
63102

64103
// Case 2: subarray on mid's right is sorted
65104
else {
105+
/** NOTE !!! "<=" */
66106
if (target <= nums[r] && target > nums[mid]) {
67107
l = mid + 1;
68108
} else {
@@ -76,8 +116,55 @@ else if (nums[mid] >= nums[l]) {
76116
}
77117

78118
// V0'
119+
// IDEA : BINARY SEARCH
120+
// CASE 1) sub array left is sorted
121+
// CASE 2) sub array right is sorted
122+
public int search_0_1(int[] nums, int target) {
123+
if (nums.length == 1){
124+
return nums[0] == target ? 0 : -1;
125+
}
126+
127+
// binary search
128+
int left = 0;
129+
int right = nums.length - 1;
130+
while (right >= left){
131+
int mid = (left + right) / 2;
132+
//System.out.println(">>> left = " + left + ", right = " + right + ", mid = " + mid);
133+
if (nums[mid] == target){
134+
return mid;
135+
}
136+
// right sub array is ascending
137+
if (nums[mid] < nums[right]){
138+
// case 1-1) mid < target < right
139+
/** NOTE !!! "<=" with right boundary */
140+
if (target <= nums[right] && target > nums[mid]){
141+
left = mid + 1;
142+
}
143+
// case 1-2) target > mid
144+
else{
145+
right = mid - 1;
146+
}
147+
}
148+
// case 2) left sub array is ascending
149+
else{
150+
// case 2-1) mid > target > left
151+
/** NOTE !!! "<=" with left boundary */
152+
if (nums[left] <= target && target < nums[mid]){
153+
right = mid - 1;
154+
}
155+
// case 2-1) target > mid
156+
else{
157+
left = mid + 1;
158+
}
159+
}
160+
}
161+
162+
return -1;
163+
}
164+
165+
// V0''
79166
// IDEA : BINARY SEARCH (fixed by GPT)
80-
public int search_0(int[] nums, int target) {
167+
public int search_0_2(int[] nums, int target) {
81168

82169
if (nums.length == 0){
83170
return -1;

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,6 +3792,49 @@ public int findMin(int[] nums){
37923792
// return nums[left];
37933793
// }
37943794

3795+
// LC 33
3796+
// 7.42 pm - 8.10 pm
3797+
public int search(int[] nums, int target) {
3798+
if (nums.length == 1){
3799+
return nums[0] == target ? 0 : -1;
3800+
}
3801+
3802+
// binary search
3803+
int left = 0;
3804+
int right = nums.length - 1;
3805+
while (right >= left){
3806+
int mid = (left + right) / 2;
3807+
System.out.println(">>> left = " + left + ", right = " + right + ", mid = " + mid);
3808+
if (nums[mid] == target){
3809+
return mid;
3810+
}
3811+
// right sub array is ascending
3812+
if (nums[mid] < nums[right]){
3813+
// case 1-1) mid < target < right
3814+
if (target <= nums[right] && target > nums[mid]){
3815+
left = mid + 1;
3816+
}
3817+
// case 1-2) target > mid
3818+
else{
3819+
right = mid - 1;
3820+
}
3821+
}
3822+
// case 2) left sub array is ascending
3823+
else{
3824+
// case 2-1) mid > target > left
3825+
if (nums[left] <= target && target < nums[mid]){
3826+
right = mid - 1;
3827+
}
3828+
// case 2-1) target > mid
3829+
else{
3830+
left = mid + 1;
3831+
}
3832+
}
3833+
}
3834+
3835+
return -1;
3836+
}
3837+
37953838
}
37963839

37973840

0 commit comments

Comments
 (0)