Skip to content

Commit d943ee2

Browse files
authored
feat: add solutions to lc problems: No.4020~4022 (#5354)
1 parent 02a6421 commit d943ee2

29 files changed

Lines changed: 2062 additions & 2 deletions

File tree

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4020.Elevator%20Requests%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [4020. 电梯请求 I](https://leetcode.cn/problems/elevator-requests-i)
10+
11+
[English Version](/solution/4000-4099/4020.Elevator%20Requests%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数 <code>n</code> ,表示一栋楼房的楼层数,楼层编号从 0 到 <code>n - 1</code> 。</p>
18+
19+
<p>同时给你一个整数数组 <code>requests</code> ,其中 <code>requests</code> 表示楼层请求的序列。</p>
20+
21+
<p>一部电梯初始在 0 层,遵循以下规则:</p>
22+
23+
<ul>
24+
<li>电梯每秒移动一层。</li>
25+
<li>电梯按给定的顺序处理请求。</li>
26+
<li>如果电梯已经在请求的楼层,则不需要移动。</li>
27+
<li>处理完一个请求后,电梯立即开始向下一个请求的楼层移动。</li>
28+
</ul>
29+
30+
<p>返回处理所有请求所需的 <strong>总时间</strong> (以秒为单位)。</p>
31+
32+
<p>&nbsp;</p>
33+
34+
<p><strong class="example">示例 1:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>输入:</strong> <span class="example-io">n = 5, requests = [2,1,4,3]</span></p>
38+
39+
<p><strong>输出:</strong> <span class="example-io">7</span></p>
40+
41+
<p><strong>解释:</strong></p>
42+
43+
<ul>
44+
<li><code>requests[0] = 2</code>:从 0 层移动到 2 层需要 2 秒。</li>
45+
<li><code>requests[1] = 1</code>:从 2 层移动到 1 层需要 1 秒。</li>
46+
<li><code>requests[2] = 4</code>:从 1 层移动到 4 层需要 3 秒。</li>
47+
<li><code>requests[3] = 3</code>:从 4 层移动到 3 层需要 1 秒。</li>
48+
</ul>
49+
50+
<p>所需的总时间是 <code>2 + 1 + 3 + 1 = 7</code> 秒。</p>
51+
</div>
52+
53+
<p><strong class="example">示例 2:</strong></p>
54+
55+
<div class="example-block">
56+
<p><strong>输入:</strong> <span class="example-io">n = 3, requests = [2,0,0]</span></p>
57+
58+
<p><strong>输出:</strong> <span class="example-io">4</span></p>
59+
60+
<p><strong>解释:</strong></p>
61+
62+
<ul>
63+
<li><code>requests[0] = 2</code>:从 0 层移动到 2 层需要 2 秒。</li>
64+
<li><code>requests[1] = 0</code>:从 2 层移动到 0 层需要 2 秒。</li>
65+
<li><code>requests[2] = 0</code>:不需要移动。</li>
66+
</ul>
67+
68+
<p>所需的总时间是 <code>2 + 2 + 0 = 4</code> 秒。</p>
69+
</div>
70+
71+
<p>&nbsp;</p>
72+
73+
<p><strong>提示:</strong></p>
74+
75+
<ul>
76+
<li><code>1 &lt;= n &lt;= 100</code></li>
77+
<li><code>1 &lt;= requests.length &lt;= 100</code></li>
78+
<li><code>0 &lt;= requests[i] &lt;= n - 1</code></li>
79+
</ul>
80+
81+
<!-- description:end -->
82+
83+
## 解法
84+
85+
<!-- solution:start -->
86+
87+
### 方法一:模拟
88+
89+
电梯从 $0$ 层出发,按给定顺序处理请求。相邻两次请求之间的移动时间为两层楼层编号之差的绝对值。第一个请求从 $0$ 层到 $\textit{requests}[0]$,耗时即为 $\textit{requests}[0]$;之后将相邻请求的楼层差绝对值累加即可。
90+
91+
时间复杂度 $O(m)$,空间复杂度 $O(1)$。其中 $m$ 是请求的数量。
92+
93+
<!-- tabs:start -->
94+
95+
#### Python3
96+
97+
```python
98+
class Solution:
99+
def elevatorRequests(self, n: int, requests: list[int]) -> int:
100+
return requests[0] + sum(abs(x - y) for x, y in pairwise(requests))
101+
```
102+
103+
#### Java
104+
105+
```java
106+
class Solution {
107+
public int elevatorRequests(int n, int[] requests) {
108+
int ans = requests[0];
109+
for (int i = 1; i < requests.length; ++i) {
110+
ans += Math.abs(requests[i - 1] - requests[i]);
111+
}
112+
return ans;
113+
}
114+
}
115+
```
116+
117+
#### C++
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int elevatorRequests(int n, vector<int>& requests) {
123+
int ans = requests[0];
124+
for (int i = 1; i < requests.size(); ++i) {
125+
ans += abs(requests[i - 1] - requests[i]);
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
132+
#### Go
133+
134+
```go
135+
func elevatorRequests(n int, requests []int) int {
136+
ans := requests[0]
137+
for i, x := range requests[1:] {
138+
ans += abs(x - requests[i])
139+
}
140+
return ans
141+
}
142+
143+
func abs(x int) int {
144+
if x < 0 {
145+
return -x
146+
}
147+
return x
148+
}
149+
```
150+
151+
#### TypeScript
152+
153+
```ts
154+
function elevatorRequests(n: number, requests: number[]): number {
155+
let ans: number = requests[0];
156+
for (let i = 1; i < requests.length; ++i) {
157+
ans += Math.abs(requests[i] - requests[i - 1]);
158+
}
159+
return ans;
160+
}
161+
```
162+
163+
<!-- tabs:end -->
164+
165+
<!-- solution:end -->
166+
167+
<!-- problem:end -->
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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>&nbsp;</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>&nbsp;</p>
71+
<p><strong>Constraints:</strong></p>
72+
73+
<ul>
74+
<li><code>1 &lt;= n &lt;= 100</code></li>
75+
<li><code>1 &lt;= requests.length &lt;= 100</code></li>
76+
<li><code>0 &lt;= requests[i] &lt;= 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 -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int elevatorRequests(int n, vector<int>& requests) {
4+
int ans = requests[0];
5+
for (int i = 1; i < requests.size(); ++i) {
6+
ans += abs(requests[i - 1] - requests[i]);
7+
}
8+
return ans;
9+
}
10+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func elevatorRequests(n int, requests []int) int {
2+
ans := requests[0]
3+
for i, x := range requests[1:] {
4+
ans += abs(x - requests[i])
5+
}
6+
return ans
7+
}
8+
9+
func abs(x int) int {
10+
if x < 0 {
11+
return -x
12+
}
13+
return x
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int elevatorRequests(int n, int[] requests) {
3+
int ans = requests[0];
4+
for (int i = 1; i < requests.length; ++i) {
5+
ans += Math.abs(requests[i - 1] - requests[i]);
6+
}
7+
return ans;
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def elevatorRequests(self, n: int, requests: list[int]) -> int:
3+
return requests[0] + sum(abs(x - y) for x, y in pairwise(requests))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function elevatorRequests(n: number, requests: number[]): number {
2+
let ans: number = requests[0];
3+
for (let i = 1; i < requests.length; ++i) {
4+
ans += Math.abs(requests[i] - requests[i - 1]);
5+
}
6+
return ans;
7+
}

0 commit comments

Comments
 (0)