From 874d556ad6ede81c8dcd297c9bac6124486a23f9 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 24 Nov 2024 18:05:28 +0800 Subject: [PATCH] update 153 java, progress --- data/progress.txt | 1 + data/to_review.txt | 14 ++-- .../FindMinimumInRotatedSortedArray.java | 57 +++++++++++++-- .../src/main/java/dev/workspace5.java | 73 +++++++++++++++++++ 4 files changed, 133 insertions(+), 12 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index 92be64b9..13e66596 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20241124: 153 20241123: 253 20241122: 776,31 20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo) diff --git a/data/to_review.txt b/data/to_review.txt index a7cd5561..6378f127 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -875,7 +875,7 @@ 2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230'] 2020-06-26 -> ['130', '298', '694', '155,165,167,179'] 2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089'] -2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153'] +2020-06-24 -> ['153', '130', '346,686', '388', '776', '742', '661,662', '517,535,529'] 2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150'] 2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146'] 2020-06-21 -> ['482,127,102,107', '388', '776', '396', '640,645', '675,297,138'] @@ -896,7 +896,7 @@ 2020-06-06 -> ['694', '663', '661,662', '701,450', '451,414,189', '836,860,863', '198,203,230'] 2020-06-05 -> ['269', '663', '661,662', '640,645', '155,165,167,179'] 2020-06-04 -> ['661,662', '701,450', '606,459', '537,553,579', '725,538,089'] -2020-06-03 -> ['701,450', '640,645', '517,535,529', '703,787,819', '153'] +2020-06-03 -> ['153', '701,450', '640,645', '517,535,529', '703,787,819'] 2020-06-02 -> ['269', '701,450', '640,645', '606,459', '355,119,536', '711,046,126,127', '669,682,739,763', '131,134,150'] 2020-06-01 -> ['640,645', '606,459', '537,553,579', '451,414,189', '063,064,120,0146', '642,652,657'] 2020-05-31 -> ['269', '606,459', '675,297,138', '545,617,628'] @@ -909,7 +909,7 @@ 2020-05-24 -> ['725,538,089', '517,535,529', '063,064,120,0146', '836,860,863', '642,652,657', '198,203,230'] 2020-05-23 -> ['725,538,089', '517,535,529', '711,046,126,127', '675,297,138', '545,617,628', '155,165,167,179'] 2020-05-22 -> ['517,535,529', '711,046,126,127', '063,064,120,0146'] -2020-05-21 -> ['711,046,126,127', '063,064,120,0146', '675,297,138', '836,860,863', '703,787,819', '445,508', '153'] +2020-05-21 -> ['153', '711,046,126,127', '063,064,120,0146', '675,297,138', '836,860,863', '703,787,819', '445,508'] 2020-05-20 -> ['063,064,120,0146', '675,297,138', '669,682,739,763', '322,380,394', '131,134,150'] 2020-05-19 -> ['675,297,138', '836,860,863', '642,652,657', '289,295'] 2020-05-18 -> ['836,860,863', '703,787,819', '545,617,628', '251,287'] @@ -917,17 +917,17 @@ 2020-05-16 -> ['703,787,819', '642,652,657', '445,508', '198,203,230'] 2020-05-15 -> ['703,787,819', '669,682,739,763', '545,617,628', '322,380,394', '155,165,167,179'] 2020-05-14 -> ['703,787,819', '669,682,739,763', '642,652,657', '289,295'] -2020-05-13 -> ['669,682,739,763', '642,652,657', '545,617,628', '445,508', '251,287', '153'] +2020-05-13 -> ['153', '669,682,739,763', '642,652,657', '545,617,628', '445,508', '251,287'] 2020-05-12 -> ['642,652,657', '545,617,628', '322,380,394', '231,232,240', '131,134,150'] 2020-05-11 -> ['545,617,628', '445,508', '289,295', '198,203,230'] 2020-05-10 -> ['445,508', '322,380,394', '251,287', '155,165,167,179'] 2020-05-09 -> ['445,508', '322,380,394', '289,295', '231,232,240'] -2020-05-08 -> ['322,380,394', '289,295', '251,287', '198,203,230', '153'] +2020-05-08 -> ['153', '322,380,394', '289,295', '251,287', '198,203,230'] 2020-05-07 -> ['289,295', '251,287', '231,232,240', '155,165,167,179', '131,134,150'] 2020-05-06 -> ['251,287', '231,232,240', '198,203,230'] -2020-05-05 -> ['231,232,240', '198,203,230', '155,165,167,179', '153'] +2020-05-05 -> ['153', '231,232,240', '198,203,230', '155,165,167,179'] 2020-05-04 -> ['198,203,230', '155,165,167,179', '131,134,150'] -2020-05-03 -> ['155,165,167,179', '153'] +2020-05-03 -> ['153', '155,165,167,179'] 2020-05-02 -> ['153', '131,134,150'] 2020-05-01 -> ['153', '131,134,150'] 2020-04-30 -> ['131,134,150'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindMinimumInRotatedSortedArray.java b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindMinimumInRotatedSortedArray.java index d7dacab0..5e40590d 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindMinimumInRotatedSortedArray.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindMinimumInRotatedSortedArray.java @@ -1,13 +1,60 @@ package LeetCodeJava.BinarySearch; +import java.util.Arrays; + + // https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ -import java.util.Arrays; +/** + * 153. Find Minimum in Rotated Sorted Array + * Solved + * Medium + * Topics + * Companies + * Hint + * Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: + * + * [4,5,6,7,0,1,2] if it was rotated 4 times. + * [0,1,2,4,5,6,7] if it was rotated 7 times. + * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. + * + * Given the sorted rotated array nums of unique elements, return the minimum element of this array. + * + * You must write an algorithm that runs in O(log n) time. + * + * + * + * Example 1: + * + * Input: nums = [3,4,5,1,2] + * Output: 1 + * Explanation: The original array was [1,2,3,4,5] rotated 3 times. + * Example 2: + * + * Input: nums = [4,5,6,7,0,1,2] + * Output: 0 + * Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. + * Example 3: + * + * Input: nums = [11,13,15,17] + * Output: 11 + * Explanation: The original array was [11,13,15,17] and it was rotated 4 times. + * + * + * Constraints: + * + * n == nums.length + * 1 <= n <= 5000 + * -5000 <= nums[i] <= 5000 + * All the integers of nums are unique. + * nums is sorted and rotated between 1 and n times. + * + * + */ public class FindMinimumInRotatedSortedArray { - - // V0 + // V0' // IDEA : BINARY SEARCH (CLOSED BOUNDARY) /** * @@ -17,7 +64,7 @@ public class FindMinimumInRotatedSortedArray { * case 1) check turing point * case 2) check if left / right sub array is in Ascending order */ - public int findMin(int[] nums) { + public int findMin_0_1(int[] nums) { if (nums.length == 0 || nums.equals(null)){ return 0; @@ -75,7 +122,7 @@ public int findMin(int[] nums) { return nums[l]; } - // V0' + // V0'' // IDEA : BINARY SEARCH (OPEN BOUNDARY) public int findMin_2(int[] nums) { diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 4129bf64..f6185050 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -3670,6 +3670,79 @@ public int minMeetingRooms(int[][] intervals) { return cnt; } + // LC 153 + // 5.07 PM - 5.20 PM + /** + * + * Input : [4,5,6,7,0,1,2] + * + * Case 1) + * + * right is ascending + * - the target val is bigger / smaller than cur + * + * Case 2) + * + * left is ascending + * - the target val is bigger / smaller than cur + */ + public int findMin(int[] nums) { + + if (nums.length == 1){ + return nums[0]; + } + + if (nums[0] < nums[nums.length-1]){ + return nums[0]; + } + + // binary search + int left = 0; + int right = nums.length-1; + + while (left >= right){ + + int mid = (left + right) / 2; + + System.out.println(">>> mid = " + mid + ", left = " + left + ", right = " + right); + + // turning point (??? + if (nums[mid] > nums[mid+1]){ + return nums[mid+1]; + // turning point (???? + }else if (nums[mid] < nums[mid-1]){ + return nums[mid]; + // norming ordering, compare mid and + }else if (nums[mid] > nums[left]){ + //right = mid; + left = mid + 1; + }else{ + //left = mid + 1; + right = mid-1; + } + +// // right is ascending +// if (nums[mid] < nums[mid+1]){ +// if (nums[mid] > nums[mid-1]){ +// right = mid; +// }else{ +// left = mid + 1; +// } +// +// } +// // left is ascending +// else{ +// if (nums[mid] < nums[mid+1]){ +// right = mid; +// }else{ +// left = mid + 1; +// } +// } + } + + return nums[left]; + } + }