Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 576142f

Browse files
feat: add solutions to lc problem: No.0836 (doocs#3940)
No.0836.Rectangle Overlap
1 parent 110e86a commit 576142f

File tree

11 files changed

+86
-20
lines changed

11 files changed

+86
-20
lines changed

‎solution/0800-0899/0831.Masking Personal Information/README_EN.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ Thus, the resulting masked number is "***-***-7890".
116116

117117
<!-- solution:start -->
118118

119-
### Solution 1
119+
### Solution 1: Simulation
120+
121+
According to the problem description, we can first determine whether the string $s$ is an email or a phone number, and then handle it accordingly.
122+
123+
The time complexity is $O(n),ドル and the space complexity is $O(n),ドル where $n$ is the length of the string $s$.
120124

121125
<!-- tabs:start -->
122126

‎solution/0800-0899/0832.Flipping an Image/README.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ tags:
7575

7676
### 方法一:双指针
7777

78-
我们可以遍历矩阵,对于遍历到的每一行 $row$:
78+
我们可以遍历矩阵,对于遍历到的每一行 $\textit{row},ドル我们使用双指针 $i$ 和 $j$ 分别指向该行的首尾元素,如果 $\textit{row}[i] = \textit{row}[j],ドル交换后两者的值仍然保持不变,因此,我们只需要对 $\textit{row}[i]$ 和 $\textit{row}[j]$ 进行异或反转即可,然后将 $i$ 和 $j$ 分别向中间移动一位,直到 $i \geq j$。如果 $\textit{row}[i] \neq \textit{row}[j],ドル此时交换后再反转两者的值,仍然保持不变,因此,可以不进行任何操作。
7979

80-
我们使用双指针 $i$ 和 $j$ 分别指向该行的首尾元素,如果 $row[i] = row[j],ドル交换后两者的值仍然保持不变,因此,我们只需要对 $row[i]$ 和 $row[j]$ 进行异或反转即可,然后将 $i$ 和 $j$ 分别向中间移动一位,直到 $i \geq j$。如果 $row[i] \neq row[j],ドル此时交换后再反转两者的值,仍然保持不变,因此,可以不进行任何操作。
81-
82-
最后,如果 $i = j,ドル我们直接对 $row[i]$ 进行反转即可。
80+
最后,如果 $i = j,ドル我们直接对 $\textit{row}[i]$ 进行反转即可。
8381

8482
时间复杂度 $O(n^2),ドル其中 $n$ 是矩阵的行数或列数。空间复杂度 $O(1)$。
8583

‎solution/0800-0899/0832.Flipping an Image/README_EN.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
6969

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

72-
### Solution 1
72+
### Solution 1: Two Pointers
73+
74+
We can traverse the matrix, and for each row $\textit{row},ドル we use two pointers $i$ and $j$ pointing to the first and last elements of the row, respectively. If $\textit{row}[i] = \textit{row}[j],ドル swapping them will keep their values unchanged, so we only need to XOR invert $\textit{row}[i]$ and $\textit{row}[j],ドル then move $i$ and $j$ one position towards the center until $i \geq j$. If $\textit{row}[i] \neq \textit{row}[j],ドル swapping and then inverting their values will also keep them unchanged, so no operation is needed.
75+
76+
Finally, if $i = j,ドル we directly invert $\textit{row}[i]$.
77+
78+
The time complexity is $O(n^2),ドル where $n$ is the number of rows or columns in the matrix. The space complexity is $O(1)$.
7379

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

‎solution/0800-0899/0833.Find And Replace in String/README.md‎

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

8888
### 方法一:模拟
8989

90-
我们遍历每个替换操作,对于当前第 $k$ 个替换操作 $(i, src),ドル如果 $s[i..i+|src|-1]$ 与 $src$ 相等,此时我们记录下标 $i$ 处需要替换的是 $targets$ 的第 $k$ 个字符串,否则不需要替换。
90+
我们遍历每个替换操作,对于当前第 $k$ 个替换操作 $(i, \text{src}),ドル如果 $s[i..i+|\text{src}|-1]$ 与 $\text{src}$ 相等,此时我们记录下标 $i$ 处需要替换的是 $\text{targets}$ 的第 $k$ 个字符串,否则不需要替换。
9191

9292
接下来,我们只需要遍历原字符串 $s,ドル根据记录的信息进行替换即可。
9393

‎solution/0800-0899/0833.Find And Replace in String/README_EN.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ tags:
8181

8282
<!-- solution:start -->
8383

84-
### Solution 1
84+
### Solution 1: Simulation
85+
86+
We iterate through each replacement operation. For the current $k$-th replacement operation $(i, \text{src}),ドル if $s[i..i+|\text{src}|-1]$ is equal to $\text{src},ドル we record that the string at index $i$ needs to be replaced with the $k$-th string in $\text{targets}$; otherwise, no replacement is needed.
87+
88+
Next, we only need to iterate through the original string $s$ and perform the replacements based on the recorded information.
89+
90+
The time complexity is $O(L),ドル and the space complexity is $O(n),ドル where $L$ is the sum of the lengths of all strings, and $n$ is the length of the string $s$.
8591

8692
<!-- tabs:start -->
8793

‎solution/0800-0899/0834.Sum of Distances in Tree/README_EN.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ Hence, answer[0] = 8, and so on.
6969

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

72-
### Solution 1
72+
### Solution 1: Tree DP (Re-rooting)
73+
74+
First, we run a DFS to calculate the size of each node's subtree, recorded in the array $size,ドル and compute the sum of distances from node 0ドル$ to all other nodes, recorded in $ans[0]$.
75+
76+
Next, we run another DFS to enumerate the sum of distances from each node when it is considered as the root. Suppose the answer for the current node $i$ is $t$. When we move from node $i$ to node $j,ドル the sum of distances changes to $t - size[j] + n - size[j],ドル meaning the sum of distances to node $j$ and its subtree nodes decreases by $size[j],ドル while the sum of distances to other nodes increases by $n - size[j]$.
77+
78+
The time complexity is $O(n),ドル and the space complexity is $O(n),ドル where $n$ is the number of nodes in the tree.
79+
80+
Similar problems:
81+
82+
- [2581. Count Number of Possible Root Nodes](https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2581.Count%20Number%20of%20Possible%20Root%20Nodes/README_EN.md)
7383

7484
<!-- tabs:start -->
7585

‎solution/0800-0899/0835.Image Overlap/README.md‎

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

7777
### 方法一:枚举
7878

79-
我们可以枚举 $img1$ 和 $img2$ 的每个 1ドル$ 的位置,分别记为 $(i, j)$ 和 $(h, k)$。然后我们计算得到偏移量 $(i - h, j - k),ドル记为 $(dx, dy),ドル用哈希表 $cnt$ 记录每个偏移量出现的次数。最后我们遍历哈希表 $cnt,ドル找到出现次数最多的偏移量,即为答案。
79+
我们可以枚举 $\textit{img1}$ 和 $\textit{img2}$ 的每个 1ドル$ 的位置,分别记为 $(i, j)$ 和 $(h, k)$。然后我们计算得到偏移量 $(i - h, j - k),ドル记为 $(dx, dy),ドル用哈希表 $\textit{cnt}$ 记录每个偏移量出现的次数。最后我们遍历哈希表 $\textit{cnt},ドル找到出现次数最多的偏移量,即为答案。
8080

81-
时间复杂度 $O(n^4),ドル空间复杂度 $O(n^2)$。其中 $n$ 是 $img1$ 的边长。
81+
时间复杂度 $O(n^4),ドル空间复杂度 $O(n^2)$。其中 $n$ 是 $\textit{img1}$ 的边长。
8282

8383
<!-- tabs:start -->
8484

‎solution/0800-0899/0835.Image Overlap/README_EN.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ The number of positions that have a 1 in both images is 3 (shown in red).
6868

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

71-
### Solution 1
71+
### Solution 1: Enumeration
72+
73+
We can enumerate each position of 1ドル$ in $\textit{img1}$ and $\textit{img2},ドル denoted as $(i, j)$ and $(h, k)$ respectively. Then we calculate the offset $(i - h, j - k),ドル denoted as $(dx, dy),ドル and use a hash table $\textit{cnt}$ to record the number of occurrences of each offset. Finally, we traverse the hash table $\textit{cnt}$ to find the offset that appears the most, which is the answer.
74+
75+
The time complexity is $O(n^4),ドル and the space complexity is $O(n^2),ドル where $n$ is the side length of $\textit{img1}$.
7276

7377
<!-- tabs:start -->
7478

‎solution/0800-0899/0836.Rectangle Overlap/README.md‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ tags:
6565

6666
### 方法一:判断不重叠的情况
6767

68-
我们记矩形 $rec1$ 的坐标点为 $(x_1, y_1, x_2, y_2),ドル矩形 $rec2$ 的坐标点为 $(x_3, y_3, x_4, y_4)$。
68+
我们记矩形 $\text{rec1}$ 的坐标点为 $(x_1, y_1, x_2, y_2),ドル矩形 $\text{rec2}$ 的坐标点为 $(x_3, y_3, x_4, y_4)$。
6969

70-
那么当满足以下任一条件时,矩形 $rec1$ 和 $rec2$ 不重叠:
70+
那么当满足以下任一条件时,矩形 $\text{rec1}$ 和 $\text{rec2}$ 不重叠:
7171

72-
- 满足 $y_3 \geq y_2,ドル即 $rec2$ 在 $rec1$ 的上方;
73-
- 满足 $y_4 \leq y_1,ドル即 $rec2$ 在 $rec1$ 的下方;
74-
- 满足 $x_3 \geq x_2,ドル即 $rec2$ 在 $rec1$ 的右方;
75-
- 满足 $x_4 \leq x_1,ドル即 $rec2$ 在 $rec1$ 的左方。
72+
- 满足 $y_3 \geq y_2,ドル即 $\text{rec2}$ 在 $\text{rec1}$ 的上方;
73+
- 满足 $y_4 \leq y_1,ドル即 $\text{rec2}$ 在 $\text{rec1}$ 的下方;
74+
- 满足 $x_3 \geq x_2,ドル即 $\text{rec2}$ 在 $\text{rec1}$ 的右方;
75+
- 满足 $x_4 \leq x_1,ドル即 $\text{rec2}$ 在 $\text{rec1}$ 的左方。
7676

77-
当以上条件都不满足时,矩形 $rec1$ 和 $rec2$ 重叠。
77+
当以上条件都不满足时,矩形 $\text{rec1}$ 和 $\text{rec2}$ 重叠。
7878

7979
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
8080

@@ -125,6 +125,16 @@ func isRectangleOverlap(rec1 []int, rec2 []int) bool {
125125
}
126126
```
127127

128+
#### TypeScript
129+
130+
```ts
131+
function isRectangleOverlap(rec1: number[], rec2: number[]): boolean {
132+
const [x1, y1, x2, y2] = rec1;
133+
const [x3, y3, x4, y4] = rec2;
134+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
135+
}
136+
```
137+
128138
<!-- tabs:end -->
129139

130140
<!-- solution:end -->

‎solution/0800-0899/0836.Rectangle Overlap/README_EN.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,20 @@ tags:
5050

5151
<!-- solution:start -->
5252

53-
### Solution 1
53+
### Solution 1: Determine Non-Overlap Cases
54+
55+
Let the coordinates of rectangle $\text{rec1}$ be $(x_1, y_1, x_2, y_2),ドル and the coordinates of rectangle $\text{rec2}$ be $(x_3, y_3, x_4, y_4)$.
56+
57+
The rectangles $\text{rec1}$ and $\text{rec2}$ do not overlap if any of the following conditions are met:
58+
59+
- $y_3 \geq y_2$: $\text{rec2}$ is above $\text{rec1}$;
60+
- $y_4 \leq y_1$: $\text{rec2}$ is below $\text{rec1}$;
61+
- $x_3 \geq x_2$: $\text{rec2}$ is to the right of $\text{rec1}$;
62+
- $x_4 \leq x_1$: $\text{rec2}$ is to the left of $\text{rec1}$.
63+
64+
If none of the above conditions are met, the rectangles $\text{rec1}$ and $\text{rec2}$ overlap.
65+
66+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
5467

5568
<!-- tabs:start -->
5669

@@ -99,6 +112,16 @@ func isRectangleOverlap(rec1 []int, rec2 []int) bool {
99112
}
100113
```
101114

115+
#### TypeScript
116+
117+
```ts
118+
function isRectangleOverlap(rec1: number[], rec2: number[]): boolean {
119+
const [x1, y1, x2, y2] = rec1;
120+
const [x3, y3, x4, y4] = rec2;
121+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
122+
}
123+
```
124+
102125
<!-- tabs:end -->
103126

104127
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /