算法-php 蓄水池問題

<?php
//fscanf(STDIN, "%d", $a);
//printf("%d\n",$a);

$args = [
    0 => 10,
    1 => 20,
    2 => 30,
    3 => 20,
    4 => 100,
    5 => 80,
    6 => 70
];

$res = 0;
$left = 0;
$right = count($args);

while($left < $right) {
    if (empty($args[$left])) {
        $left++;
    }
    if (empty($args[$right])) {
        $right--;
    }
    $tmpres = ($right-$left) * min($args[$left], $args[$right]);
    echo " {$left} * {$right} {$tmpres} ";
    ($res < $tmpres) && $res = $tmpres;
    if ($args[$left] < $args[$right]) {
        $left++;
    }else {
        $right--;
    }
}
echo $res;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章