Skip to the content

The galleon's hold

Problem

Whatever doesn't fit in the hold stays on the island. The loot is piled on the beach in lots, and within a lot every crate is worth the same. All crates take the same room, and the captain wants to sail away with the most valuable load.

Write a function that takes lots, a list of strings shaped like "quantityxvalue" ("4x7" is 4 crates worth 7 coins each; both numbers are integers from 1 up; the list may be empty), and space, an integer from 0 up: how many crates fit. Return an integer: the highest value you can load. You may take just some of the crates of a lot. If they all fit, you take them all.

With ["4x7", "2x10", "5x3"] and space 5: you load the 2 crates worth 10 and 3 of the ones worth 7, for 20 + 21 = 41.

Examples

  • The one from the example

    ["4x7", "2x10", "5x3"], 5 → 41

  • Everything fits

    ["2x5", "1x8"], 10 → 18

  • No room

    ["3x9"], 0 → 0

  • No loot

    [], 6 → 0

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def loot(lots, space):
    pass

JavaScript

function loot(lots, space) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →