• + 2 comments

    Here is the PHP code of this solution. Teached me a lot. Thanks.

    <?php
    $_fp = fopen("php://stdin", "r");
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    fscanf($_fp, "%d %d", $size, $operations);
    $array = [];
    
    $max = PHP_INT_MIN;
    
    for($i=0; $i<$operations; $i++){
        fscanf($_fp, "%d %d %d", $start, $end, $value);
        $negativeValue = $value * -1;
        if(!isset($array[$start])) $array[$start] = $value;
        else $array[$start] += $value;
        if($end+1<=$size){
            if(!isset($array[$end+1])) $array[$end+1] = $negativeValue;
            else $array[$end+1] -= $value;
        }
    }
    ksort($array);
    
    $sum = 0;
    foreach($array as $amount){
        $sum += $amount;
        if($sum>$max) $max=$sum;
    }
    
    echo $max;
    ?>