Skip to content

Commit ccddd0c

Browse files
authored
Merge branch 'main' into main
2 parents ba248ed + b875726 commit ccddd0c

File tree

93 files changed

+29999
-26347
lines changed

Some content is hidden

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

93 files changed

+29999
-26347
lines changed

images/starcharts.svg

Lines changed: 25379 additions & 25274 deletions
Loading

solution/0500-0599/0552.Student Attendance Record II/README.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,30 @@ class Solution {
152152
#### C++
153153

154154
```cpp
155-
int f[100010][2][3];
156-
const int mod = 1e9 + 7;
157-
158155
class Solution {
159156
public:
160157
int checkRecord(int n) {
161-
this->n = n;
158+
int f[n][2][3];
162159
memset(f, -1, sizeof(f));
163-
return dfs(0, 0, 0);
164-
}
165-
166-
int dfs(int i, int j, int k) {
167-
if (i >= n) {
168-
return 1;
169-
}
170-
if (f[i][j][k] != -1) {
171-
return f[i][j][k];
172-
}
173-
int ans = dfs(i + 1, j, 0);
174-
if (j == 0) {
175-
ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
176-
}
177-
if (k < 2) {
178-
ans = (ans + dfs(i + 1, j, k + 1)) % mod;
179-
}
180-
return f[i][j][k] = ans;
160+
const int mod = 1e9 + 7;
161+
auto dfs = [&](auto&& dfs, int i, int j, int k) -> int {
162+
if (i >= n) {
163+
return 1;
164+
}
165+
if (f[i][j][k] != -1) {
166+
return f[i][j][k];
167+
}
168+
int ans = dfs(dfs, i + 1, j, 0);
169+
if (j == 0) {
170+
ans = (ans + dfs(dfs, i + 1, j + 1, 0)) % mod;
171+
}
172+
if (k < 2) {
173+
ans = (ans + dfs(dfs, i + 1, j, k + 1)) % mod;
174+
}
175+
return f[i][j][k] = ans;
176+
};
177+
return dfs(dfs, 0, 0, 0);
181178
}
182-
183-
private:
184-
int n;
185179
};
186180
```
187181

solution/0500-0599/0552.Student Attendance Record II/README_EN.md

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,36 +134,30 @@ class Solution {
134134
#### C++
135135

136136
```cpp
137-
int f[100010][2][3];
138-
const int mod = 1e9 + 7;
139-
140137
class Solution {
141138
public:
142139
int checkRecord(int n) {
143-
this->n = n;
140+
int f[n][2][3];
144141
memset(f, -1, sizeof(f));
145-
return dfs(0, 0, 0);
146-
}
147-
148-
int dfs(int i, int j, int k) {
149-
if (i >= n) {
150-
return 1;
151-
}
152-
if (f[i][j][k] != -1) {
153-
return f[i][j][k];
154-
}
155-
int ans = dfs(i + 1, j, 0);
156-
if (j == 0) {
157-
ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
158-
}
159-
if (k < 2) {
160-
ans = (ans + dfs(i + 1, j, k + 1)) % mod;
161-
}
162-
return f[i][j][k] = ans;
142+
const int mod = 1e9 + 7;
143+
auto dfs = [&](auto&& dfs, int i, int j, int k) -> int {
144+
if (i >= n) {
145+
return 1;
146+
}
147+
if (f[i][j][k] != -1) {
148+
return f[i][j][k];
149+
}
150+
int ans = dfs(dfs, i + 1, j, 0);
151+
if (j == 0) {
152+
ans = (ans + dfs(dfs, i + 1, j + 1, 0)) % mod;
153+
}
154+
if (k < 2) {
155+
ans = (ans + dfs(dfs, i + 1, j, k + 1)) % mod;
156+
}
157+
return f[i][j][k] = ans;
158+
};
159+
return dfs(dfs, 0, 0, 0);
163160
}
164-
165-
private:
166-
int n;
167161
};
168162
```
169163
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
int f[100010][2][3];
2-
const int mod = 1e9 + 7;
3-
41
class Solution {
52
public:
63
int checkRecord(int n) {
7-
this->n = n;
4+
int f[n][2][3];
85
memset(f, -1, sizeof(f));
9-
return dfs(0, 0, 0);
6+
const int mod = 1e9 + 7;
7+
auto dfs = [&](auto&& dfs, int i, int j, int k) -> int {
8+
if (i >= n) {
9+
return 1;
10+
}
11+
if (f[i][j][k] != -1) {
12+
return f[i][j][k];
13+
}
14+
int ans = dfs(dfs, i + 1, j, 0);
15+
if (j == 0) {
16+
ans = (ans + dfs(dfs, i + 1, j + 1, 0)) % mod;
17+
}
18+
if (k < 2) {
19+
ans = (ans + dfs(dfs, i + 1, j, k + 1)) % mod;
20+
}
21+
return f[i][j][k] = ans;
22+
};
23+
return dfs(dfs, 0, 0, 0);
1024
}
11-
12-
int dfs(int i, int j, int k) {
13-
if (i >= n) {
14-
return 1;
15-
}
16-
if (f[i][j][k] != -1) {
17-
return f[i][j][k];
18-
}
19-
int ans = dfs(i + 1, j, 0);
20-
if (j == 0) {
21-
ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
22-
}
23-
if (k < 2) {
24-
ans = (ans + dfs(i + 1, j, k + 1)) % mod;
25-
}
26-
return f[i][j][k] = ans;
27-
}
28-
29-
private:
30-
int n;
31-
};
25+
};

solution/0600-0699/0624.Maximum Distance in Arrays/README.md

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,48 @@ tags:
99

1010
<!-- problem:start -->
1111

12-
# [624. 数组列表中的最大距离 🔒](https://leetcode.cn/problems/maximum-distance-in-arrays)
12+
# [624. 数组列表中的最大距离](https://leetcode.cn/problems/maximum-distance-in-arrays)
1313

1414
[English Version](/solution/0600-0699/0624.Maximum%20Distance%20in%20Arrays/README_EN.md)
1515

1616
## 题目描述
1717

1818
<!-- description:start -->
1919

20-
<p>给定&nbsp;<code>m</code>&nbsp;个数组,每个数组都已经按照升序排好序了。现在你需要从两个不同的数组中选择两个整数(每个数组选一个)并且计算它们的距离。两个整数&nbsp;<code>a</code>&nbsp;&nbsp;<code>b</code>&nbsp;之间的距离定义为它们差的绝对值&nbsp;<code>|a-b|</code>&nbsp;。你的任务就是去找到最大距离</p>
20+
<p>给定&nbsp;<code>m</code>&nbsp;个数组,每个数组都已经按照升序排好序了。</p>
21+
22+
<p>现在你需要从两个不同的数组中选择两个整数(每个数组选一个)并且计算它们的距离。两个整数&nbsp;<code>a</code>&nbsp;&nbsp;<code>b</code>&nbsp;之间的距离定义为它们差的绝对值&nbsp;<code>|a-b|</code>&nbsp;。</p>
23+
24+
<p>返回最大距离。</p>
2125

2226
<p><strong>示例 1:</strong></p>
2327

24-
<pre><strong>输入:</strong>
25-
[[1,2,3],
26-
[4,5],
27-
[1,2,3]]
28-
<strong>输出:</strong> 4
28+
<pre>
29+
<strong>输入:</strong>[[1,2,3],[4,5],[1,2,3]]
30+
<strong>输出:</strong>4
2931
<strong>解释:</strong>
3032
一种得到答案 4 的方法是从第一个数组或者第三个数组中选择 1,同时从第二个数组中选择 5 。
3133
</pre>
3234

35+
<p><strong class="example">示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>arrays = [[1],[1]]
39+
<b>输出:</b>0
40+
</pre>
41+
3342
<p>&nbsp;</p>
3443

35-
<p><strong>注意:</strong></p>
44+
<p><strong>提示:</strong></p>
3645

37-
<ol>
38-
<li>每个给定数组至少会有 1 个数字。列表中至少有两个非空数组。</li>
39-
<li><strong>所有</strong>&nbsp;<code>m</code>&nbsp;个数组中的数字总数目在范围 [2, 10000] 内。</li>
40-
<li><code>m</code>&nbsp;个数组中所有整数的范围在 [-10000, 10000] 内。</li>
41-
</ol>
46+
<ul>
47+
<li><code>m == arrays.length</code></li>
48+
<li><code>2 &lt;= m &lt;= 10<sup>5</sup></code></li>
49+
<li><code>1 &lt;= arrays[i].length &lt;= 500</code></li>
50+
<li><code>-10<sup>4</sup> &lt;= arrays[i][j] &lt;= 10<sup>4</sup></code></li>
51+
<li><code>arrays[i]</code>&nbsp;以&nbsp;<strong>升序</strong>&nbsp;排序。</li>
52+
<li>所有数组中最多有&nbsp;<code>10<sup>5</sup></code> 个整数。</li>
53+
</ul>
4254

4355
<p>&nbsp;</p>
4456

@@ -136,6 +148,90 @@ func abs(x int) int {
136148
}
137149
```
138150

151+
#### TypeScript
152+
153+
```ts
154+
function maxDistance(arrays: number[][]): number {
155+
const n = arrays.length;
156+
let res = 0;
157+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
158+
159+
for (let i = 0; i < n; i++) {
160+
const a = arrays[i];
161+
res = Math.max(Math.max(a.at(-1)! - min, max - a[0]), res);
162+
min = Math.min(min, a[0]);
163+
max = Math.max(max, a.at(-1)!);
164+
}
165+
166+
return res;
167+
}
168+
```
169+
170+
#### JavaScript
171+
172+
```js
173+
/**
174+
* @param {number[][]} arrays
175+
* @return {number}
176+
*/
177+
var maxDistance = function (arrays) {
178+
const n = arrays.length;
179+
let res = 0;
180+
let [min, max] = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
181+
182+
for (let i = 0; i < n; i++) {
183+
const a = arrays[i];
184+
res = Math.max(Math.max(a.at(-1) - min, max - a[0]), res);
185+
min = Math.min(min, a[0]);
186+
max = Math.max(max, a.at(-1));
187+
}
188+
189+
return res;
190+
};
191+
```
192+
193+
<!-- tabs:end -->
194+
195+
<!-- solution:end -->
196+
197+
<!-- solution:start -->
198+
199+
### 方法二:一行
200+
201+
<!-- tabs:start -->
202+
203+
#### TypeScript
204+
205+
```ts
206+
const maxDistance = (arrays: number[][]): number =>
207+
arrays.reduce(
208+
([res, min, max], a) => [
209+
Math.max(Math.max(a.at(-1)! - min, max - a[0]), res),
210+
Math.min(min, a[0]),
211+
Math.max(max, a.at(-1)!),
212+
],
213+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
214+
)[0];
215+
```
216+
217+
#### JavaScript
218+
219+
```js
220+
/**
221+
* @param {number[][]} arrays
222+
* @return {number}
223+
*/
224+
var maxDistance = arrays =>
225+
arrays.reduce(
226+
([res, min, max], a) => [
227+
Math.max(Math.max(a.at(-1) - min, max - a[0]), res),
228+
Math.min(min, a[0]),
229+
Math.max(max, a.at(-1)),
230+
],
231+
[0, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
232+
)[0];
233+
```
234+
139235
<!-- tabs:end -->
140236

141237
<!-- solution:end -->

0 commit comments

Comments
 (0)