Skip to content

Commit 1f56b64

Browse files
committed
feat: add solutions to lc problems: No.2648~2654
* No.2648.Generate Fibonacci Sequence * No.2649.Nested Array Generator * No.2650.Design Cancellable Function * No.2651.Calculate Delayed Arrival Time * No.2652.Sum Multiples * No.2653.Sliding Subarray Beauty * No.2654.Minimum Number of Operations to Make All Array Elements Equal to 1
1 parent ed7bd4b commit 1f56b64

File tree

47 files changed

+2554
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2554
-4
lines changed

solution/2600-2699/2624.Snail Traversal/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>请你编写一段代码为所有数组实现&nbsp;&nbsp;<code>snail(rowsCount,colsCount)</code> 方法,该方法将 1D 数组转换为以蜗牛排序的模式的 2D 数组。无效的输入值应该输出一个空数组。当 <code>rowsCount * colsCount&nbsp;!==</code><code>nums</code>&nbsp;时。这个输入被认为是无效的。</p>
9+
<p>请你编写一段代码为所有数组实现&nbsp;&nbsp;<code>snail(rowsCount,colsCount)</code> 方法,该方法将 1D 数组转换为以蜗牛排序的模式的 2D 数组。无效的输入值应该输出一个空数组。当 <code>rowsCount * colsCount&nbsp;!==</code><code>nums.length</code>&nbsp;时。这个输入被认为是无效的。</p>
1010

1111
<p>蜗牛排序从左上角的单元格开始,从当前数组的第一个值开始。然后,它从上到下遍历第一列,接着移动到右边的下一列,并从下到上遍历它。将这种模式持续下去,每列交替变换遍历方向,直到覆盖整个数组。例如,当给定输入数组&nbsp;&nbsp;<code>[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]</code> ,当 <code>rowsCount = 5</code>&nbsp;&nbsp;<code>colsCount = 4</code> 时,需要输出矩阵如下图所示。注意,矩阵沿箭头方向对应于原数组中数字的顺序</p>
1212

solution/2600-2699/2624.Snail Traversal/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<p>Write code that enhances all arrays such that you can call the <code>snail(rowsCount, colsCount)</code> method that transforms the 1D&nbsp;array into&nbsp;a 2D array organised in&nbsp;the pattern known as <strong>snail traversal order</strong>. Invalid input values should output an empty array. If&nbsp;<code>rowsCount * colsCount !== nums.length</code>,&nbsp;the input is considered invalid.</p>
88

9-
<p><strong>Snail traversal order</strong><em>&nbsp;</em>starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array&nbsp;[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]<code> </code>with <code>rowsCount = 5</code> and <code>colsCount = 4</code>,&nbsp;the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.</p>
9+
<p><strong>Snail traversal order</strong><em>&nbsp;</em>starts at the top left cell with the first value of the current array. It then moves through the entire first column from top to bottom, followed by moving to the next column on the right and traversing it from bottom to top. This pattern continues, alternating the direction of traversal with each column, until the entire current array is covered. For example, when given the input array&nbsp;<code>[19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]</code> with <code>rowsCount = 5</code> and <code>colsCount = 4</code>,&nbsp;the desired output matrix is shown below. Note that iterating the matrix following the arrows corresponds to the order of numbers in the original array.</p>
1010

1111
<p>&nbsp;</p>
1212

solution/2600-2699/2633.Convert Object to JSON String/README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Primitive types are valid inputs.</pre>
5151
<li><code>object includes strings, integers, booleans, arrays, objects, and null</code></li>
5252
<li><code>1 &lt;= JSON.stringify(object).length &lt;= 10<sup>5</sup></code></li>
5353
<li><code>maxNestingLevel &lt;= 1000</code></li>
54+
<li><code>all strings will only contain alphanumeric characters</code></li>
5455
</ul>
5556

