본문 바로가기

Computer Science/Algorithm with Code

[HackerRank] Preparation_Kit (15. Mark and Toys) [Sorting]

func maximumToys(prices: [Int], k: Int) -> Int {
	var ans = 0
	let sortedPrices = prices.sorted()
	var budget = k
	for p in sortedPrices {
		if budget > p {
			budget -= p
			ans += 1
		}
	}
	return ans
}