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

Commit 475c6cc

Browse files
aQuaaQua
aQua
authored and
aQua
committed
850 accepted
1 parent 760b39c commit 475c6cc

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

Algorithms/0850.rectangle-area-ii/rectangle-area-ii.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,38 @@ import (
44
"sort"
55
)
66

7+
// 思路来自 https://leetcode.com/problems/rectangle-area-ii/discuss/137914/C++Python-Discretization-and-O(NlogN)
8+
79
const mod = 1e9 + 7
810

911
func rectangleArea(rectangles [][]int) int {
12+
// 提取 rectangles 中所有的 x 坐标值,并按照升序排列
1013
xs := getXs(rectangles)
11-
sort.Ints(xs)
1214

13-
idxX := make(map[int]int, 2*len(xs))
14-
for k, x := range xs {
15-
idxX[x] = k
15+
idxs := make(map[int]int, 2*len(xs))
16+
for idx, x := range xs {
17+
idxs[x] = idx
1618
}
1719

1820
count := make([]int, len(xs))
1921

20-
L := make([][]int, 0, 2*len(rectangles))
21-
22-
for _, r := range rectangles {
23-
x1, y1, x2, y2 := r[0], r[1], r[2], r[3]
24-
L = append(L,
25-
[]int{y1, x1, x2, 1},
26-
[]int{y2, x1, x2, -1},
27-
)
28-
}
29-
30-
sort.Slice(L, func(i int, j int) bool {
31-
eq0 := L[i][0] == L[j][0]
32-
eq1 := L[i][1] == L[j][1]
33-
eq2 := L[i][2] == L[j][2]
34-
if eq0 && eq1 && eq2 {
35-
return L[i][3] < L[j][3]
36-
}
37-
if eq0 && eq1 {
38-
return L[i][2] < L[j][2]
39-
}
40-
if eq0 {
41-
return L[i][1] < L[j][1]
42-
}
43-
return L[i][0] < L[j][0]
44-
})
22+
labels := getLabels(rectangles)
4523

4624
curY, curXSum, area := 0, 0, 0
4725

48-
for _, l := range L {
26+
for _, l := range labels {
4927
y, x1, x2, sig := l[0], l[1], l[2], l[3]
5028
area += (y - curY) * curXSum
5129
curY = y
52-
for i := idxX[x1]; i < idxX[x2]; i++ {
30+
for i := idxs[x1]; i < idxs[x2]; i++ {
5331
count[i] += sig
5432
}
5533
curXSum = 0
5634
for i := 0; i+1 < len(count); i++ {
57-
if count[i] != 0 {
35+
if count[i] > 0 {
5836
curXSum += xs[i+1] - xs[i]
5937
}
6038
}
61-
6239
}
6340

6441
return area % mod
@@ -68,15 +45,43 @@ func getXs(rects [][]int) []int {
6845
size := len(rects)
6946
res := make([]int, 1, size*2)
7047
xMap := make(map[int]bool, size*2)
71-
7248
for _, r := range rects {
7349
xMap[r[0]] = true
7450
xMap[r[2]] = true
7551
}
76-
7752
for k := range xMap {
7853
res = append(res, k)
7954
}
80-
55+
sort.Ints(res)
8156
return res
8257
}
58+
59+
func getLabels(rects [][]int) [][]int {
60+
labels := make([][]int, 0, 2*len(rects))
61+
for _, r := range rects {
62+
x1, y1, x2, y2 := r[0], r[1], r[2], r[3]
63+
labels = append(labels,
64+
[]int{y1, x1, x2, 1},
65+
[]int{y2, x1, x2, -1},
66+
)
67+
}
68+
69+
// 对 labels 进行排序
70+
sort.Slice(labels, func(i int, j int) bool {
71+
eq0 := labels[i][0] == labels[j][0]
72+
eq1 := labels[i][1] == labels[j][1]
73+
eq2 := labels[i][2] == labels[j][2]
74+
if eq0 && eq1 && eq2 {
75+
return labels[i][3] < labels[j][3]
76+
}
77+
if eq0 && eq1 {
78+
return labels[i][2] < labels[j][2]
79+
}
80+
if eq0 {
81+
return labels[i][1] < labels[j][1]
82+
}
83+
return labels[i][0] < labels[j][0]
84+
})
85+
86+
return labels
87+
}

Algorithms/0850.rectangle-area-ii/rectangle-area-ii_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var tcs = []struct {
1313
ans int
1414
}{
1515

16+
{
17+
[][]int{{0, 0, 2, 2}, {3, 0, 5, 2}},
18+
8,
19+
},
20+
1621
{
1722
[][]int{{0, 0, 2, 2}, {1, 0, 2, 3}, {1, 0, 3, 1}},
1823
6,

0 commit comments

Comments
 (0)