File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
src/Algorithms/0154.find-minimum-in-rotated-sorted-array-ii Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+
3
+ /**
4
+ * @param Integer[] $nums
5
+ * @return Integer
6
+ */
7
+ function findMin ($nums ) {
8
+ $r = count ($nums )- 1 ;
9
+ $l = 0 ;
10
+ if ($r < 0 ) return - 1 ;
11
+ $first = $nums [0 ];
12
+ if ($first == $nums [$r ]){// move util the start is not equal to the end any more;
13
+ while ($l < $r && $nums [$l ]== $first ) $l ++ ;
14
+ while ($l < $r && $nums [$r ]== $first ) $r -- ;
15
+ }
16
+ if ($nums [$l ]<= $nums [$r ]){ // deal with the ascending condition.
17
+ return $nums [$l ]< $first ?$nums [$l ]:$first ;
18
+ }
19
+ while ($l < $r - 1 ){// now we are bold to handle it as there must be a steep and the minimum is the right one.
20
+ $m = intval ($l + ($r - $l )/ 2 );
21
+ if ($nums [$m ]>= $nums [$l ]) $l = $m ;
22
+ else $r = $m ;
23
+ }
24
+ return $nums [$r ];
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments