Skip to content

Commit ec4ab05

Browse files
authored
Create count-number-of-rectangles-containing-each-point.py
1 parent 3b05433 commit ec4ab05

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(nlogn + m * max_y * logn), n = len(rectangles), m = len(points)
2+
# Space: O(n)
3+
4+
import bisect
5+
6+
7+
# bucket sort, binary search
8+
class Solution(object):
9+
def countRectangles(self, rectangles, points):
10+
"""
11+
:type rectangles: List[List[int]]
12+
:type points: List[List[int]]
13+
:rtype: List[int]
14+
"""
15+
max_y = max(y for _, y in rectangles)
16+
buckets = [[] for _ in xrange(max_y+1)]
17+
for x, y in rectangles:
18+
buckets[y].append(x)
19+
for bucket in buckets:
20+
bucket.sort()
21+
result = []
22+
for x, y in points:
23+
cnt = 0
24+
for y in xrange(y, max_y+1):
25+
cnt += len(buckets[y])-bisect.bisect_left(buckets[y], x)
26+
result.append(cnt)
27+
return result

0 commit comments

Comments
 (0)