Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 33a3c26

Browse files
aQuaaQua
aQua
authored and
aQua
committed
497 finish
1 parent 021b5ed commit 33a3c26

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [497. Random Point in Non-overlapping Rectangles](https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/)
2+
3+
## 题目
4+
5+
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the spacecovered by the rectangles.
6+
7+
Note:
8+
9+
1. An integer pointis a point that has integer coordinates.
10+
1. A pointon the perimeterof a rectangle isincluded in the space covered by the rectangles.
11+
1. ith rectangle = rects[i] =[x1,y1,x2,y2], where [x1, y1]are the integer coordinates of the bottom-left corner, and [x2, y2]are the integer coordinates of the top-right corner.
12+
1. length and width of each rectangle does not exceed 2000.
13+
1. 1 <= rects.length<= 100
14+
1. pick return a point as an array of integer coordinates[p_x, p_y]
15+
1. pick is called at most 10000times.
16+
17+
Example 1:
18+
19+
```text
20+
Input:
21+
["Solution","pick","pick","pick"]
22+
[[[[1,1,5,5]]],[],[],[]]
23+
Output:
24+
[null,[4,1],[4,1],[3,3]]
25+
```
26+
27+
Example 2:
28+
29+
```text
30+
Input:
31+
["Solution","pick","pick","pick","pick","pick"]
32+
[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]
33+
Output:
34+
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]
35+
```
36+
37+
Explanation of Input Syntax:
38+
39+
The input is two lists:the subroutines calledand theirarguments.Solution'sconstructor has one argument, the array of rectangles rects. pickhas no arguments.Argumentsarealways wrapped with a list, even if there aren't any.
40+
41+
## 解题思路
42+
43+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package problem0497
2+
3+
import (
4+
"math/rand"
5+
"sort"
6+
)
7+
8+
// Solution object will be instantiated and called as such:
9+
// obj := Constructor(rects);
10+
// param_1 := obj.Pick();
11+
type Solution struct {
12+
total int // 所有可能离散的点的个数
13+
counts []int // count[i] 记录了前 i 个矩形中所有点的个数
14+
rects [][]int
15+
}
16+
17+
// Constructor 创建 Solution
18+
func Constructor(rects [][]int) Solution {
19+
size := len(rects)
20+
21+
total := 0
22+
counts := make([]int, size)
23+
tmp := make([][]int, size)
24+
25+
for i, r := range rects {
26+
x1, y1, x2, y2 := r[0], r[1], r[2], r[3]
27+
w, h := x2-x1+1, y2-y1+1 // +1 是因为 [a,b] 之间一共有 b-a+1 个离散的点
28+
total += w * h
29+
counts[i] = total
30+
tmp[i] = []int{x1, y1, w, h} // 修改 rect 的表达方式
31+
}
32+
33+
return Solution{
34+
counts: counts,
35+
rects: tmp,
36+
total: total,
37+
}
38+
}
39+
40+
// Pick 返回正方形内的点
41+
func (s *Solution) Pick() []int {
42+
cand := rand.Intn(s.total)
43+
// 根据 cand 找到需要返回那个矩形下的点
44+
i := sort.SearchInts(s.counts, cand)
45+
46+
x1, y1, w, h := s.rects[i][0], s.rects[i][1], s.rects[i][2], s.rects[i][3]
47+
48+
x := x1 + rand.Intn(w)
49+
y := y1 + rand.Intn(h)
50+
51+
return []int{x, y}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package problem0497
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_Solution(t *testing.T) {
10+
ast := assert.New(t)
11+
12+
rects := [][]int{
13+
[]int{1, 1, 5, 5},
14+
}
15+
16+
s := Constructor(rects)
17+
18+
for i := 0; i < 10000; i++ {
19+
p := s.Pick()
20+
actual := 0
21+
for _, r := range rects {
22+
if isWithin(r, p) {
23+
actual++
24+
}
25+
}
26+
ast.Equal(1, actual)
27+
}
28+
}
29+
30+
func Test_Solution_2(t *testing.T) {
31+
ast := assert.New(t)
32+
33+
rects := [][]int{
34+
{-2, -2, -1, -1},
35+
{1, 0, 3, 0},
36+
}
37+
38+
s := Constructor(rects)
39+
40+
for i := 0; i < 10000; i++ {
41+
p := s.Pick()
42+
actual := 0
43+
for _, r := range rects {
44+
if isWithin(r, p) {
45+
actual++
46+
}
47+
}
48+
ast.Equal(1, actual)
49+
}
50+
}
51+
52+
func isWithin(rect, point []int) bool {
53+
x1, y1, x2, y2 := rect[0], rect[1], rect[2], rect[3]
54+
x, y := point[0], point[1]
55+
56+
return x1 <= x && x <= x2 &&
57+
y1 <= y && y <= y2
58+
}

leetcode.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 869,
4-
"Updated": "2018-08-04T21:14:48.409930646+08:00",
4+
"Updated": "2018-08-04T21:16:55.918544895+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 180,

0 commit comments

Comments
 (0)