php 求出回旋数组的最小值 可刷新运行 刷新

$arr=[1,2,3,4,5,6,7,8,9,10];//自增$arr1=[5,6,7,8,9,10,1,2,3,4];//是回旋数组,输出1,原理:每次跳过2*n ,符合条件的情况下,再n-- 至符合条件的数

代码:

<?php
//$arr=[1,2,3,4,5,6,7,8,9,10];
$arr1=[5,6,7,8,9,10,1,2,3,4];//输出1


var_export(getMin($arr1,1,true));

function getMin($arr1,$step,$state){
    $length=count($arr1);
    if($step>=$length){
        $step=$length-1;
    }
    if($arr1[0]<$arr1[$step]&&$state){
        if($step==($length-1)){
            return $arr1[0];
        }
        $step=2*$step;
        $min=getMin($arr1,$step,$state);
    }else{
        $step--;
        if($arr1[0]>$arr1[$step]){
            $min=getMin($arr1,$step,false);
        }else{
            return $arr1[$step+1];
        }
    }
    return $min;
}

输出:

1