5657
## Solutions
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [2648. 生成斐波那契数列](https://leetcode.cn/problems/generate-fibonacci-sequence)
2+
3+
[English Version](/solution/2600-2699/2648.Generate%20Fibonacci%20Sequence/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>请你编写一个生成器函数,并返回一个可以生成 <strong>斐波那契数列</strong> 的生成器对象。</p>
10+
11+
<p><strong>斐波那契数列</strong> 的递推公式为 <code>X<sub>n</sub>&nbsp;= X<sub>n-1</sub>&nbsp;+ X<sub>n-2</sub></code> 。</p>
12+
13+
<p>这个数列的前几个数字是 <code>0, 1, 1, 2, 3, 5, 8, 13</code>&nbsp;。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre>
20+
<strong>输入:</strong>callCount = 5
21+
<b>输出:</b>[0,1,1,2,3]
22+
<strong>解释:</strong>
23+
const gen = fibGenerator();
24+
gen.next().value; // 0
25+
gen.next().value; // 1
26+
gen.next().value; // 1
27+
gen.next().value; // 2
28+
gen.next().value; // 3
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre>
34+
<b>输入:</b>callCount = 0
35+
<strong>输出:</strong>[]
36+
<b>解释:</b>gen.next() 永远不会被调用,所以什么也不会输出
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>0 &lt;= callCount &lt;= 50</code></li>
45+
</ul>
46+
47+
## 解法
48+
49+
<!-- 这里可写通用的实现逻辑 -->
50+
51+
<!-- tabs:start -->
52+
53+
### **TypeScript**
54+
55+
```ts
56+
function* fibGenerator(): Generator<number, any, number> {
57+
let a = 0;
58+
let b = 1;
59+
while (true) {
60+
yield a;
61+
[a, b] = [b, a + b];
62+
}
63+
}
64+
65+
/**
66+
* const gen = fibGenerator();
67+
* gen.next().value; // 0
68+
* gen.next().value; // 1
69+
*/
70+
```
71+
72+
### **...**
73+
74+
```
75+
76+
```
77+
78+
<!-- tabs:end -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [2648. Generate Fibonacci Sequence](https://leetcode.com/problems/generate-fibonacci-sequence)
2+
3+
[中文文档](/solution/2600-2699/2648.Generate%20Fibonacci%20Sequence/README.md)
4+
5+
## Description
6+
7+
<p>Write a generator function that returns a generator object which yields the&nbsp;<strong>fibonacci sequence</strong>.</p>
8+
9+
<p>The&nbsp;<strong>fibonacci sequence</strong>&nbsp;is defined by the relation <code>X<sub>n</sub>&nbsp;= X<sub>n-1</sub>&nbsp;+ X<sub>n-2</sub></code>.</p>
10+
11+
<p>The first few numbers&nbsp;of the series are <code>0, 1, 1, 2, 3, 5, 8, 13</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> callCount = 5
18+
<strong>Output:</strong> [0,1,1,2,3]
19+
<strong>Explanation:</strong>
20+
const gen = fibGenerator();
21+
gen.next().value; // 0
22+
gen.next().value; // 1
23+
gen.next().value; // 1
24+
gen.next().value; // 2
25+
gen.next().value; // 3
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> callCount = 0
32+
<strong>Output:</strong> []
33+
<strong>Explanation:</strong> gen.next() is never called so nothing is outputted
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>0 &lt;= callCount &lt;= 50</code></li>
41+
</ul>
42+
43+
## Solutions
44+
45+
<!-- tabs:start -->
46+
47+
### **TypeScript**
48+
49+
```ts
50+
function* fibGenerator(): Generator<number, any, number> {
51+
let a = 0;
52+
let b = 1;
53+
while (true) {
54+
yield a;
55+
[a, b] = [b, a + b];
56+
}
57+
}
58+
59+
/**
60+
* const gen = fibGenerator();
61+
* gen.next().value; // 0
62+
* gen.next().value; // 1
63+
*/
64+
```
65+
66+
### **...**
67+
68+
```
69+
70+
```
71+
72+
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function* fibGenerator(): Generator<number, any, number> {
2+
let a = 0;
3+
let b = 1;
4+
while (true) {
5+
yield a;
6+
[a, b] = [b, a + b];
7+
}
8+
}
9+
10+
/**
11+
* const gen = fibGenerator();
12+
* gen.next().value; // 0
13+
* gen.next().value; // 1
14+
*/
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2649. 嵌套数组生成器](https://leetcode.cn/problems/nested-array-generator)
2+
3+
[English Version](/solution/2600-2699/2649.Nested%20Array%20Generator/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>现给定一个整数的 <strong>多维数组</strong> ,请你返回一个生成器对象,按照&nbsp;<strong>中序遍历</strong> 的顺序逐个生成整数。</p>
10+
11+
<p><strong>多维数组</strong> 是一个递归数据结构,包含整数和其他 <strong>多维数组</strong>。</p>
12+
13+
<p><strong>中序遍历</strong> 是从左到右遍历每个数组,在遇到任何整数时生成它,遇到任何数组时递归应用 <strong>中序遍历</strong> 。</p>
14+
15+
<p>&nbsp;</p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<pre>
20+
<b>输入:</b>arr = [[[6]],[1,3],[]]
21+
<b>输出:</b>[6,1,3]
22+
<strong>解释:</strong>
23+
const generator = inorderTraversal(arr);
24+
generator.next().value; // 6
25+
generator.next().value; // 1
26+
generator.next().value; // 3
27+
generator.next().done; // true
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<b>输入:</b>arr = []
34+
<b>输出:</b>[]
35+
<b>解释:</b>输入的多维数组没有任何参数,所以生成器不需要生成任何值。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>0 &lt;= arr.flat().length &lt;= 10<sup>5</sup></code></li>
44+
<li><code>0 &lt;= arr.flat()[i]&nbsp;&lt;= 10<sup>5</sup></code></li>
45+
<li><code>maxNestingDepth &lt;= 10<sup>5</sup></code></li>
46+
</ul>
47+
48+
## 解法
49+
50+
<!-- 这里可写通用的实现逻辑 -->
51+
52+
<!-- tabs:start -->
53+
54+
### **TypeScript**
55+
56+
```ts
57+
type MultidimensionalArray = (MultidimensionalArray | number)[];
58+
59+
function* inorderTraversal(
60+
arr: MultidimensionalArray,
61+
): Generator<number, void, unknown> {
62+
for (const e of arr) {
63+
if (Array.isArray(e)) {
64+
yield* inorderTraversal(e);
65+
} else {
66+
yield e;
67+
}
68+
}
69+
}
70+
71+
/**
72+
* const gen = inorderTraversal([1, [2, 3]]);
73+
* gen.next().value; // 1
74+
* gen.next().value; // 2
75+
* gen.next().value; // 3
76+
*/
77+
```
78+
79+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [2649. Nested Array Generator](https://leetcode.com/problems/nested-array-generator)
2+
3+
[中文文档](/solution/2600-2699/2649.Nested%20Array%20Generator/README.md)
4+
5+
## Description
6+
7+
<p>Given a&nbsp;<strong>multi-dimensional array</strong> of integers, return&nbsp;a generator object which&nbsp;yields integers in the same order as&nbsp;<strong>inorder traversal</strong>.</p>
8+
9+
<p>A&nbsp;<strong>multi-dimensional array</strong>&nbsp;is a recursive data structure that contains both integers and other&nbsp;<strong>multi-dimensional arrays</strong>.</p>
10+
11+
<p><strong>inorder traversal</strong>&nbsp;iterates over&nbsp;each array from left to right, yielding any integers it encounters or applying&nbsp;<strong>inorder traversal</strong>&nbsp;to any arrays it encounters.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> arr = [[[6]],[1,3],[]]
18+
<strong>Output:</strong> [6,1,3]
19+
<strong>Explanation:</strong>
20+
const generator = inorderTraversal(arr);
21+
generator.next().value; // 6
22+
generator.next().value; // 1
23+
generator.next().value; // 3
24+
generator.next().done; // true
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> arr = []
31+
<strong>Output:</strong> []
32+
<strong>Explanation:</strong> There are no integers so the generator doesn&#39;t yield anything.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>0 &lt;= arr.flat().length &lt;= 10<sup>5</sup></code></li>
40+
<li><code>0 &lt;= arr.flat()[i]&nbsp;&lt;= 10<sup>5</sup></code></li>
41+
<li><code>maxNestingDepth &lt;= 10<sup>5</sup></code></li>
42+
</ul>
43+
44+
## Solutions
45+
46+
<!-- tabs:start -->
47+
48+
### **TypeScript**
49+
50+
```ts
51+
type MultidimensionalArray = (MultidimensionalArray | number)[];
52+
53+
function* inorderTraversal(
54+
arr: MultidimensionalArray,
55+
): Generator<number, void, unknown> {
56+
for (const e of arr) {
57+
if (Array.isArray(e)) {
58+
yield* inorderTraversal(e);
59+
} else {
60+
yield e;
61+
}
62+
}
63+
}
64+
65+
/**
66+
* const gen = inorderTraversal([1, [2, 3]]);
67+
* gen.next().value; // 1
68+
* gen.next().value; // 2
69+
* gen.next().value; // 3
70+
*/
71+
```
72+
73+
### **...**
74+
75+
```
76+
77+
```
78+
79+
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type MultidimensionalArray = (MultidimensionalArray | number)[];
2+
3+
function* inorderTraversal(
4+
arr: MultidimensionalArray,
5+
): Generator<number, void, unknown> {
6+
for (const e of arr) {
7+
if (Array.isArray(e)) {
8+
yield* inorderTraversal(e);
9+
} else {
10+
yield e;
11+
}
12+
}
13+
}
14+
15+
/**
16+
* const gen = inorderTraversal([1, [2, 3]]);
17+
* gen.next().value; // 1
18+
* gen.next().value; // 2
19+
* gen.next().value; // 3
20+
*/

0 commit comments

Comments
 (0)