|
| 1 | +# [2534. Time Taken to Cross the Door](https://leetcode.com/problems/time-taken-to-cross-the-door) |
| 2 | + |
| 3 | +[中文文档](/solution/2500-2599/2534.Time%20Taken%20to%20Cross%20the%20Door/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>There are <code>n</code> persons numbered from <code>0</code> to <code>n - 1</code> and a door. Each person can enter or exit through the door once, taking one second.</p> |
| 8 | + |
| 9 | +<p>You are given a <strong>non-decreasing</strong> integer array <code>arrival</code> of size <code>n</code>, where <code>arrival[i]</code> is the arrival time of the <code>i<sup>th</sup></code> person at the door. You are also given an array <code>state</code> of size <code>n</code>, where <code>state[i]</code> is <code>0</code> if person <code>i</code> wants to enter through the door or <code>1</code> if they want to exit through the door.</p> |
| 10 | + |
| 11 | +<p>If two or more persons want to use the door at the <strong>same</strong> time, they follow the following rules:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li>If the door was <strong>not</strong> used in the previous second, then the person who wants to <strong>exit</strong> goes first.</li> |
| 15 | + <li>If the door was used in the previous second for <strong>entering</strong>, the person who wants to enter goes first.</li> |
| 16 | + <li>If the door was used in the previous second for <strong>exiting</strong>, the person who wants to <strong>exit</strong> goes first.</li> |
| 17 | + <li>If multiple persons want to go in the same direction, the person with the <strong>smallest</strong> index goes first.</li> |
| 18 | +</ul> |
| 19 | + |
| 20 | +<p>Return <em>an array </em><code>answer</code><em> of size </em><code>n</code><em> where </em><code>answer[i]</code><em> is the second at which the </em><code>i<sup>th</sup></code><em> person crosses the door</em>.</p> |
| 21 | + |
| 22 | +<p><strong>Note</strong> that:</p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li>Only one person can cross the door at each second.</li> |
| 26 | + <li>A person may arrive at the door and wait without entering or exiting to follow the mentioned rules.</li> |
| 27 | +</ul> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Example 1:</strong></p> |
| 31 | + |
| 32 | +<pre> |
| 33 | +<strong>Input:</strong> arrival = [0,1,1,2,4], state = [0,1,0,0,1] |
| 34 | +<strong>Output:</strong> [0,3,1,2,4] |
| 35 | +<strong>Explanation:</strong> At each second we have the following: |
| 36 | +- At t = 0: Person 0 is the only one who wants to enter, so they just enter through the door. |
| 37 | +- At t = 1: Person 1 wants to exit, and person 2 wants to enter. Since the door was used the previous second for entering, person 2 enters. |
| 38 | +- At t = 2: Person 1 still wants to exit, and person 3 wants to enter. Since the door was used the previous second for entering, person 3 enters. |
| 39 | +- At t = 3: Person 1 is the only one who wants to exit, so they just exit through the door. |
| 40 | +- At t = 4: Person 4 is the only one who wants to exit, so they just exit through the door. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p><strong>Example 2:</strong></p> |
| 44 | + |
| 45 | +<pre> |
| 46 | +<strong>Input:</strong> arrival = [0,0,0], state = [1,0,1] |
| 47 | +<strong>Output:</strong> [0,2,1] |
| 48 | +<strong>Explanation:</strong> At each second we have the following: |
| 49 | +- At t = 0: Person 1 wants to enter while persons 0 and 2 want to exit. Since the door was not used in the previous second, the persons who want to exit get to go first. Since person 0 has a smaller index, they exit first. |
| 50 | +- At t = 1: Person 1 wants to enter, and person 2 wants to exit. Since the door was used in the previous second for exiting, person 2 exits. |
| 51 | +- At t = 2: Person 1 is the only one who wants to enter, so they just enter through the door. |
| 52 | +</pre> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Constraints:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li><code>n == arrival.length == state.length</code></li> |
| 59 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 60 | + <li><code>0 <= arrival[i] <= n</code></li> |
| 61 | + <li><code>arrival</code> is sorted in <strong>non-decreasing</strong> order.</li> |
| 62 | + <li><code>state[i]</code> is either <code>0</code> or <code>1</code>.</li> |
| 63 | +</ul> |
| 64 | + |
| 65 | +## Solutions |
| 66 | + |
| 67 | +<!-- tabs:start --> |
| 68 | + |
| 69 | +### **Python3** |
| 70 | + |
| 71 | +```python |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +### **Java** |
| 76 | + |
| 77 | +```java |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +### **C++** |
| 82 | + |
| 83 | +```cpp |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +### **Go** |
| 88 | + |
| 89 | +```go |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +### **...** |
| 94 | + |
| 95 | +``` |
| 96 | +
|
| 97 | +``` |
| 98 | + |
| 99 | +<!-- tabs:end --> |
0 commit comments