Hackerrank: Mark and Toys Solution
1function maximumToys(prices, k) {2 prices.sort((a, b) => a - b)3 let sum = 04 const prefixSums = prices.map(price => {5 sum += price6 return sum7 })8 let numberOfToyes9 for (let i = 0; i < prefixSums.length; i++) {10 if (prefixSums[i] > k) {11 numberOfToyes = i12 break13 }14 }15 return numberOfToyes16}
Comments
Loading comments...
Tags
hackerrank
sorting
prefix sum
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Hackerrank: Sherlock and the Valid String
Jan 28, 2021
Previous Post
Hackerrank: Frequency Queries
Jan 27, 2021