Skip to content

Commit 80f7848

Browse files
committed
feat: add solutions to lc problem: No.0497
No.0497.Random Point in Non-overlapping Rectangles
1 parent beb9c17 commit 80f7848

File tree

6 files changed

+397
-2
lines changed

6 files changed

+397
-2
lines changed

solution/0400-0499/0497.Random Point in Non-overlapping Rectangles/README.md

+138-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,159 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
前缀和 + 二分查找。
57+
58+
将矩形面积求前缀和 s,然后随机获取到一个面积 v,利用二分查找定位到是哪个矩形,然后继续随机获取该矩形的其中一个整数点坐标即可。
59+
5660
<!-- tabs:start -->
5761

5862
### **Python3**
5963

6064
<!-- 这里可写当前语言的特殊实现逻辑 -->
6165

6266
```python
63-
67+
class Solution:
68+
69+
def __init__(self, rects: List[List[int]]):
70+
self.rects = rects
71+
self.s = [0] * len(rects)
72+
for i, (x1, y1, x2, y2) in enumerate(rects):
73+
self.s[i] = self.s[i - 1] + (x2 - x1 + 1) * (y2 - y1 + 1)
74+
75+
def pick(self) -> List[int]:
76+
v = random.randint(1, self.s[-1])
77+
left, right = 0, len(self.s) - 1
78+
while left < right:
79+
mid = (left + right) >> 1
80+
if self.s[mid] >= v:
81+
right = mid
82+
else:
83+
left = mid + 1
84+
x1, y1, x2, y2 = self.rects[left]
85+
return [random.randint(x1, x2), random.randint(y1, y2)]
86+
87+
88+
# Your Solution object will be instantiated and called as such:
89+
# obj = Solution(rects)
90+
# param_1 = obj.pick()
6491
```
6592

6693
### **Java**
6794

6895
<!-- 这里可写当前语言的特殊实现逻辑 -->
6996

7097
```java
98+
class Solution {
99+
private int[] s;
100+
private int[][] rects;
101+
private Random random = new Random();
102+
103+
public Solution(int[][] rects) {
104+
int n = rects.length;
105+
s = new int[n + 1];
106+
for (int i = 0; i < n; ++i) {
107+
s[i + 1] = s[i] + (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1);
108+
}
109+
this.rects = rects;
110+
}
111+
112+
public int[] pick() {
113+
int n = rects.length;
114+
int v = 1 + random.nextInt(s[n]);
115+
int left = 0, right = n;
116+
while (left < right) {
117+
int mid = (left + right) >> 1;
118+
if (s[mid] >= v) {
119+
right = mid;
120+
} else {
121+
left = mid + 1;
122+
}
123+
}
124+
int[] rect = rects[left - 1];
125+
return new int[]{rect[0] + random.nextInt(rect[2] - rect[0] + 1), rect[1] + random.nextInt(rect[3] - rect[1] + 1)};
126+
}
127+
}
128+
129+
/**
130+
* Your Solution object will be instantiated and called as such:
131+
* Solution obj = new Solution(rects);
132+
* int[] param_1 = obj.pick();
133+
*/
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
vector<int> s;
142+
vector<vector<int>> rects;
143+
144+
Solution(vector<vector<int>>& rects) {
145+
int n = rects.size();
146+
s.resize(n + 1);
147+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1);
148+
this->rects = rects;
149+
srand(time(nullptr));
150+
}
151+
152+
vector<int> pick() {
153+
int n = rects.size();
154+
int v = 1 + rand() % s[n];
155+
int left = 0, right = n;
156+
while (left < right)
157+
{
158+
int mid = (left + right) >> 1;
159+
if (s[mid] >= v) right = mid;
160+
else left = mid + 1;
161+
}
162+
auto& rect = rects[left - 1];
163+
int x = rect[0] + rand() % (rect[2] - rect[0] + 1);
164+
int y = rect[1] + rand() % (rect[3] - rect[1] + 1);
165+
return {x, y};
166+
}
167+
};
168+
169+
/**
170+
* Your Solution object will be instantiated and called as such:
171+
* Solution* obj = new Solution(rects);
172+
* vector<int> param_1 = obj->pick();
173+
*/
174+
```
71175

