Beating 100% users in Runtime & Memory using PHP
Code\n\nclass Solution {\n    function fallingSquares($positions) {\n        $hgt = [0];\n        $pos = [0];\n        $res = [];\n        $mx = 0;\n\n        f
# Code\n```\nclass Solution {\n    function fallingSquares($positions) {\n        $hgt = [0];\n        $pos = [0];\n        $res = [];\n        $mx = 0;\n\n        foreach ($positions as $box) {\n            $mx = max($mx, self::helper($box, $pos, $hgt));\n            $res[] = $mx;\n        }\n\n        return $res;\n    }\n\n    private static function helper($box, &$pos, &$hgt) {\n        list($l, $h) = $box;\n        $r = $l + $h;\n\n        $i = self::bisectRight($pos, $l);\n        $j = self::bisectLeft($pos, $r);\n\n        $maxHeight = 0;\n        for ($k = $i - 1; $k < $j; $k++) {\n            if (isset($hgt[$k]) && $hgt[$k] > $maxHeight) {\n                $maxHeight = $hgt[$k];\n            }\n        }\n\n        $res = $h + $maxHeight;\n        array_splice($pos, $i, $j - $i, [$l, $r]);\n        array_splice($hgt, $i, $j - $i, [$res, isset($hgt[$j - 1]) ? $hgt[$j - 1] : 0]);\n\n        return $res;\n    }\n\n    private static function bisectRight($arr, $x) {\n        $lo = 0;\n        $hi = count($arr);\n        while ($lo < $hi) {\n            $mid = intval(($lo + $hi) / 2);\n            if ($x < $arr[$mid]) {\n                $hi = $mid;\n            } else {\n                $lo = $mid + 1;\n            }\n        }\n        return $lo;\n    }\n\n    private static function bisectLeft($arr, $x) {\n        $lo = 0;\n        $hi = count($arr);\n        while ($lo < $hi) {\n            $mid = intval(($lo + $hi) / 2);\n            if ($arr[$mid] < $x) {\n                $lo = $mid + 1;\n            } else {\n                $hi = $mid;\n            }\n        }\n        return $lo;\n    }\n}\n```
['PHP']