Problem · Array
Minimum Tunnel Crossing Time
Learn this problemProblem statement
A group must pass through a tunnel of height tunnelHeight. Every person's height is strictly less than the tunnel height.
A crossing may contain either one person or two people:
- One person takes
soloTimeseconds. - Two people may cross together only when the sum of their heights is strictly less than
tunnelHeight; that crossing takespairTimeseconds.
Crossings happen one after another. Return the minimum total time needed for everyone to cross.
Function
minimumTunnelTime(heights: int[], tunnelHeight: int, soloTime: int, pairTime: int) → intExamples
Example 1
heights = [1,3,4,4,2]tunnelHeight = 9soloTime = 4pairTime = 6return = 16Pair heights 1 and 4, pair heights 3 and 4, and let the remaining person cross alone. The total is 6 + 6 + 4 = 16.
Example 2
heights = [1,3,4]tunnelHeight = 9soloTime = 4pairTime = 6return = 10One valid pair crosses in 6 seconds and the remaining person crosses in 4 seconds.
Constraints
heights.length >= 1.- Every height is positive and strictly less than
tunnelHeight. soloTimeandpairTimeare positive.