11package LeetCodeJava .BinarySearch ;
22
3- import java .util .Arrays ;
4-
5-
63// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
74
85/**
1310 * Companies
1411 * Hint
1512 * 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:
16- *
13+ * <p>
1714 * [4,5,6,7,0,1,2] if it was rotated 4 times.
1815 * [0,1,2,4,5,6,7] if it was rotated 7 times.
1916 * 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]].
20- *
17+ * <p>
2118 * Given the sorted rotated array nums of unique elements, return the minimum element of this array.
22- *
19+ * <p>
2320 * You must write an algorithm that runs in O(log n) time.
24- *
25- *
26- *
21+ * <p>
22+ * <p>
23+ * <p>
2724 * Example 1:
28- *
25+ * <p>
2926 * Input: nums = [3,4,5,1,2]
3027 * Output: 1
3128 * Explanation: The original array was [1,2,3,4,5] rotated 3 times.
3229 * Example 2:
33- *
30+ * <p>
3431 * Input: nums = [4,5,6,7,0,1,2]
3532 * Output: 0
3633 * Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
3734 * Example 3:
38- *
35+ * <p>
3936 * Input: nums = [11,13,15,17]
4037 * Output: 11
4138 * Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
42- *
43- *
39+ * <p>
40+ * <p>
4441 * Constraints:
45- *
42+ * <p>
4643 * n == nums.length
4744 * 1 <= n <= 5000
4845 * -5000 <= nums[i] <= 5000
4946 * All the integers of nums are unique.
5047 * nums is sorted and rotated between 1 and n times.
51- *
52- *
5348 */
5449
5550public class FindMinimumInRotatedSortedArray {
5651
57- // V0'
52+ // V0
5853 // IDEA : BINARY SEARCH (CLOSED BOUNDARY)
54+ public int findMin (int [] nums ) {
55+
56+ // edge case 1): if len == 1
57+ if (nums .length == 1 ) {
58+ return nums [0 ];
59+ }
60+
61+ // edge case 1): array already in ascending order
62+ int left = 0 ;
63+ int right = nums .length - 1 ;
64+ if (nums [right ] > nums [0 ]) {
65+ return nums [0 ];
66+ }
67+
68+ // binary search
69+ while (right >= left ) {
70+ int mid = (left + right ) / 2 ;
71+ // turning point case 1
72+ if (nums [mid ] > nums [mid + 1 ]) {
73+ return nums [mid + 1 ];
74+ }
75+ // TODO : check why below
76+ // turning point case 2
77+ // if (nums[mid] > nums[mid-1]){
78+ // return nums[mid - 1];
79+ // }
80+ if (nums [mid - 1 ] > nums [mid ]) {
81+ return nums [mid ];
82+ }
83+ // left sub array is ascending
84+ if (nums [mid ] > nums [0 ]) {
85+ left = mid + 1 ;
86+ }
87+ // right sub array is ascending
88+ else {
89+ right = mid - 1 ;
90+ }
91+ }
92+
93+ return -1 ;
94+ }
95+
96+ // V0''
97+ // IDEA : BINARY SEARCH (CLOSED BOUNDARY)
98+
5999 /**
60- *
61100 * NOTE !!! the turing point (rotation point)
62- * -> it's ALWAYS the place that min element located (may at at i, i+1, i-1 place)
63- *
64- * case 1) check turing point
65- * case 2) check if left / right sub array is in Ascending order
101+ * -> it's ALWAYS the place that min element located (may at at i, i+1, i-1 place)
102+ * <p>
103+ * case 1) check turing point
104+ * case 2) check if left / right sub array is in Ascending order
66105 */
67106 public int findMin_0_1 (int [] nums ) {
68107
69- if (nums .length == 0 || nums .equals (null )){
108+ if (nums .length == 0 || nums .equals (null )) {
70109 return 0 ;
71110 }
72111
73112 // check if already in ascending order
74- if (nums [0 ] < nums [nums .length - 1 ]){
113+ if (nums [0 ] < nums [nums .length - 1 ]) {
75114 return nums [0 ];
76115 }
77116
78- if (nums .length == 1 ){
117+ if (nums .length == 1 ) {
79118 return nums [0 ];
80119 }
81120
@@ -84,7 +123,7 @@ public int findMin_0_1(int[] nums) {
84123 int r = nums .length - 1 ;
85124
86125 /** NOTE !!! here : r >= l (closed boundary) */
87- while (r >= l ){
126+ while (r >= l ) {
88127 int mid = (l + r ) / 2 ;
89128
90129 /** NOTE !!! BELOW 4 CONDITIONS
@@ -97,23 +136,23 @@ public int findMin_0_1(int[] nums) {
97136 */
98137
99138 /** NOTE !!! we found turing point, and it's the minimum element (idx = mid + 1) */
100- if (nums [mid ] > nums [mid + 1 ]){
101- return nums [mid + 1 ];
139+ if (nums [mid ] > nums [mid + 1 ]) {
140+ return nums [mid + 1 ];
102141
103- /** NOTE !!! we found turing point, and it's the minimum element (idx = mid -1) */
104- }else if (nums [mid - 1 ] > nums [mid ]){
142+ /** NOTE !!! we found turing point, and it's the minimum element (idx = mid -1) */
143+ } else if (nums [mid - 1 ] > nums [mid ]) {
105144 return nums [mid ];
106145
107- /** NOTE !!! compare mid with left, and this means left sub array is ascending order,
108- * so minimum element MUST in right sub array
109- */
110- }else if (nums [mid ] > nums [l ]){
146+ /** NOTE !!! compare mid with left, and this means left sub array is ascending order,
147+ * so minimum element MUST in right sub array
148+ */
149+ } else if (nums [mid ] > nums [l ]) {
111150 l = mid + 1 ;
112151
113- /** NOTE !!! compare mid with left, and this means right sub array is ascending order,
114- * so minimum element MUST in left sub array
115- */
116- }else {
152+ /** NOTE !!! compare mid with left, and this means right sub array is ascending order,
153+ * so minimum element MUST in left sub array
154+ */
155+ } else {
117156 r = mid - 1 ;
118157 }
119158
@@ -126,25 +165,25 @@ public int findMin_0_1(int[] nums) {
126165 // IDEA : BINARY SEARCH (OPEN BOUNDARY)
127166 public int findMin_2 (int [] nums ) {
128167
129- if (nums .length == 0 || nums .equals (null )){
168+ if (nums .length == 0 || nums .equals (null )) {
130169 return 0 ;
131170 }
132171
133172 // already in ascending order
134- if (nums [0 ] < nums [nums .length - 1 ]){
173+ if (nums [0 ] < nums [nums .length - 1 ]) {
135174 return nums [0 ];
136175 }
137176
138177 // binary search
139178 int l = 0 ;
140179 int r = nums .length - 1 ;
141180 /** NOTE !!! here : r > l (open boundary) */
142- while (r > l ){
181+ while (r > l ) {
143182 int mid = (l + r ) / 2 ;
144- if (nums [mid ] > nums [r ]){
183+ if (nums [mid ] > nums [r ]) {
145184 /** NOTE !!! here : (open boundary) */
146185 l = mid + 1 ;
147- }else {
186+ } else {
148187 /** NOTE !!! here : (open boundary) */
149188 r = mid ;
150189 }
0 commit comments