Skip to content

Commit bf4634d

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 649daeb + fc9ce90 commit bf4634d

File tree

56 files changed

+1791
-427
lines changed

Some content is hidden

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

56 files changed

+1791
-427
lines changed

solution/0400-0499/0413.Arithmetic Slices/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0413.Ar
55
tags:
66
- 数组
77
- 动态规划
8+
- 滑动窗口
89
---
910

1011
<!-- problem:start -->

solution/0400-0499/0413.Arithmetic Slices/README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/0400-0499/0413.Ar
55
tags:
66
- Array
77
- Dynamic Programming
8+
- Sliding Window
89
---
910

1011
<!-- problem:start -->

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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,4 +716,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
716716

717717
<!-- solution:end -->
718718

719+
<!-- solution:start -->
720+
721+
### Solution 3: Line Sweep
722+
723+
<!-- tabs:start -->
724+
725+
#### TypeScript
726+
727+
```ts
728+
class MyCalendarTwo {
729+
#OVERLAPS = 2;
730+
#cnt: Record<number, number> = {};
731+
732+
book(start: number, end: number): boolean {
733+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
734+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
735+
736+
let sum = 0;
737+
for (const v of Object.values(this.#cnt)) {
738+
sum += v;
739+
if (sum > this.#OVERLAPS) {
740+
this.#cnt[start]--;
741+
this.#cnt[end]++;
742+
743+
if (!this.#cnt[start]) delete this.#cnt[start];
744+
if (!this.#cnt[end]) delete this.#cnt[end];
745+
746+
return false;
747+
}
748+
}
749+
750+
return true;
751+
}
752+
}
753+
```
754+
755+
#### JavaScript
756+
757+
```js
758+
class MyCalendarTwo {
759+
#OVERLAPS = 2;
760+
#cnt = {};
761+
762+
book(start, end) {
763+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
764+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
765+
766+
let sum = 0;
767+
for (const v of Object.values(this.#cnt)) {
768+
sum += v;
769+
if (sum > this.#OVERLAPS) {
770+
this.#cnt[start]--;
771+
this.#cnt[end]++;
772+
773+
if (!this.#cnt[start]) delete this.#cnt[start];
774+
if (!this.#cnt[end]) delete this.#cnt[end];
775+
776+
return false;
777+
}
778+
}
779+
780+
return true;
781+
}
782+
}
783+
```
784+
785+
<!-- tabs:end -->
786+
787+
<!-- solution:end -->
788+
719789
<!-- problem:end -->

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,74 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
698698

699699
<!-- solution:end -->
700700

701+
<!-- solution:start -->
702+
703+
### Solution 3: Line Sweep
704+
705+
<!-- tabs:start -->
706+
707+
#### TypeScript
708+
709+
```ts
710+
class MyCalendarTwo {
711+
#OVERLAPS = 2;
712+
#cnt: Record<number, number> = {};
713+
714+
book(start: number, end: number): boolean {
715+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
716+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
717+
718+
let sum = 0;
719+
for (const v of Object.values(this.#cnt)) {
720+
sum += v;
721+
if (sum > this.#OVERLAPS) {
722+
this.#cnt[start]--;
723+
this.#cnt[end]++;
724+
725+
if (!this.#cnt[start]) delete this.#cnt[start];
726+
if (!this.#cnt[end]) delete this.#cnt[end];
727+
728+
return false;
729+
}
730+
}
731+
732+
return true;
733+
}
734+
}
735+
```
736+
737+
#### JavaScript
738+
739+
```js
740+
class MyCalendarTwo {
741+
#OVERLAPS = 2;
742+
#cnt = {};
743+
744+
book(start, end) {
745+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
746+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
747+
748+
let sum = 0;
749+
for (const v of Object.values(this.#cnt)) {
750+
sum += v;
751+
if (sum > this.#OVERLAPS) {
752+
this.#cnt[start]--;
753+
this.#cnt[end]++;
754+
755+
if (!this.#cnt[start]) delete this.#cnt[start];
756+
if (!this.#cnt[end]) delete this.#cnt[end];
757+
758+
return false;
759+
}
760+
}
761+
762+
return true;
763+
}
764+
}
765+
```
766+
767+
<!-- tabs:end -->
768+
769+
<!-- solution:end -->
770+
701771
<!-- problem:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MyCalendarTwo {
2+
#OVERLAPS = 2;
3+
#cnt = {};
4+
5+
book(start, end) {
6+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
7+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
8+
9+
let sum = 0;
10+
for (const v of Object.values(this.#cnt)) {
11+
sum += v;
12+
if (sum > this.#OVERLAPS) {
13+
this.#cnt[start]--;
14+
this.#cnt[end]++;
15+
16+
if (!this.#cnt[start]) delete this.#cnt[start];
17+
if (!this.#cnt[end]) delete this.#cnt[end];
18+
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class MyCalendarTwo {
2+
#OVERLAPS = 2;
3+
#cnt: Record<number, number> = {};
4+
5+
book(start: number, end: number): boolean {
6+
this.#cnt[start] = (this.#cnt[start] ?? 0) + 1;
7+
this.#cnt[end] = (this.#cnt[end] ?? 0) - 1;
8+
9+
let sum = 0;
10+
for (const v of Object.values(this.#cnt)) {
11+
sum += v;
12+
if (sum > this.#OVERLAPS) {
13+
this.#cnt[start]--;
14+
this.#cnt[end]++;
15+
16+
if (!this.#cnt[start]) delete this.#cnt[start];
17+
if (!this.#cnt[end]) delete this.#cnt[end];
18+
19+
return false;
20+
}
21+
}
22+
23+
return true;
24+
}
25+
}

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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)