Skip to content

Commit 40cfcc0

Browse files
author
Мето Трајковски
committed
Added solution for Perfect Rectangle and added a note about competitive programming in the README
1 parent 3b3f424 commit 40cfcc0

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

Diff for: Hashing DS/perfect_rectangle.py

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'''
2+
Perfect Rectangle
3+
4+
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region.
5+
Each rectangle is represented as a bottom-left point and a top-right point. For example, a unit square is represented as [1,1,2,2].
6+
(coordinate of bottom-left point is (1, 1) and top-right point is (2, 2)).
7+
8+
Input: [
9+
[1, 1, 3, 3],
10+
[3, 1, 4, 2],
11+
[3, 2, 4, 4],
12+
[1, 3, 2, 4],
13+
[2, 3, 3, 4]
14+
]
15+
Output: True
16+
Output explanation: All 5 rectangles together form an exact cover of a rectangular region.
17+
18+
Input: [
19+
[1, 1, 2, 3],
20+
[1, 3, 2, 4],
21+
[3, 1, 4, 2],
22+
[3, 2, 4, 4]
23+
]
24+
Output: False
25+
Output explanation: Because there is a gap between the two rectangular regions.
26+
27+
Input: [
28+
[1, 1, 3, 3],
29+
[3, 1, 4, 2],
30+
[1, 3, 2, 4],
31+
[3, 2, 4, 4]
32+
]
33+
Output: False
34+
Output explanation: Because there is a gap in the top center.
35+
36+
Input: [
37+
[1, 1, 3, 3],
38+
[3, 1, 4, 2],
39+
[1, 3, 2, 4],
40+
[2, 2, 4, 4]
41+
]
42+
Output: False
43+
Output explanation: Because two of the rectangles overlap with each other.
44+
45+
=========================================
46+
Check if 4 unique points exist. If 4 unique points exist, then
47+
check if the sum of all rectangles is equal to the final rectangle.
48+
Time Complexity: O(N)
49+
Space Complexity: O(N)
50+
'''
51+
52+
53+
############
54+
# Solution #
55+
############
56+
57+
import math
58+
59+
def is_perfect_rectangle(rectangles):
60+
areas_sum = 0
61+
all_points = set()
62+
63+
for rect in rectangles:
64+
# sum the areas of all rectangles
65+
areas_sum += (rect[2] - rect[0]) * (rect[3] - rect[1])
66+
67+
# find all points of the rectangle and check if they already exist
68+
rect_points = [
69+
(rect[0], rect[1]), # left bottom
70+
(rect[0], rect[3]), # left top
71+
(rect[2], rect[3]), # right top
72+
(rect[2], rect[1]) # right bottom
73+
]
74+
75+
for point in rect_points:
76+
if point in all_points:
77+
all_points.remove(point)
78+
else:
79+
all_points.add(point)
80+
81+
# if we want a perfect rectangle then the rectangle must have 4 unique points
82+
if len(all_points) != 4:
83+
return False
84+
85+
# find the bounding rectangle coordinates (minX, minY, maxX, maxY)
86+
bounding_rectangle = [math.inf, math.inf, -math.inf, -math.inf]
87+
for point in all_points:
88+
bounding_rectangle = [
89+
min(bounding_rectangle[0], point[0]),
90+
min(bounding_rectangle[1], point[1]),
91+
max(bounding_rectangle[2], point[0]),
92+
max(bounding_rectangle[3], point[1])
93+
]
94+
95+
# calculate the area of bounding rectangle
96+
bounding_rectangle_area = (bounding_rectangle[2] - bounding_rectangle[0]) * (bounding_rectangle[3] - bounding_rectangle[1])
97+
98+
# to see if there are overlapping, compare the sum of areas with the final rectangle area
99+
return areas_sum == bounding_rectangle_area
100+
101+
102+
###########
103+
# Testing #
104+
###########
105+
106+
# Test 1
107+
# Correct result => True
108+
rectangles = [[1, 1, 3, 3], [3, 1, 4, 2], [3, 2, 4, 4], [1, 3, 2, 4], [2, 3, 3, 4]]
109+
print(is_perfect_rectangle(rectangles))
110+
111+
# Test 2
112+
# Correct result => False
113+
rectangles = [[1, 1, 2, 3], [1, 3, 2, 4], [3, 1, 4, 2], [3, 2, 4, 4]]
114+
print(is_perfect_rectangle(rectangles))
115+
116+
# Test 3
117+
# Correct result => False
118+
rectangles = [[1, 1, 3, 3], [3, 1, 4, 2], [1, 3, 2, 4], [3, 2, 4, 4]]
119+
print(is_perfect_rectangle(rectangles))
120+
121+
# Test 4
122+
# Correct result => False
123+
rectangles = [[1, 1, 3, 3], [3, 1, 4, 2], [1, 3, 2, 4], [2, 2, 4, 4]]
124+
print(is_perfect_rectangle(rectangles))

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Several books that have made an impression on me:
144144
3. [Algorithms by Robert Sedgewick & Kevin Wayne](https://www.goodreads.com/book/show/10803540-algorithms) - These authors are instructors of the previously mentioned coursera courses: [Algorithms Part 1](https://www.coursera.org/learn/algorithms-part1) and [Algorithms Part 2](https://www.coursera.org/learn/algorithms-part2). Also this book has excellent and free [site](http://algs4.cs.princeton.edu) with exercises, presentations, and examples.
145145
4. [The Algorithm Design Manual by Steven Skiena](https://www.goodreads.com/book/show/425208.The_Algorithm_Design_Manual) - The book describes many advanced topics and algorithms and it focuses on real life practical examples. This book has one of the best [site](http://www.algorist.com) with resources ([solutions](http://www.algorist.com/algowiki/index.php/The_Algorithms_Design_Manual_(Second_Edition)), [algorithms and data structures](http://www.algorist.com/algorist.html), [python implementations](http://www.algorist.com/languages/Python.html)).
146146
5. [Algorithms by S. Dasgupta, C. Papadimitriou, and U. Vazirani](https://www.goodreads.com/book/show/138563.Algorithms) - This book is an official book for algorithms and data structures classes in several famous universities.
147-
6. [Competitive Programming 3 by Steven Halim & Felix Halim](https://www.goodreads.com/book/show/22820968-competitive-programming-3) - A great book that prepares you for competitive programming (not for complete beginners). You can learn many things and tricks about competitive programming.
147+
6. [Competitive Programming 3 by Steven Halim & Felix Halim](https://www.goodreads.com/book/show/22820968-competitive-programming-3) - A great book that prepares you for competitive programming (not for complete beginners). You can learn many things and tricks about competitive programming. *But if your goal is to prepare for competitive programming then choose a faster language than Python, **C/C++** (or Java, it's faster than Python but not like C/C++).*
148148

149149

150150
### Training Sites

0 commit comments

Comments
 (0)