Skip to content

Commit be62e49

Browse files
committed
feat: add solutions to lc problem: No.0836
No.0836.Rectangle Overlap
1 parent e25e85b commit be62e49

File tree

8 files changed

+106
-14
lines changed

8 files changed

+106
-14
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,70 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:判断不重叠的情况**
54+
55+
我们记矩形 $rec1$ 的坐标点为 $(x_1, y_1, x_2, y_2)$,矩形 $rec2$ 的坐标点为 $(x_3, y_3, x_4, y_4)$。
56+
57+
那么当满足以下任一条件时,矩形 $rec1$ 和 $rec2$ 不重叠:
58+
59+
- 满足 $y_3 \geq y_2$,即 $rec2$ 在 $rec1$ 的上方;
60+
- 满足 $y_4 \leq y_1$,即 $rec2$ 在 $rec1$ 的下方;
61+
- 满足 $x_3 \geq x_2$,即 $rec2$ 在 $rec1$ 的右方;
62+
- 满足 $x_4 \leq x_1$,即 $rec2$ 在 $rec1$ 的左方。
63+
64+
当以上条件都不满足时,矩形 $rec1$ 和 $rec2$ 重叠。
65+
66+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
67+
5368
<!-- tabs:start -->
5469

5570
### **Python3**
5671

5772
<!-- 这里可写当前语言的特殊实现逻辑 -->
5873

5974
```python
60-
75+
class Solution:
76+
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
77+
x1, y1, x2, y2 = rec1
78+
x3, y3, x4, y4 = rec2
79+
return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)
6180
```
6281

6382
### **Java**
6483

6584
<!-- 这里可写当前语言的特殊实现逻辑 -->
6685

6786
```java
87+
class Solution {
88+
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
89+
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
90+
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
91+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
102+
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
103+
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
104+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
105+
}
106+
};
107+
```
108+
109+
### **Go**
68110
111+
```go
112+
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
113+
x1, y1, x2, y2 := rec1[0], rec1[1], rec1[2], rec1[3]
114+
x3, y3, x4, y4 := rec2[0], rec2[1], rec2[2], rec2[3]
115+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1)
116+
}
69117
```
70118

71119
### **...**

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,46 @@
3838
### **Python3**
3939

4040
```python
41-
41+
class Solution:
42+
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
43+
x1, y1, x2, y2 = rec1
44+
x3, y3, x4, y4 = rec2
45+
return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)
4246
```
4347

4448
### **Java**
4549

4650
```java
51+
class Solution {
52+
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
53+
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
54+
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
55+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
56+
}
57+
}
58+
```
59+
60+
### **C++**
61+
62+
```cpp
63+
class Solution {
64+
public:
65+
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
66+
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
67+
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
68+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
69+
}
70+
};
71+
```
72+
73+
### **Go**
4774
75+
```go
76+
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
77+
x1, y1, x2, y2 := rec1[0], rec1[1], rec1[2], rec1[3]
78+
x3, y3, x4, y4 := rec2[0], rec2[1], rec2[2], rec2[3]
79+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1)
80+
}
4881
```
4982

5083
### **...**
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
class Solution {
22
public:
33
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
4-
int l1 = rec1[1], u1 = rec1[0], r1 = rec1[3], d1 = rec1[2];
5-
int l2 = rec2[1], u2 = rec2[0], r2 = rec2[3], d2 = rec2[2];
6-
7-
//printf("1: (%d,%d), (%d,%d)\n", l1, u1, r1, d1) ;
8-
//printf("2: (%d,%d), (%d,%d)\n", l2, u2, r2, d2) ;
9-
10-
if (l1 < r2 && u1 < d2 && l2 < r1 && u2 < d1)
11-
return true;
12-
return false;
4+
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
5+
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
6+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
137
}
148
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
2+
x1, y1, x2, y2 := rec1[0], rec1[1], rec1[2], rec1[3]
3+
x3, y3, x4, y4 := rec2[0], rec2[1], rec2[2], rec2[3]
4+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1)
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
3+
int x1 = rec1[0], y1 = rec1[1], x2 = rec1[2], y2 = rec1[3];
4+
int x3 = rec2[0], y3 = rec2[1], x4 = rec2[2], y4 = rec2[3];
5+
return !(y3 >= y2 || y4 <= y1 || x3 >= x2 || x4 <= x1);
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
3+
x1, y1, x2, y2 = rec1
4+
x3, y3, x4, y4 = rec2
5+
return not (y3 >= y2 or y4 <= y1 or x3 >= x2 or x4 <= x1)

solution/1000-1099/1053.Previous Permutation With One Swap/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151

5252
**方法一:贪心**
5353

54-
我们先从右到左遍历数组,找到第一个满足 `arr[i - 1] > arr[i]` 的下标 `i`,此时 `arr[i - 1]` 就是我们要交换的数字,我们再从右到左遍历数组,找到第一个满足 `arr[j] < arr[i - 1]``arr[j] != arr[j - 1]` 的下标 `j`,此时我们交换 `arr[i - 1]``arr[j]` 后返回即可。
54+
我们先从右到左遍历数组,找到第一个满足 $arr[i - 1] \gt arr[i]$ 的下标 $i$,此时 $arr[i - 1]$ 就是我们要交换的数字,我们再从右到左遍历数组,找到第一个满足 $arr[j] \lt arr[i - 1]$$arr[j] \neq arr[j - 1]$ 的下标 $j$,此时我们交换 $arr[i - 1]$$arr[j]$ 后返回即可。
5555

56-
如果遍历完数组都没有找到满足条件的下标 `i`,说明数组已经是最小排列,直接返回原数组即可。
56+
如果遍历完数组都没有找到满足条件的下标 $i$,说明数组已经是最小排列,直接返回原数组即可。
5757

5858
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
5959

solution/1000-1099/1054.Distant Barcodes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
重排条形码时,我们每次从堆顶弹出一个元素 `(v, k)`,将 `k` 添加到结果数组中,并将 `(v-1, k)` 放入队列 `q` 中。当队列长度大于 $1$ 时,弹出队首元素 `p`,若此时 `p.v` 大于 $0$,则将 `p` 放入堆中。循环,直至堆为空。
4646

47-
时间复杂度 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为条形码数组的长度。
47+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为条形码数组的长度。
4848

4949
相似题目:[767. 重构字符串](/solution/0700-0799/0767.Reorganize%20String/README.md)
5050

0 commit comments

Comments
 (0)