176+
### **Go**
177+
178+
```go
179+
type Solution struct {
180+
s []int
181+
rects [][]int
182+
}
183+
184+
func Constructor(rects [][]int) Solution {
185+
n := len(rects)
186+
s := make([]int, n+1)
187+
for i, v := range rects {
188+
s[i+1] = s[i] + (v[2]-v[0]+1)*(v[3]-v[1]+1)
189+
}
190+
return Solution{s, rects}
191+
}
192+
193+
func (this *Solution) Pick() []int {
194+
n := len(this.rects)
195+
v := 1 + rand.Intn(this.s[len(this.s)-1])
196+
left, right := 0, n
197+
for left < right {
198+
mid := (left + right) >> 1
199+
if this.s[mid] >= v {
200+
right = mid
201+
} else {
202+
left = mid + 1
203+
}
204+
}
205+
rect := this.rects[left-1]
206+
x, y := rect[0]+rand.Intn(rect[2]-rect[0]+1), rect[1]+rand.Intn(rect[3]-rect[1]+1)
207+
return []int{x, y}
208+
}
72209
```
73210

74211
### **...**

solution/0400-0499/0497.Random Point in Non-overlapping Rectangles/README_EN.md

+134-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,146 @@
7777
### **Python3**
7878

7979
```python
80-
80+
class Solution:
81+
82+
def __init__(self, rects: List[List[int]]):
83+
self.rects = rects
84+
self.s = [0] * len(rects)
85+
for i, (x1, y1, x2, y2) in enumerate(rects):
86+
self.s[i] = self.s[i - 1] + (x2 - x1 + 1) * (y2 - y1 + 1)
87+
88+
def pick(self) -> List[int]:
89+
v = random.randint(1, self.s[-1])
90+
left, right = 0, len(self.s) - 1
91+
while left < right:
92+
mid = (left + right) >> 1
93+
if self.s[mid] >= v:
94+
right = mid
95+
else:
96+
left = mid + 1
97+
x1, y1, x2, y2 = self.rects[left]
98+
return [random.randint(x1, x2), random.randint(y1, y2)]
99+
100+
101+
# Your Solution object will be instantiated and called as such:
102+
# obj = Solution(rects)
103+
# param_1 = obj.pick()
81104
```
82105

83106
### **Java**
84107

