2021-07-01

用PHP刷LintCode算法题----视野争夺

题目: 链接

题解

class StatAnchorAnalysisController extends Backend
{
    public $sum = 0;

    /**
    * Lists all StatAnchorAnalysis models.
    * @return mixed
    */
    public function actionIndex()
    {
        $pos = [[0,2],[0,1],[2,4],[4,5]];
        $length =5;
        var_dump($this->actionGetMiniumVisionWard($pos,$length,0));exit;
    }

    public function actionGetMiniumVisionWard($pos=[],$length=0,$left_length=0){
        //判断数组右边界是否大于等于当前长度
        $right_boundary = array_column($pos,1);
        array_multisort($right_boundary,SORT_DESC,$pos);
        if ($pos[0][1]<$length){
            return -1;
        }
        //判断数组左边界是否小于等于0
        $left_boundary = array_column($pos,0);
        array_multisort($left_boundary,SORT_ASC,$pos);
        if ($pos[0][0]>$left_length){
            return -1;
        }
        $unset_arr = [];
        foreach ($pos as $k=>$v){
            if ($v[0]<=$left_length){
                $unset_arr[] = $v;
            }
        }
        //拿到左边界小于等于零的最大长度守卫
        $unset_length = 0;
        $unset = [];
        foreach ($unset_arr as $k=>$v){
            if ($v[1]-$v[0]>$unset_length){
                $unset = $v;
                $unset_length = $v[1]-$v[0];
            }
        }
        $this->sum++;
        if ($unset_length>=$length){
            return $this->sum;
        }else{
            unset($pos[array_search($unset,$pos)]);
            $this->actionGetMiniumVisionWard($pos,$length-$unset_length,$unset[1]);
        }
        return $this->sum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值