Skip to content

Commit be9d7c6

Browse files
authored
feat: update solutions to lc problem: No.1504 (doocs#3578)
1 parent a0bf608 commit be9d7c6

File tree

13 files changed

+122
-22
lines changed

13 files changed

+122
-22
lines changed

solution/0600-0699/0621.Task Scheduler/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ tags:
5353

5454
<p><strong>示例 3:</strong></p>
5555

56-
<div class="example-block"><strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 0</div>
56+
<div class="example-block"><strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 3</div>
5757

58-
<div class="example-block"><strong>输出:</strong>6</div>
58+
<div class="example-block"><strong>输出:</strong>10</div>
5959

6060
<div class="example-block"><strong>解释:</strong>一种可能的序列为:A -&gt; B -&gt; idle -&gt; idle -&gt; A -&gt; B -&gt; idle -&gt; idle -&gt; A -&gt; B。</div>
6161

solution/0700-0799/0731.My Calendar II/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ MyCalendar.book(10, 40); // returns true
4343
MyCalendar.book(5, 15); // returns false
4444
MyCalendar.book(5, 10); // returns true
4545
MyCalendar.book(25, 55); // returns true
46-
<strong>解释:</strong>
46+
<strong>解释:</strong>
4747
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
4848
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
4949
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。

solution/0700-0799/0731.My Calendar II/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ tags:
4646

4747
<strong>Explanation</strong>
4848
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
49-
myCalendarTwo.book(10, 20); // return True, The event can be booked.
50-
myCalendarTwo.book(50, 60); // return True, The event can be booked.
51-
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
49+
myCalendarTwo.book(10, 20); // return True, The event can be booked.
50+
myCalendarTwo.book(50, 60); // return True, The event can be booked.
51+
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
5252
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
5353
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
5454
myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.

solution/0900-0999/0908.Smallest Range I/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tags:
1919

2020
<p>给你一个整数数组 <code>nums</code>,和一个整数 <code>k</code> 。</p>
2121

22-
<p>在一个操作中,您可以选择 <code>0 &lt;= i &lt; nums.length</code> 的任何索引 <code>i</code> 。将 <code>nums[i]</code> 改为 <code>nums[i] + x</code> ,其中 <code>x</code> 是一个范围为 <code>[-k, k]</code> 的整数。对于每个索引 <code>i</code> ,最多 <strong>只能 </strong>应用 <strong>一次</strong> 此操作。</p>
22+
<p>在一个操作中,您可以选择 <code>0 &lt;= i &lt; nums.length</code> 的任何索引 <code>i</code> 。将 <code>nums[i]</code> 改为 <code>nums[i] + x</code> ,其中 <code>x</code> 是一个范围为 <code>[-k, k]</code> 的任意整数。对于每个索引 <code>i</code> ,最多 <strong>只能 </strong>应用 <strong>一次</strong> 此操作。</p>
2323

2424
<p><code>nums</code>&nbsp;&nbsp;<strong>分数&nbsp;</strong>是&nbsp;<code>nums</code>&nbsp;中最大和最小元素的差值。&nbsp;</p>
2525

solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ tags:
9090

9191
题目关键点在于两只蚂蚁相遇,然后分别调转方向的情况,实际上相当于两只蚂蚁继续往原来的方向移动。因此,我们只需要求出所有蚂蚁中最远的那只蚂蚁的移动距离即可。
9292

93-
注意 $left$ 和 $right$ 数组的长度可能为 $0$。
93+
注意 $\textit{left}$ 和 $\textit{right}$ 数组的长度可能为 $0$。
9494

95-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为木板的长度
95+
时间复杂度 $O(n)$,其中 $n$ 为木板的长度。空间复杂度 $O(1)$。
9696

9797
<!-- tabs:start -->
9898

solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ The last moment when an ant was on the plank is t = 4 seconds. After that, it fa
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Brain Teaser
81+
82+
The key point of the problem is that when two ants meet and then turn around, it is equivalent to the two ants continuing to move in their original directions. Therefore, we only need to find the maximum distance moved by any ant.
83+
84+
Note that the lengths of the $\textit{left}$ and $\textit{right}$ arrays may be $0$.
85+
86+
The time complexity is $O(n)$, where $n$ is the length of the plank. The space complexity is $O(1)$.
8187

8288
<!-- tabs:start -->
8389

solution/1500-1599/1504.Count Submatrices With All Ones/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,37 @@ func numSubmat(mat [][]int) (ans int) {
197197
}
198198
```
199199

200+
#### TypeScript
201+
202+
```ts
203+
function numSubmat(mat: number[][]): number {
204+
const m = mat.length;
205+
const n = mat[0].length;
206+
const g: number[][] = Array.from({ length: m }, () => Array(n).fill(0));
207+
208+
for (let i = 0; i < m; i++) {
209+
for (let j = 0; j < n; j++) {
210+
if (mat[i][j]) {
211+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
212+
}
213+
}
214+
}
215+
216+
let ans = 0;
217+
for (let i = 0; i < m; i++) {
218+
for (let j = 0; j < n; j++) {
219+
let col = Infinity;
220+
for (let k = i; k >= 0; k--) {
221+
col = Math.min(col, g[k][j]);
222+
ans += col;
223+
}
224+
}
225+
}
226+
227+
return ans;
228+
}
229+
```
230+
200231
<!-- tabs:end -->
201232

202233
<!-- solution:end -->

solution/1500-1599/1504.Count Submatrices With All Ones/README_EN.md

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ tags:
3030
<pre>
3131
<strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]
3232
<strong>Output:</strong> 13
33-
<strong>Explanation:</strong>
33+
<strong>Explanation:</strong>
3434
There are 6 rectangles of side 1x1.
3535
There are 2 rectangles of side 1x2.
3636
There are 3 rectangles of side 2x1.
37-
There is 1 rectangle of side 2x2.
37+
There is 1 rectangle of side 2x2.
3838
There is 1 rectangle of side 3x1.
3939
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
4040
</pre>
@@ -44,14 +44,14 @@ Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
4444
<pre>
4545
<strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
4646
<strong>Output:</strong> 24
47-
<strong>Explanation:</strong>
47+
<strong>Explanation:</strong>
4848
There are 8 rectangles of side 1x1.
4949
There are 5 rectangles of side 1x2.
50-
There are 2 rectangles of side 1x3.
50+
There are 2 rectangles of side 1x3.
5151
There are 4 rectangles of side 2x1.
52-
There are 2 rectangles of side 2x2.
53-
There are 2 rectangles of side 3x1.
54-
There is 1 rectangle of side 3x2.
52+
There are 2 rectangles of side 2x2.
53+
There are 2 rectangles of side 3x1.
54+
There is 1 rectangle of side 3x2.
5555
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
5656
</pre>
5757

@@ -69,7 +69,13 @@ Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Enumeration + Prefix Sum
73+
74+
We can enumerate the bottom-right corner $(i, j)$ of the matrix, and then enumerate the first row $k$ upwards. The width of the matrix with $(i, j)$ as the bottom-right corner in each row is $\min_{k \leq i} \textit{g}[k][j]$, where $\textit{g}[k][j]$ represents the width of the matrix with $(k, j)$ as the bottom-right corner in the $k$-th row.
75+
76+
Therefore, we can preprocess a 2D array $g[i][j]$, where $g[i][j]$ represents the number of consecutive $1$s from the $j$-th column to the left in the $i$-th row.
77+
78+
The time complexity is $O(m^2 \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.
7379

7480
<!-- tabs:start -->
7581

@@ -184,6 +190,37 @@ func numSubmat(mat [][]int) (ans int) {
184190
}
185191
```
186192

193+
#### TypeScript
194+
195+
```ts
196+
function numSubmat(mat: number[][]): number {
197+
const m = mat.length;
198+
const n = mat[0].length;
199+
const g: number[][] = Array.from({ length: m }, () => Array(n).fill(0));
200+
201+
for (let i = 0; i < m; i++) {
202+
for (let j = 0; j < n; j++) {
203+
if (mat[i][j]) {
204+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
205+
}
206+
}
207+
}
208+
209+
let ans = 0;
210+
for (let i = 0; i < m; i++) {
211+
for (let j = 0; j < n; j++) {
212+
let col = Infinity;
213+
for (let k = i; k >= 0; k--) {
214+
col = Math.min(col, g[k][j]);
215+
ans += col;
216+
}
217+
}
218+
}
219+
220+
return ans;
221+
}
222+
```
223+
187224
<!-- tabs:end -->
188225

189226
<!-- solution:end -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function numSubmat(mat: number[][]): number {
2+
const m = mat.length;
3+
const n = mat[0].length;
4+
const g: number[][] = Array.from({ length: m }, () => Array(n).fill(0));
5+
6+
for (let i = 0; i < m; i++) {
7+
for (let j = 0; j < n; j++) {
8+
if (mat[i][j]) {
9+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
10+
}
11+
}
12+
}
13+
14+
let ans = 0;
15+
for (let i = 0; i < m; i++) {
16+
for (let j = 0; j < n; j++) {
17+
let col = Infinity;
18+
for (let k = i; k >= 0; k--) {
19+
col = Math.min(col, g[k][j]);
20+
ans += col;
21+
}
22+
}
23+
}
24+
25+
return ans;
26+
}

solution/1900-1999/1971.Find if Path Exists in Graph/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<strong>输入:</strong>n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
3434
<strong>输出:</strong>true
3535
<strong>解释:</strong>存在由顶点 0 到顶点 2 的路径:
36-
- 0 → 1 → 2
36+
- 0 → 1 → 2
3737
- 0 → 2
3838
</pre>
3939

solution/2200-2299/2286.Booking Concert Tickets in Groups/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bms.gather(2, 0); // 返回 []
6464
bms.scatter(5, 1); // 返回 True
6565
// 这一组安排第 0 排第 4 个座位和第 1 排 [0, 3] 的座位。
6666
bms.scatter(5, 1); // 返回 False
67-
// 总共只剩下 2 个座位。
67+
// 总共只剩下 1 个座位。
6868
</pre>
6969

7070
<p>&nbsp;</p>

solution/CONTEST_README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ comments: true
6868
- [3272. 统计好整数的数目](/solution/3200-3299/3272.Find%20the%20Count%20of%20Good%20Integers/README.md)
6969
- [3273. 对 Bob 造成的最少伤害](/solution/3200-3299/3273.Minimum%20Amount%20of%20Damage%20Dealt%20to%20Bob/README.md)
7070

71-
#### 第 412 场周赛(2024-08-25 10:30, 90 分钟) 参赛人数 2681
71+
#### 第 412 场周赛(2024-08-25 10:30, 90 分钟) 参赛人数 2682
7272

7373
- [3264. K 次乘运算后的最终数组 I](/solution/3200-3299/3264.Final%20Array%20State%20After%20K%20Multiplication%20Operations%20I/README.md)
7474
- [3265. 统计近似相等数对 I](/solution/3200-3299/3265.Count%20Almost%20Equal%20Pairs%20I/README.md)

solution/contest.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)