85108
```java
109+
class Solution {
110+
private int[] s;
111+
private int[][] rects;
112+
private Random random = new Random();
113+
114+
public Solution(int[][] rects) {
115+
int n = rects.length;
116+
s = new int[n + 1];
117+
for (int i = 0; i < n; ++i) {
118+
s[i + 1] = s[i] + (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1);
119+
}
120+
this.rects = rects;
121+
}
122+
123+
public int[] pick() {
124+
int n = rects.length;
125+
int v = 1 + random.nextInt(s[n]);
126+
int left = 0, right = n;
127+
while (left < right) {
128+
int mid = (left + right) >> 1;
129+
if (s[mid] >= v) {
130+
right = mid;
131+
} else {
132+
left = mid + 1;
133+
}
134+
}
135+
int[] rect = rects[left - 1];
136+
return new int[]{rect[0] + random.nextInt(rect[2] - rect[0] + 1), rect[1] + random.nextInt(rect[3] - rect[1] + 1)};
137+
}
138+
}
139+
140+
/**
141+
* Your Solution object will be instantiated and called as such:
142+
* Solution obj = new Solution(rects);
143+
* int[] param_1 = obj.pick();
144+
*/
145+
```
146+
147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
vector<int> s;
153+
vector<vector<int>> rects;
154+
155+
Solution(vector<vector<int>>& rects) {
156+
int n = rects.size();
157+
s.resize(n + 1);
158+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1);
159+
this->rects = rects;
160+
srand(time(nullptr));
161+
}
162+
163+
vector<int> pick() {
164+
int n = rects.size();
165+
int v = 1 + rand() % s[n];
166+
int left = 0, right = n;
167+
while (left < right)
168+
{
169+
int mid = (left + right) >> 1;
170+
if (s[mid] >= v) right = mid;
171+
else left = mid + 1;
172+
}
173+
auto& rect = rects[left - 1];
174+
int x = rect[0] + rand() % (rect[2] - rect[0] + 1);
175+
int y = rect[1] + rand() % (rect[3] - rect[1] + 1);
176+
return {x, y};
177+
}
178+
};
179+
180+
/**
181+
* Your Solution object will be instantiated and called as such:
182+
* Solution* obj = new Solution(rects);
183+
* vector<int> param_1 = obj->pick();
184+
*/
185+
```
86186

187+
### **Go**
188+
189+
```go
190+
type Solution struct {
191+
s []int
192+
rects [][]int
193+
}
194+
195+
func Constructor(rects [][]int) Solution {
196+
n := len(rects)
197+
s := make([]int, n+1)
198+
for i, v := range rects {
199+
s[i+1] = s[i] + (v[2]-v[0]+1)*(v[3]-v[1]+1)
200+
}
201+
return Solution{s, rects}
202+
}
203+
204+
func (this *Solution) Pick() []int {
205+
n := len(this.rects)
206+
v := 1 + rand.Intn(this.s[len(this.s)-1])
207+
left, right := 0, n
208+
for left < right {
209+
mid := (left + right) >> 1
210+
if this.s[mid] >= v {
211+
right = mid
212+
} else {
213+
left = mid + 1
214+
}
215+
}
216+
rect := this.rects[left-1]
217+
x, y := rect[0]+rand.Intn(rect[2]-rect[0]+1), rect[1]+rand.Intn(rect[3]-rect[1]+1)
218+
return []int{x, y}
219+
}
87220
```
88221

89222
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
vector<int> s;
4+
vector<vector<int>> rects;
5+
6+
Solution(vector<vector<int>>& rects) {
7+
int n = rects.size();
8+
s.resize(n + 1);
9+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + (rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1);
10+
this->rects = rects;
11+
srand(time(nullptr));
12+
}
13+
14+
vector<int> pick() {
15+
int n = rects.size();
16+
int v = 1 + rand() % s[n];
17+
int left = 0, right = n;
18+
while (left < right)
19+
{
20+
int mid = (left + right) >> 1;
21+
if (s[mid] >= v) right = mid;
22+
else left = mid + 1;
23+
}
24+
auto& rect = rects[left - 1];
25+
int x = rect[0] + rand() % (rect[2] - rect[0] + 1);
26+
int y = rect[1] + rand() % (rect[3] - rect[1] + 1);
27+
return {x, y};
28+
}
29+
};
30+
31+
/**
32+
* Your Solution object will be instantiated and called as such:
33+
* Solution* obj = new Solution(rects);
34+
* vector<int> param_1 = obj->pick();
35+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
type Solution struct {
2+
s []int
3+
rects [][]int
4+
}
5+
6+
func Constructor(rects [][]int) Solution {
7+
n := len(rects)
8+
s := make([]int, n+1)
9+
for i, v := range rects {
10+
s[i+1] = s[i] + (v[2]-v[0]+1)*(v[3]-v[1]+1)
11+
}
12+
return Solution{s, rects}
13+
}
14+
15+
func (this *Solution) Pick() []int {
16+
n := len(this.rects)
17+
v := 1 + rand.Intn(this.s[len(this.s)-1])
18+
left, right := 0, n
19+
for left < right {
20+
mid := (left + right) >> 1
21+
if this.s[mid] >= v {
22+
right = mid
23+
} else {
24+
left = mid + 1
25+
}
26+
}
27+
rect := this.rects[left-1]
28+
x, y := rect[0]+rand.Intn(rect[2]-rect[0]+1), rect[1]+rand.Intn(rect[3]-rect[1]+1)
29+
return []int{x, y}
30+
}

0 commit comments

Comments
 (0)