Skip to content

Commit 41b1b6a

Browse files
committed
Add solution 154.
1 parent bc735b9 commit 41b1b6a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)