The error occurred because the binarySearch function is not defined within the Solution class. To fix this, you can either define the binarySearch function within the Solution class or move it outside the class and call it as a standalone function.

Here's an updated version of the code with the binarySearch function defined within the Solution class:

class Solution {
    /**
     * @param Integer[] $obstacles
     * @return Integer[]
     */
    function binarySearch($arr, $target) {
        $left = 0;
        $right = count($arr) - 1;
        while ($left <= $right) {
            $mid = $left + ($right - $left >> 1);
            if ($arr[$mid] <= $target) $left = $mid + 1;
            else $right = $mid - 1;
        }
        return $left;
    }
    
    function longestObstacleCourseAtEachPosition($obstacles) {
        $ans = array();
        $dp = array();
        for ($i = 0; $i < count($obstacles); $i++) {
            $v = $obstacles[$i];
            $p = $this->binarySearch($dp, $v+1);
            if ($p < count($dp)) $dp[$p] = $v;
            else $dp[] = $v;
            $ans[] = $p + 1;
        }
        return $ans;
    }
}

Now, you should be able to call the longestObstacleCourseAtEachPosition method without any errors

class Solution param Integer $obstacles return Integer 	function binarySearch$arr $target 		$left = 0;		$right = count$arr - 1;		while $left = $right 			$mid = $left + $right - $lef

原文地址: http://www.cveoy.top/t/topic/iSPT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录