|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4020.Elevator%20Requests%20I/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [4020. Elevator Requests I](https://leetcode.com/problems/elevator-requests-i) |
| 10 | + |
| 11 | +[中文文档](/solution/4000-4099/4020.Elevator%20Requests%20I/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given an integer <code>n</code> denoting the number of floors in a building, where the floors are numbered from 0 to <code>n - 1</code>.</p> |
| 18 | + |
| 19 | +<p>You are also given an integer array <code>requests</code>, where <code>requests</code> represents the sequence of floor requests.</p> |
| 20 | + |
| 21 | +<p>An elevator starts at floor 0, and follows these rules:</p> |
| 22 | + |
| 23 | +<ul> |
| 24 | + <li>The elevator moves one floor per second.</li> |
| 25 | + <li>The elevator serves requests in the given order.</li> |
| 26 | + <li>If the elevator is already on the requested floor, no movement is needed.</li> |
| 27 | + <li>After serving a request, the elevator immediately starts moving toward the next request.</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p>Return the <strong>total time</strong> (in seconds) required to serve all requests.</p> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong class="example">Example 1:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block"> |
| 36 | +<p><strong>Input:</strong> <span class="example-io">n = 5, requests = [2,1,4,3]</span></p> |
| 37 | + |
| 38 | +<p><strong>Output:</strong> <span class="example-io">7</span></p> |
| 39 | + |
| 40 | +<p><strong>Explanation:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>requests[0] = 2</code>: Moving from floor 0 to floor 2 takes 2 seconds.</li> |
| 44 | + <li><code>requests[1] = 1</code>: Moving from floor 2 to floor 1 takes 1 second.</li> |
| 45 | + <li><code>requests[2] = 4</code>: Moving from floor 1 to floor 4 takes 3 seconds.</li> |
| 46 | + <li><code>requests[3] = 3</code>: Moving from floor 4 to floor 3 takes 1 second.</li> |
| 47 | +</ul> |
| 48 | + |
| 49 | +<p>The total time required is <code>2 + 1 + 3 + 1 = 7</code> seconds.</p> |
| 50 | +</div> |
| 51 | + |
| 52 | +<p><strong class="example">Example 2:</strong></p> |
| 53 | + |
| 54 | +<div class="example-block"> |
| 55 | +<p><strong>Input:</strong> <span class="example-io">n = 3, requests = [2,0,0]</span></p> |
| 56 | + |
| 57 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 58 | + |
| 59 | +<p><strong>Explanation:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li><code>requests[0] = 2</code>: Moving from floor 0 to floor 2 takes 2 seconds.</li> |
| 63 | + <li><code>requests[1] = 0</code>: Moving from floor 2 to floor 0 takes 2 seconds.</li> |
| 64 | + <li><code>requests[2] = 0</code>: No movement is needed.</li> |
| 65 | +</ul> |
| 66 | + |
| 67 | +<p>The total time required is <code>2 + 2 + 0 = 4</code> seconds.</p> |
| 68 | +</div> |
| 69 | + |
| 70 | +<p> </p> |
| 71 | +<p><strong>Constraints:</strong></p> |
| 72 | + |
| 73 | +<ul> |
| 74 | + <li><code>1 <= n <= 100</code></li> |
| 75 | + <li><code>1 <= requests.length <= 100</code></li> |
| 76 | + <li><code>0 <= requests[i] <= n - 1</code></li> |
| 77 | +</ul> |
| 78 | + |
| 79 | +<!-- description:end --> |
| 80 | + |
| 81 | +## Solutions |
| 82 | + |
| 83 | +<!-- solution:start --> |
| 84 | + |
| 85 | +### Solution 1: Simulation |
| 86 | + |
| 87 | +The elevator starts at floor $0$ and serves requests in the given order. The travel time between two consecutive requests is the absolute difference of their floor numbers. The first request goes from floor $0$ to $\textit{requests}[0]$, which takes $\textit{requests}[0]$ seconds. Then we add the absolute differences of adjacent requests. |
| 88 | + |
| 89 | +The time complexity is $O(m)$, and the space complexity is $O(1)$, where $m$ is the number of requests. |
| 90 | + |
| 91 | +<!-- tabs:start --> |
| 92 | + |
| 93 | +#### Python3 |
| 94 | + |
| 95 | +```python |
| 96 | +class Solution: |
| 97 | + def elevatorRequests(self, n: int, requests: list[int]) -> int: |
| 98 | + return requests[0] + sum(abs(x - y) for x, y in pairwise(requests)) |
| 99 | +``` |
| 100 | + |
| 101 | +#### Java |
| 102 | + |
| 103 | +```java |
| 104 | +class Solution { |
| 105 | + public int elevatorRequests(int n, int[] requests) { |
| 106 | + int ans = requests[0]; |
| 107 | + for (int i = 1; i < requests.length; ++i) { |
| 108 | + ans += Math.abs(requests[i - 1] - requests[i]); |
| 109 | + } |
| 110 | + return ans; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +#### C++ |
| 116 | + |
| 117 | +```cpp |
| 118 | +class Solution { |
| 119 | +public: |
| 120 | + int elevatorRequests(int n, vector<int>& requests) { |
| 121 | + int ans = requests[0]; |
| 122 | + for (int i = 1; i < requests.size(); ++i) { |
| 123 | + ans += abs(requests[i - 1] - requests[i]); |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +}; |
| 128 | +``` |
| 129 | +
|
| 130 | +#### Go |
| 131 | +
|
| 132 | +```go |
| 133 | +func elevatorRequests(n int, requests []int) int { |
| 134 | + ans := requests[0] |
| 135 | + for i, x := range requests[1:] { |
| 136 | + ans += abs(x - requests[i]) |
| 137 | + } |
| 138 | + return ans |
| 139 | +} |
| 140 | +
|
| 141 | +func abs(x int) int { |
| 142 | + if x < 0 { |
| 143 | + return -x |
| 144 | + } |
| 145 | + return x |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +#### TypeScript |
| 150 | + |
| 151 | +```ts |
| 152 | +function elevatorRequests(n: number, requests: number[]): number { |
| 153 | + let ans: number = requests[0]; |
| 154 | + for (let i = 1; i < requests.length; ++i) { |
| 155 | + ans += Math.abs(requests[i] - requests[i - 1]); |
| 156 | + } |
| 157 | + return ans; |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +<!-- tabs:end --> |
| 162 | + |
| 163 | +<!-- solution:end --> |
| 164 | + |
| 165 | +<!-- problem:end --> |
0 commit comments