Compute the total travel of a lift that serves calls in order.
A lift starts on floor S and receives N calls in order, each asking for floor f[i]. It serves calls strictly in order, travelling straight from its current floor to the one called at one unit of travel per floor. Calls to floors closed for maintenance are cancelled: the lift ignores them completely and stays where it is. Return the total travel over the shift.
Input: N, then N rows of (floor, working) where 1 means the floor is working and 0 means under maintenance, then S. Calls to closed floors are cancelled and the lift stays where it is. Example 1: N=3, calls (3,1) (7,1) (2,1), S=1 -> 11 Example 2: N=3, calls (3,1) (7,0) (2,1), S=1 -> 3 Example 3: N=3, calls (3,0) (7,0) (2,0), S=1 -> 0
Format · Online coding test on the Wingspan platform, in Java.
Community answers
Related questions
Compute the total size of a file or directory in a directory tree.Print the level order traversal of a tree.Related courses


