Skip to main content

Formula 1 Last Lap Improvement

easy
Software engineer

In a Formula 1 race, each driver completes the same number of laps. You are given a list of driver names drivers and a 2D list lapTimes, where lapTimes[i][j] represents the time in seconds that the $i^{th}$ driver took to finish the $j^{th}$ lap.

A driver's last-lap improvement is defined as the difference between their own average lap time before the last lap and their last lap time.

Specifically, for a driver:

  1. Calculate the average of all their laps except the last one.
  2. Subtract the time of the last lap from this average.

Return the name of the driver with the greatest last-lap improvement. If multiple drivers have the same improvement, return the one whose name comes first in lexicographical order.

Example 1

Input

drivers = ["Hamilton", "Verstappen"], lapTimes = [[90.5, 91.0, 88.0], [89.0, 90.5, 89.5]]

Output

"Hamilton"

Explanation

Hamilton's previous average: (90.5 + 91.0) / 2 = 90.75. Improvement: 90.75 - 88.0 = 2.75. Verstappen's previous average: (89.0 + 90.5) / 2 = 89.75. Improvement: 89.75 - 89.5 = 0.25. 2.75 > 0.25, so Hamilton wins.

Example 2

Input

drivers = ["Leclerc", "Russell"], lapTimes = [[95.0, 94.0, 91.0], [92.0, 93.0, 92.5]]

Output

"Leclerc"

Example 3

Input

drivers = ["Alonso", "Sainz", "Norris", "Piastri"], lapTimes = [[88.5, 87.0, 89.0, 85.5], [90.0, 88.5, 87.0, 86.0], [91.0, 90.0, 89.5, 87.0], [89.0, 88.0, 90.0, 88.5]]

Output

"Norris"

Constraints

  • 2 <= lapTimes[i].length <= 1000
  • 1 <= drivers.length == lapTimes.length <= 1000
  • 0 <= lapTimes[i][j] <= 1000
  • All driver names are unique and consist of English letters.

Onsite

ArrayGreedyMathMatrixString
Language
Code editor loads in the browser.

Output

Input

["Hamilton","Verstappen"]
[[90.5,91,88],[89,90.5,89.5]]

Expected

"Hamilton"

Your output

Run to see your output.