We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Greedy
- Mark and Toys
- Discussions
Mark and Toys
Mark and Toys
+ 0 comments Here is problem solution - https://programs.programmingoneonone.com/2021/03/hackerrank-mark-and-toys-solution.html
+ 0 comments Python3 Solution
def maximumToys(prices, k): # Sort the prices in ascending order prices.sort() # Initialize a variable to keep track of the total cost of the toys total_cost = 0 # Initialize a variable to keep track of the number of toys num_toys = 0 # Iterate through the prices list for price in prices: # Check if the current price plus the total cost is less than or equal to k if total_cost + price <= k: # If yes, add the current price to the total cost total_cost += price # Increment the number of toys num_toys += 1 else: # If not, break out of the loop break return num_toys
+ 0 comments def maximumToys(prices, k): # Write your code here prices.sort() count=0 priceSum=0 for i in prices: if (i<=k and priceSum+i<=k): priceSum+=i count+=1 return count
+ 0 comments php
function maximumToys($prices, $k) { // Write your code here $length = count($prices); sort($prices); $sum = 0; for ($i = 0; $i < $length; $i++) { $sum += $prices[$i]; if ($sum > $k) { return $i; } } return $i; }
+ 0 comments public static int maximumToys(List<int> prices, int k) { prices.Sort(); int count = 0; while((k - prices[count]) >= 0) k -= prices[count++]; return count; }
Load more conversations
Sort 956 Discussions, By:
Please Login in order to post a comment