Dead-End Driveway
Cars park one behind another in a narrow dead-end driveway, so only the car that parked last can drive straight out. events is applied in order:
"in X": carXparks behind every car already there."out X": carXleaves. Every car parked after it must first back out into the street, and then they park again in the same order.
Return the total number of times a car had to back out to let another car leave.
Example 1
events ["in a","in b","in c","out a","out c"]
returns 2
For a to leave, b and c back out (2 moves). Then c is last in line and leaves without moving anyone.
Example 2
events ["in x","out x","in y","in z","out y","in w","out z"]
returns 2
Only z backs out for y, and only w backs out for z.
Constraints
0 <= len(events) <= 100- names are 1 to 5 lowercase letters
out Xalways names a parked car, and a parked car never parks again before it leaves
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Elevator TripsBeginner
- Flip the PancakesBeginner
- Last In, First OutBeginner
- Notes on the BoardBeginner
- One-Bay Car WashBeginner
- Round the QueueBeginner
- Served in OrderBeginner
- Stack After CommandsBeginner