Mark and Toys Discussions | Algorithms | HackerRank
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.
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
Mark and Toys
You are viewing a single comment's thread. Return to all comments →
Python3 Solution