The fastest kilometer
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- indexes
Problem
"Which was my best kilometer?" Emma asks as she crosses the finish line. Her watch cannot tell her: all it logged was her total time each time she passed a kilometer marker.
Write a function that takes that list of running totals, in whole seconds and always increasing, and returns the number of the kilometer she ran in the least time. Kilometers are counted from 1, and the first one is measured from the start, which is second 0.
With [300, 590, 900, 1185] the kilometers took 300, 290, 310 and 285 seconds: you return 4.
If two kilometers tie, you return the first of them. If the list is empty, you return 0.
Examples
The example
[300, 590, 900, 1185] → 4
The second kilometer
[310, 600, 905] → 2
Two kilometers tied
[300, 590, 880, 1180] → 2
A single kilometer
[400] → 1
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def fastest_km(times):
passJavaScript
function fastestKm(times) {
}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.