diff --git a/data/progress.md b/data/progress.md index ffa7ded6..984d6687 100644 --- a/data/progress.md +++ b/data/progress.md @@ -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 diff --git a/data/progress.txt b/data/progress.txt index 13e66596..c99a407a 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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 diff --git a/data/to_review.txt b/data/to_review.txt index 6378f127..0706093b 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java index fb1a24cf..0896fa4c 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java @@ -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 @@ -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 { @@ -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 { @@ -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; diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 04f30f32..32523a1b 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -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; + } + }