Skip to content

Commit 31090c9

Browse files
committed
836_Rectangle_Overlap
1 parent b173c58 commit 31090c9

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Also, there are open source implementations for basic data structs and algorithm
184184
| 811 | [Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/811_Subdomain_Visit_Count.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/811_Subdomain_Visit_Count.java) | String split and HashMap, O(n) and O(n) |
185185
| 819 | [Most Common Word](https://leetcode.com/problems/most-common-word/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/819_Most_Common_Word.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/819_Most_Common_Word.java) | String processing, be careful about 'b,b,b'. regex is recommended. |
186186
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/832_Flipping_an_Image.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/832_Flipping_an_Image.java) | Invert and swap can be done at the same time, and careful about (n + 1)/2, O(n^2) and O(1) |
187+
| 836 | [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/836_Rectangle_Overlap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/836_Rectangle_Overlap.java) | 1. Check position, O(1) and O(1)<br>2. Check area, O(1) and O(1) |
187188
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/844_Backspace_String_Compare.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/844_Backspace_String_Compare.java) | 1. Stack pop when encounters #, O(n) and O(n)<br>2. Compare string from end to start, O(n) and O(1) |
188189
| 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/852_Peak_Index_in_a_Mountain_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/852_Peak_Index_in_a_Mountain_Array.java) | 1. Scan the array until encountering decline, O(n) and O(1)<br>2. Binary seach with additional check for [i + 1], O(logn) and O(1) |
189190
| 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/867_Transpose_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/867_Transpose_Matrix.java) | Res[i][j] = A[j][i] |

java/836_Rectangle_Overlap.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
/*public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
3+
// Check position
4+
return !(rec1[2] <= rec2[0] || // left
5+
rec1[3] <= rec2[1] || // bottom
6+
rec1[0] >= rec2[2] || // right
7+
rec1[1] >= rec2[3]); // top
8+
}*/
9+
/*public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
10+
// Check area
11+
// https://leetcode.com/problems/rectangle-area/discuss/62149/Just-another-short-way
12+
int left = Math.max(rec1[0], rec2[0]), right = Math.max(Math.min(rec1[2], rec2[2]), left);
13+
int bottom = Math.max(rec1[1], rec2[1]), top = Math.max(Math.min(rec1[3], rec2[3]), bottom);
14+
return (right - left) * (top - bottom) != 0;
15+
}*/
16+
17+
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
18+
// Check area
19+
return (Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) && // width > 0
20+
Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1])); // height > 0
21+
}
22+
}

python/836_Rectangle_Overlap.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def isRectangleOverlap(self, rec1, rec2):
3+
"""
4+
:type rec1: List[int]
5+
:type rec2: List[int]
6+
:rtype: bool
7+
"""
8+
return not (rec1[2] <= rec2[0] or # left
9+
rec1[3] <= rec2[1] or # bottom
10+
rec1[0] >= rec2[2] or # right
11+
rec1[1] >= rec2[3]) # top
12+
13+
# def isRectangleOverlap(self, rec1, rec2):
14+
# def intersect(p_left, p_right, q_left, q_right):
15+
# return min(p_right, q_right) > max(p_left, q_left)
16+
# return (intersect(rec1[0], rec1[2], rec2[0], rec2[2]) and
17+
# intersect(rec1[1], rec1[3], rec2[1], rec2[3]))

0 commit comments

Comments
 (0)