Speed to Pressure Lookup
Learn this problemProblem statement
Speed to Pressure Lookup
Write an algorithm that receives a speed and returns the corresponding pressure. Each speed interval uses the pressure associated with the smaller endpoint. For example, every speed from 0 up to, but not including, the next listed speed uses the pressure associated with 0.
The source notes that the lookup values are not guaranteed to remain hard-coded.
Practice Contract
For this exercise, assume speedBreakpoints is a strictly increasing list whose first value is 0, and pressures[i] is the pressure that begins at speedBreakpoints[i].
Return the pressure at the greatest breakpoint that is less than or equal to speed. A speed equal to a breakpoint uses that breakpoint's pressure. A speed above the final breakpoint uses the final pressure.
Function
lookupPressure(speedBreakpoints: int[], pressures: int[], speed: int) → intExamples
Example 1
speedBreakpoints = [0,10,20,30]pressures = [100,95,80,60]speed = 7return = 100The greatest breakpoint not exceeding 7 is 0, so the answer is pressures[0] = 100.
Example 2
speedBreakpoints = [0,10,20,30]pressures = [100,95,80,60]speed = 10return = 95A speed equal to a breakpoint uses that breakpoint's pressure, so 10 maps to 95.
Example 3
speedBreakpoints = [0,10,20,30]pressures = [100,95,80,60]speed = 99return = 60The speed is above the final breakpoint, so the final pressure applies.
Constraints
1 ≤ speedBreakpoints.length == pressures.length ≤ 100,000speedBreakpoints[0] == 00 ≤ speedBreakpoints[i] ≤ 10^9speedBreakpointsis strictly increasing.-10^9 ≤ pressures[i] ≤ 10^90 ≤ speed ≤ 10^9