Hackerrank: Mark and Toys Solution

1function maximumToys(prices, k) {
2 prices.sort((a, b) => a - b)
3 let sum = 0
4 const prefixSums = prices.map(price => {
5 sum += price
6 return sum
7 })
8 let numberOfToyes
9 for (let i = 0; i < prefixSums.length; i++) {
10 if (prefixSums[i] > k) {
11 numberOfToyes = i
12 break
13 }
14 }
15 return numberOfToyes
16}

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: Fraudulent Activity Notifications

Jan 28, 2021

HoningJS

Search Posts