2534. Time Taken to Cross the Door - Detailed Explanation
Problem Statement
Description:
A door is used by a line of people to exit a building. The door is initially closed. Each person takes exactly 1 second to cross the door once it is open. However, when the door is closed and a person arrives, it takes an extra k seconds to open it. Once the door is open, it remains open as long as the gap between the finish time of the previous crossing and the arrival of the next person is at most d seconds. If a person arrives after a gap of more than d seconds, the door is assumed to have closed immediately after the previous person crossed, and the new arrival must wait for the door to open again (adding k extra seconds).
You are given:
- A sorted array
arrival
wherearrival[i]
represents the time (in seconds) when the i-th person arrives at the door. - An integer
k
representing the door-opening delay (in seconds) when the door is closed. - An integer
d
representing the maximum allowed gap (in seconds) between consecutive crossings for which the door stays open.
Your task is to simulate the process and return the total time (in seconds) from time 0 until the last person finishes crossing the door.
Examples:
-
Example 1:
- Input:
arrival = [1, 2, 3]
,k = 2
,d = 1
- Output:
6
- Explanation:
- Person 1: Arrives at time 1. Since the door is closed, wait k = 2 seconds (door opens at time 3) and then cross (takes 1 second). Finish at time 4.
- Person 2: Arrives at time 2 (while the door is already in the process of opening or is open by the time they need to cross). The gap from the previous finish (4) is within d = 1 (actually, person 2 has been waiting). They cross immediately after person 1, finishing at time 5.
- Person 3: Arrives at time 3. The gap from the previous finish (5) is within d, so they cross next and finish at time 6.
- Total time: 6 seconds.
- Input:
-
Example 2:
- Input:
arrival = [1, 4, 10]
,k = 3
,d = 5
- Output:
11
- Explanation:
- Person 1: Arrives at 1. The door is closed, so wait k = 3 seconds (opens at time 4), then cross (finishing at time 5).
- Person 2: Arrives at 4. Since person 1 is still in process or has just finished (at 5) and the gap (4–5 or waiting since 4) is ≤ d = 5, the door remains open. Person 2 crosses immediately after person 1, finishing at time 6.
- Person 3: Arrives at 10. The gap from the previous finish (6) is 4 seconds (≤ 5), so the door stays open. Person 3 starts crossing at their arrival time 10 and finishes at time 11.
- Total time: 11 seconds.
- Input:
Constraints:
- (1 \leq \text{arrival.length} \leq 10^5)
- (1 \leq \text{arrival}[i] \leq 10^9) (and the array is sorted in non-decreasing order)
- (1 \leq k, d \leq 10^5)
Hints Before You Start
- Hint 1: Think of the process as a simulation where you maintain a “current time” that tracks when the door (or the last crossing) is finished.
- Hint 2: For the first person (or whenever the door is closed), you must add the door-opening delay k before the person can cross.
- Hint 3: For subsequent persons, compare their arrival time with the finish time of the previous crossing. If the gap is within d, the door remains open and the person crosses immediately; otherwise, the door has closed and you must add another k seconds.
Approaches
Simulation Approach
Idea:
- Maintain a simulation variable:
Letcurrent_time
represent the time by which the previous person has finished crossing. - Process each arrival in order:
-
If the next person’s arrival time is later than
current_time
, updatecurrent_time
to the arrival time. -
If this is the first person or if the gap between the current arrival and the previous finish is greater than d, add the door-opening delay k.
-
Add 1 second for the person to cross.
-
Update the finish time.
-
- Return
current_time
after processing all persons.
Complexity Analysis:
-
Time Complexity: O(n), where n is the number of persons (each person is processed once).
-
Space Complexity: O(1), only a few variables are maintained.
Code Implementations
Python Code
Java Code
Step-by-Step Walkthrough and Visual Example
Example 1 Walkthrough
- Input:
arrival = [1, 2, 3]
,k = 2
,d = 1
- Person 1:
- Arrival = 1,
current_time
is 0 → updatecurrent_time
to 1. - Since it’s the first person, add door-opening delay: 1 + 2 = 3.
- Person 1 crosses (1 second): finish at 3 + 1 = 4.
- Set
prev_finish = 4
.
- Arrival = 1,
- Person 2:
- Arrival = 2, but
current_time
is already 4. - Gap = (arrival 2) – (prev_finish 4) = -2 (door still open).
- No door-opening delay is added.
- Person 2 crosses: finish at 4 + 1 = 5.
- Update
prev_finish = 5
.
- Arrival = 2, but
- Person 3:
- Arrival = 3,
current_time
is 5. - Gap = 3 – 5 = -2 (door remains open).
- Person 3 crosses: finish at 5 + 1 = 6.
- Update
prev_finish = 6
.
- Arrival = 3,
Total Time: 6 seconds.
Example 2 Walkthrough
- Input:
arrival = [1, 4, 10]
,k = 3
,d = 5
- Person 1:
- Arrival = 1, update
current_time
to 1. - Door closed, add delay: 1 + 3 = 4.
- Cross: finish at 4 + 1 = 5.
- Set
prev_finish = 5
.
- Arrival = 1, update
- Person 2:
- Arrival = 4,
current_time
is 5. - Gap = 4 – 5 = -1 (door remains open).
- Cross immediately: finish at 5 + 1 = 6.
- Update
prev_finish = 6
.
- Arrival = 4,
- Person 3:
- Arrival = 10,
current_time
is behind arrival → updatecurrent_time
to 10. - Gap = 10 – 6 = 4, which is ≤ d (5) so the door stays open.
- Cross: finish at 10 + 1 = 11.
- Update
prev_finish = 11
.
- Arrival = 10,
Total Time: 11 seconds.
Common Mistakes
-
Forgetting to “catch up” to the arrival time:
If the simulation’s current time is behind a person’s arrival, you must update it to the arrival time. -
Miscomputing the gap:
Always compare the new person’s arrival time with the finish time of the previous crossing. -
Adding the door-opening delay when it isn’t needed:
Only add the delay k if the door is closed—that is, if it’s the first person or the gap from the previous finish is greater than d.
Edge Cases
-
Single Person:
Ensure that even one person causes the door-opening delay plus 1 second for crossing. -
Simultaneous Arrivals:
When multiple persons arrive at the same time (or before the door even opens), only the first triggers the opening delay. -
Large Gaps Between Arrivals:
When the gap between persons exceeds d, each new arrival triggers a door-opening delay.
Related Problems
-
Minimum Time to Cross a Bridge:
Similar simulation problems that require handling delays and waiting times. -
Time to Buy Tickets:
Involves simulating waiting times and processing times. -
Queue Simulation Problems:
More general simulation problems that involve sequential processing and waiting.
GET YOUR FREE
Coding Questions Catalog
