Skip to content

Commit 73b660f

Browse files
committed
feat: leetcode 1441
1 parent 4dd5850 commit 73b660f

File tree

8 files changed

+135
-1
lines changed

8 files changed

+135
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:520-green" alt="进度:520">
5+
<img src="https://img.shields.io/badge/-进度:521-green" alt="进度:521">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,12 @@
24952495
"path": "./problemset/maximum-score-after-splitting-a-string/README.md",
24962496
"difficulty": "简单"
24972497
},
2498+
{
2499+
"id": "1441",
2500+
"title": "用栈操作构建数组",
2501+
"path": "./problemset/build-an-array-with-stack-operations/README.md",
2502+
"difficulty": "中等"
2503+
},
24982504
{
24992505
"id": "1464",
25002506
"title": "数组中的两元素的最大乘积",

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -4559,6 +4559,16 @@
45594559
"url": "https://leetcode.cn/problems/maximum-score-after-splitting-a-string/",
45604560
"path": "./problemset/maximum-score-after-splitting-a-string/README.md"
45614561
},
4562+
{
4563+
"id": "1441",
4564+
"title": {
4565+
"cn": "用栈操作构建数组",
4566+
"en": "build-an-array-with-stack-operations"
4567+
},
4568+
"difficulty": "中等",
4569+
"url": "https://leetcode.cn/problems/build-an-array-with-stack-operations/",
4570+
"path": "./problemset/build-an-array-with-stack-operations/README.md"
4571+
},
45624572
{
45634573
"id": "1447",
45644574
"title": {

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@
457457
| 1342. [将数字变成 0 的操作次数](../../problemset/number-of-steps-to-reduce-a-number-to-zero/README.md) | 简单 |
458458
| 1374. [生成每种字符都是奇数个的字符串](../../problemset/generate-a-string-with-characters-that-have-odd-counts/README.md) | 简单 |
459459
| 1422. [分割字符串的最大得分](../../problemset/maximum-score-after-splitting-a-string/README.md) | 简单 |
460+
| 1441. [用栈操作构建数组](../../problemset/build-an-array-with-stack-operations/README.md) | 中等 |
460461
| 1464. [数组中的两元素的最大乘积](../../problemset/maximum-product-of-two-elements-in-an-array/README.md) | 简单 |
461462
| 1470. [重新排序数组](../../problemset/shuffle-the-array/README.md) | 简单 |
462463
| 1582. [二进制矩阵中的特殊位置](../../problemset/special-positions-in-a-binary-matrix/README.md) | 简单 |

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,8 @@
912912

913913
[1422. 分割字符串的最大得分](../../problemset/maximum-score-after-splitting-a-string/README.md)
914914

915+
[1441. 用栈操作构建数组](../../problemset/build-an-array-with-stack-operations/README.md)
916+
915917
[1447. 最简分数](../../problemset/simplified-fractions/README.md)
916918

917919
[1450. 在既定时间做作业的学生人数](../../problemset/number-of-students-doing-homework-at-a-given-time/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 用栈操作构建数组
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode.cn/problems/build-an-array-with-stack-operations/
6+
7+
## 题目
8+
9+
给你一个数组 `target` 和一个整数 `n`。每次迭代,需要从  `list = { 1 , 2 , 3 ..., n }` 中依次读取一个数字。
10+
11+
请使用下述操作来构建目标数组 `target`
12+
13+
- `"Push"`:从 `list` 中读取一个新元素, 并将其推入数组中。
14+
- `"Pop"`:删除数组中的最后一个元素。
15+
- 如果目标数组构建完成,就停止读取更多元素。
16+
17+
题目数据保证目标数组严格递增,并且只包含 `1``n` 之间的数字。
18+
19+
请返回构建目标数组所用的操作序列。如果存在多个可行方案,返回任一即可。
20+
21+
### 示例
22+
23+
#### 示例 1:
24+
25+
```
26+
输入:target = [1,3], n = 3
27+
输出:["Push","Push","Pop","Push"]
28+
解释:
29+
读取 1 并自动推入数组 -> [1]
30+
读取 2 并自动推入数组,然后删除它 -> [1]
31+
读取 3 并自动推入数组 -> [1,3]
32+
```
33+
34+
#### 示例 2:
35+
36+
```
37+
输入:target = [1,2,3], n = 3
38+
输出:["Push","Push","Push"]
39+
```
40+
41+
#### 示例 3:
42+
43+
```
44+
输入:target = [1,2], n = 4
45+
输出:["Push","Push"]
46+
解释:只需要读取前 2 个数字就可以停止。
47+
```
48+
49+
## 解题
50+
51+
```ts
52+
/**
53+
* 模拟
54+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
55+
* @param target
56+
* @param n
57+
* @returns
58+
*/
59+
export function buildArray(target: number[], n: number): string[] {
60+
const res = []
61+
let prev = 0
62+
for (const number of target) {
63+
for (let i = 0; i < number - prev - 1; i++) {
64+
res.push('Push')
65+
res.push('Pop')
66+
}
67+
res.push('Push')
68+
prev = number
69+
}
70+
return res
71+
}
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { buildArray } from '.'
3+
4+
describe('用栈操作构建数组', () => {
5+
testCase(buildArray)
6+
})
7+
8+
function testCase(fn: typeof buildArray) {
9+
it.each([
10+
[
11+
[1, 3], 3, ['Push', 'Push', 'Pop', 'Push'],
12+
],
13+
[
14+
[1, 2, 3], 3, ['Push', 'Push', 'Push'],
15+
],
16+
[
17+
[1, 2], 4, ['Push', 'Push'],
18+
],
19+
])('示例%#', (target, n, expected) => {
20+
expect(fn(target, n)).toStrictEqual(expected)
21+
})
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
/**
3+
* 模拟
4+
* @desc 时间复杂度 O(N) 空间复杂度 O(1)
5+
* @param target
6+
* @param n
7+
* @returns
8+
*/
9+
export function buildArray(target: number[], n: number): string[] {
10+
const res = []
11+
let prev = 0
12+
for (const number of target) {
13+
for (let i = 0; i < number - prev - 1; i++) {
14+
res.push('Push')
15+
res.push('Pop')
16+
}
17+
res.push('Push')
18+
prev = number
19+
}
20+
return res
21+
}

0 commit comments

Comments
 (0)