Lift Total Travel
Updated
Problem statement
A lift starts on floor s and receives n calls in order. Each call is given as calls[i] = [floor, open]: the call asks the lift to go to floor floor, and open tells whether that floor is working (1) or closed for maintenance (0).
The lift serves the calls strictly in the given order:
- For a call to a working floor, the lift travels straight from its current floor to the called floor. Each floor it passes costs one unit of travel, so going from floor
ato floorbcosts|a - b|. - A call to a closed floor is cancelled. The lift ignores it completely and stays where it is.
Return the total travel of the lift over all the calls.
Function
totalTravel(calls: int[][], s: int) → longExamples
Example 1
calls = [[3,1],[7,1],[2,1]]s = 1return = 11Every floor is working. The lift goes from floor 1 to 3 (2 floors), then from 3 to 7 (4 floors), then from 7 to 2 (5 floors). The total is 2 + 4 + 5 = 11.
Example 2
calls = [[3,1],[7,0],[2,1]]s = 1return = 3The lift goes from floor 1 to 3 (2 floors). Floor 7 is closed, so that call is cancelled and the lift stays on floor 3. Then it goes from 3 to 2 (1 floor). The total is 2 + 1 = 3.
Example 3
calls = [[3,0],[7,0],[2,0]]s = 1return = 0Every called floor is closed, so all three calls are cancelled and the lift never leaves floor 1. The total is 0.
Constraints
1 <= calls.length <= 10^4calls[i].length == 20 <= calls[i][j] <= 10^9calls[i][1]is0or10 <= s <= 10^9
More Infosys Online Assessment questions
- Hiking Club Fastest PaceOA · Seen Nov 2026
- Lowest Common Ancestor with a Changing RootOA · Seen Nov 2026
- Minimum Leaf Path Sum over All RootsOA · Seen Nov 2026
- Split Array to Minimize the Largest SumOA · Seen Nov 2026
- Cherry PickupOA · Seen Nov 2026
- Lexicographically Smallest Grid Path with One Letter ChangeOA · Seen Nov 2026
- Minimum Cost to Supply Water with Wells and PipesOA · Seen Nov 2026
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3OA · Seen Nov 2026
- Minimum Rounds to Complete All TasksOA · Seen Nov 2026
- Count Equal-Distinct Character PartitionsOA · Seen Oct 2026
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OA
- Book Reading EveningsGoogle · OA
- Basic Item DiscountsPalantir · OA
- Wind-Blown Leaves Remaining in a GridAbridge · OA
- Shell Command Counts with History ReferencesTikTok · OA
- Count Dropped RequestsJP Morgan Chase · OA
- Recursively Expanded Shell Command CountsHudson River Trading · OA
- Diagonal Bounce TraversalAutodesk · OA
- First Value Below Both NeighborsCapital One · OA
- REST API: Highest International StudentsRippling · OA