forked from JediXL/LeetCodeByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path497Random_Point_in_Non-overlapping_Rectangles.py
59 lines (48 loc) · 1.7 KB
/
497Random_Point_in_Non-overlapping_Rectangles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import random
import bisect
'''
@auther: Jedi.L
@Date: Tue, Feb 26, 2019 12:10
@Email: [email protected]
'''
# class Solution:
# def __init__(self, rects: List[List[int]]):
# self.rects = rects
# self.weights = []
# for [x_bl, y_bl, x_tr, y_tr] in self.rects:
# self.weights.append((x_tr - x_bl + 1) * (y_tr - y_bl + 1))
# def pick(self) -> List[int]:
# [x_bl, y_bl, x_tr, y_tr] = random.choices(
# self.rects, weights=self.weights)[0]
# res = [
# random.randrange(x_bl, x_tr + 1),
# random.randrange(y_bl, y_tr + 1)
# ]
# return res
class Solution:
def __init__(self, rects: List[List[int]]):
self.rects = rects
self.weights = []
for [x_bl, y_bl, x_tr, y_tr] in self.rects:
self.weights.append((x_tr - x_bl + 1) * (y_tr - y_bl + 1))
def pick(self) -> List[int]:
[x_bl, y_bl, x_tr, y_tr] = random.choices(
self.rects, weights=self.weights)[0]
res = [
random.randrange(x_bl, x_tr + 1),
random.randrange(y_bl, y_tr + 1)
]
return res
class Solution2:
def __init__(self, rects):
self.rects, self.ranges, point_sum = rects, [], 0
for x_bl, y_bl, x_tr, y_tr in rects:
point_sum += (x_tr - x_bl + 1) * (y_tr - y_bl + 1)
self.ranges.append(point_sum)
def pick(self):
x1, y1, x2, y2 = self.rects[bisect.bisect_left(
self.ranges, random.randint(1, self.ranges[-1]))]
return [random.randint(x1, x2), random.randint(y1, y2)]
# Your Solution object will be instantiated and called as such:
# obj = Solution(rects)
# param_1 = obj.pick()