As many shows as you can
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- lists
- strings
- sorting
Problem
Twelve hours of music, five stages and only one pair of legs. You want to see as many whole shows as you can: you don't walk in once one has started, you don't leave before it ends, and you can't be at two at once. Write a function that takes shows, a list of strings "start-end" with whole hours from 0 to 24 (the start always comes before the end; the list may be empty), and returns an integer: the most shows you can see. If one starts right at the hour another one ends, you catch both. With ["9-12", "10-11", "11-13", "13-15"] you see 3: "10-11", "11-13" and "13-15". If you go to the one that starts first, "9-12", you only catch 2.
Careful: the one that starts first isn't always the best pick, and neither is the shortest.
Examples
The example
["9-12", "10-11", "11-13", "13-15"] → 3
One right after another
["8-10", "10-12", "12-14"] → 3
The shortest gets in the way
["9-13", "12-14", "13-17"] → 2
A single show
["20-23"] → 1
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def max_shows(shows):
passJavaScript
function maxShows(shows) {
}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.