FunctionRacerProblemsRace

Most Marbles in a Bag Limit

EasyGreedyPython, JavaScript, TypeScript, Java, C++

Each item of crates is [bags, marbles]: that crate holds bags identical bags with marbles marbles in each bag. You can carry at most limit bags in total, taken from any crates in any mix, and a bag is taken whole or not at all.

Return the largest number of marbles you can carry. The largest number is unique, so no tie rule is needed. Return 0 when crates is empty or limit is 0.

Example 1

crates [[1,3],[2,2],[3,1]]
limit 4
returns 8

Take the single bag of 3, both bags of 2 and one bag of 1: 3 + 4 + 1 = 8 marbles in 4 bags.

Example 2

crates [[5,10],[2,5],[4,7]]
limit 10
returns 83

All five bags of 10, all four bags of 7 and one bag of 5: 50 + 28 + 5 = 83 marbles in 10 bags.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Greedy problems