Mark and Toys Discussions | Algorithms | HackerRank

Mark and Toys

  • + 0 comments

    PHP

    function maximumToys($prices, $k) {
        // Write your code here
        sort ($prices);
        $hasil   = 0;
        $counter = 0;
        
        for ($i=0; $i < count($prices); $i++) {
            if (($hasil+$prices[$i]) <= $k) {
                $hasil+=$prices[$i];
                $counter++;
            }
            else {
                break;
            }
        }
        return $counter;
    }