• + 0 comments

    PHP

    function workbook($n, $k, $arr) {
        $result = array_fill(0, array_sum($arr), []);
        $pages = 0;
        $specials = 0;
    
        foreach ($arr as $numPages) {
            for ($problem = 1; $problem <= $numPages; $problem++) {
                if (count($result[$pages]) >= $k || $problem === 1) {
                    $pages++;
                }
                $result[$pages][] = $problem;
            }
        }
    
        for ($i = 1; $i < count($result); $i++) {
            if (in_array($i, $result[$i])) {
                $specials++;
            }
        }
    
        return $result;
    }