Skip to content

Commit

Permalink
update 33 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 26, 2024
1 parent 0e15edc commit a526911
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 12 deletions.
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-25
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

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

Expand Down
3 changes: 2 additions & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
20241125: 33
20241124: 153
20241123: 253
20241122: 776,31
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
20241121: 004(todo),81(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
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
2025-01-17 -> ['253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2025-01-15 -> ['004(todo),81(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']
Expand All @@ -10,31 +10,31 @@
2024-12-28 -> ['900']
2024-12-27 -> ['253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2024-12-25 -> ['004(todo),81(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-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['776,31', '444', '298,729']
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-12 -> ['004(todo),81(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-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),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
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-01 -> ['253', '53,210,207']
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
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),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
2024-11-26 -> ['253', '004(todo),81(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),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
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']
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']
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-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
@@ -1,7 +1,45 @@
package LeetCodeJava.BinarySearch;

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

/**
* 33. Search in Rotated Sorted Array
* Solved
* Medium
* Topics
* Companies
* There is an integer array nums sorted in ascending order (with distinct values).
*
* 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].
*
* 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.
*
* You must write an algorithm with O(log n) runtime complexity.
*
*
*
* Example 1:
*
* Input: nums = [4,5,6,7,0,1,2], target = 0
* Output: 4
* Example 2:
*
* Input: nums = [4,5,6,7,0,1,2], target = 3
* Output: -1
* Example 3:
*
* Input: nums = [1], target = 0
* Output: -1
*
*
* Constraints:
*
* 1 <= nums.length <= 5000
* -104 <= nums[i] <= 104
* All values of nums are unique.
* nums is an ascending array that is possibly rotated.
* -104 <= target <= 104
*
*/
public class SearchInRotatedSortedArray {

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

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

// V0'
// IDEA : BINARY SEARCH
// CASE 1) sub array left is sorted
// CASE 2) sub array right is sorted
public int search_0_1(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;
//System.out.println(">>> left = " + left + ", right = " + right + ", mid = " + mid);
if (nums[mid] == target){
return mid;
}
// right sub array is ascending
if (nums[mid] < nums[right]){
// case 1-1) mid < target < right
/** NOTE !!! "<=" with right boundary */
if (target <= nums[right] && target > nums[mid]){
left = mid + 1;
}
// case 1-2) target > mid
else{
right = mid - 1;
}
}
// case 2) left sub array is ascending
else{
// case 2-1) mid > target > left
/** NOTE !!! "<=" with left boundary */
if (nums[left] <= target && target < nums[mid]){
right = mid - 1;
}
// case 2-1) target > mid
else{
left = mid + 1;
}
}
}

return -1;
}

// V0''
// IDEA : BINARY SEARCH (fixed by GPT)
public int search_0(int[] nums, int target) {
public int search_0_2(int[] nums, int target) {

if (nums.length == 0){
return -1;
Expand Down
43 changes: 43 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3792,6 +3792,49 @@ public int findMin(int[] nums){
// return nums[left];
// }

// LC 33
// 7.42 pm - 8.10 pm
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;
System.out.println(">>> left = " + left + ", right = " + right + ", mid = " + mid);
if (nums[mid] == target){
return mid;
}
// right sub array is ascending
if (nums[mid] < nums[right]){
// case 1-1) mid < target < right
if (target <= nums[right] && target > nums[mid]){
left = mid + 1;
}
// case 1-2) target > mid
else{
right = mid - 1;
}
}
// case 2) left sub array is ascending
else{
// case 2-1) mid > target > left
if (nums[left] <= target && target < nums[mid]){
right = mid - 1;
}
// case 2-1) target > mid
else{
left = mid + 1;
}
}
}

return -1;
}

}


0 comments on commit a526911

Please sign in to comment.