Skip to content

Commit 1fc4ec0

Browse files
solve srectangle overlap
1 parent a34b107 commit 1fc4ec0

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-194/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-194/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-195/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-195/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -225,7 +225,7 @@
225225
| 824 | [Goat Latin](https://leetcode.com/problems/goat-latin) | [![Java](assets/java.png)](src/GoatLatin.java) |
226226
| 830 | [Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups) | [![Java](assets/java.png)](src/PositionsOfLargeGroups.java) |
227227
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image) | [![Java](assets/java.png)](src/FlippingAnImage.java) |
228-
| 836 | [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap) | |
228+
| 836 | [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap) | [![Java](assets/java.png)](src/RectangleOverlap.java) |
229229
| 840 | [Magic Squares in Grid](https://leetcode.com/problems/magic-squares-in-grid) | |
230230
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare) | |
231231
| 849 | [Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person) | |

Diff for: src/RectangleOverlap.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class RectangleOverlap {
2+
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
3+
Rectangle rectangle1 = new Rectangle(rec1);
4+
Rectangle rectangle2 = new Rectangle(rec2);
5+
return hasXIntersection(rectangle1, rectangle2) && hasYIntersection(rectangle1, rectangle2);
6+
}
7+
8+
private boolean hasXIntersection(Rectangle rec1, Rectangle rec2) {
9+
return (rec1.x1 <= rec2.x1 && rec2.x1 < rec1.x2)
10+
|| (rec2.x1 < rec1.x1 && rec1.x1 < rec2.x2);
11+
}
12+
13+
private boolean hasYIntersection(Rectangle rec1, Rectangle rec2) {
14+
return (rec1.y1 <= rec2.y1 && rec2.y1 < rec1.y2)
15+
|| (rec2.y1 < rec1.y1 && rec1.y1 < rec2.y2);
16+
}
17+
18+
private static class Rectangle {
19+
private final int x1, x2, y1, y2;
20+
Rectangle(int[] points) {
21+
this.x1 = points[0];
22+
this.y1 = points[1];
23+
this.x2 = points[2];
24+
this.y2 = points[3];
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)