Skip to content

feat: update solutions to lc problem: No.1504 #3578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions solution/0600-0699/0621.Task Scheduler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ tags:

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

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

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

<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>

Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0731.My Calendar II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ MyCalendar.book(10, 40); // returns true
MyCalendar.book(5, 15); // returns false
MyCalendar.book(5, 10); // returns true
MyCalendar.book(25, 55); // returns true
<strong>解释:</strong>
<strong>解释:</strong>
前两个日程安排可以添加至日历中。 第三个日程安排会导致双重预订,但可以添加至日历中。
第四个日程安排活动(5,15)不能添加至日历中,因为它会导致三重预订。
第五个日程安排(5,10)可以添加至日历中,因为它未使用已经双重预订的时间10。
Expand Down
6 changes: 3 additions & 3 deletions solution/0700-0799/0731.My Calendar II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ tags:

<strong>Explanation</strong>
MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(10, 20); // return True, The event can be booked.
myCalendarTwo.book(50, 60); // return True, The event can be booked.
myCalendarTwo.book(10, 40); // return True, The event can be double booked.
myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
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.
Expand Down
2 changes: 1 addition & 1 deletion solution/0900-0999/0908.Smallest Range I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tags:

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

<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>
<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>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ tags:

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ The last moment when an ant was on the plank is t = 4 seconds. After that, it fa

<!-- solution:start -->

### Solution 1
### Solution 1: Brain Teaser

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.

Note that the lengths of the $\textit{left}$ and $\textit{right}$ arrays may be $0$.

The time complexity is $O(n)$, where $n$ is the length of the plank. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
31 changes: 31 additions & 0 deletions solution/1500-1599/1504.Count Submatrices With All Ones/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,37 @@ func numSubmat(mat [][]int) (ans int) {
}
```

#### TypeScript

```ts
function numSubmat(mat: number[][]): number {
const m = mat.length;
const n = mat[0].length;
const g: number[][] = Array.from({ length: m }, () => Array(n).fill(0));

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j]) {
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
}
}
}

let ans = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let col = Infinity;
for (let k = i; k >= 0; k--) {
col = Math.min(col, g[k][j]);
ans += col;
}
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ tags:
<pre>
<strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]
<strong>Output:</strong> 13
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are 6 rectangles of side 1x1.
There are 2 rectangles of side 1x2.
There are 3 rectangles of side 2x1.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 3x1.
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
</pre>
Expand All @@ -44,14 +44,14 @@ Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
<pre>
<strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
<strong>Output:</strong> 24
<strong>Explanation:</strong>
<strong>Explanation:</strong>
There are 8 rectangles of side 1x1.
There are 5 rectangles of side 1x2.
There are 2 rectangles of side 1x3.
There are 2 rectangles of side 1x3.
There are 4 rectangles of side 2x1.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.
</pre>

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

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration + Prefix Sum

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.

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.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -184,6 +190,37 @@ func numSubmat(mat [][]int) (ans int) {
}
```

#### TypeScript

```ts
function numSubmat(mat: number[][]): number {
const m = mat.length;
const n = mat[0].length;
const g: number[][] = Array.from({ length: m }, () => Array(n).fill(0));

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j]) {
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
}
}
}

let ans = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let col = Infinity;
for (let k = i; k >= 0; k--) {
col = Math.min(col, g[k][j]);
ans += col;
}
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function numSubmat(mat: number[][]): number {
const m = mat.length;
const n = mat[0].length;
const g: number[][] = Array.from({ length: m }, () => Array(n).fill(0));

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j]) {
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
}
}
}

let ans = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let col = Infinity;
for (let k = i; k >= 0; k--) {
col = Math.min(col, g[k][j]);
ans += col;
}
}
}

return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tags:
<strong>输入:</strong>n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
<strong>输出:</strong>true
<strong>解释:</strong>存在由顶点 0 到顶点 2 的路径:
- 0 → 1 → 2
- 0 → 1 → 2
- 0 → 2
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ bms.gather(2, 0); // 返回 []
bms.scatter(5, 1); // 返回 True
// 这一组安排第 0 排第 4 个座位和第 1 排 [0, 3] 的座位。
bms.scatter(5, 1); // 返回 False
// 总共只剩下 2 个座位。
// 总共只剩下 1 个座位。
</pre>

<p>&nbsp;</p>
Expand Down
2 changes: 1 addition & 1 deletion solution/CONTEST_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ comments: true
- [3272. 统计好整数的数目](/solution/3200-3299/3272.Find%20the%20Count%20of%20Good%20Integers/README.md)
- [3273. 对 Bob 造成的最少伤害](/solution/3200-3299/3273.Minimum%20Amount%20of%20Damage%20Dealt%20to%20Bob/README.md)

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

- [3264. K 次乘运算后的最终数组 I](/solution/3200-3299/3264.Final%20Array%20State%20After%20K%20Multiplication%20Operations%20I/README.md)
- [3265. 统计近似相等数对 I](/solution/3200-3299/3265.Count%20Almost%20Equal%20Pairs%20I/README.md)
Expand Down
2 changes: 1 addition & 1 deletion solution/contest.json

Large diffs are not rendered by default.

Loading