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

Commit aeec9a7

Browse files
aQuaaQua
aQua
authored and
aQua
committed
850 wrong answer
1 parent 47f85d2 commit aeec9a7

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [850. Rectangle Area II](https://leetcode.com/problems/rectangle-area-ii/)
2+
3+
## 题目
4+
5+
We are given a list of (axis-aligned)rectangles. Eachrectangle[i] = [x1, y1, x2, y2], where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle.
6+
7+
Find the total area covered by all rectangles in the plane. Since the answermay be too large, return it modulo 10^9 + 7.
8+
9+
![pic](rectangle_area_ii_pic.png)
10+
11+
Example 1:
12+
13+
```text
14+
Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
15+
Output: 6
16+
Explanation: As illustrated in the picture.
17+
```
18+
19+
Example 2:
20+
21+
```text
22+
Input: [[0,0,1000000000,1000000000]]
23+
Output: 49
24+
Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49.
25+
```
26+
27+
Note:
28+
29+
1. 1 <= rectangles.length <= 200
30+
1. rectanges[i].length = 4
31+
1. 0 <= rectangles[i][j] <= 10^9
32+
1. The total area covered by all rectangles will never exceed2^63 - 1and thus will fit in a 64-bit signed integer.
33+
34+
## 解题思路
35+
36+
见程序注释
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package problem0850
2+
3+
const mod = 1e9 + 7
4+
5+
func rectangleArea(rectangles [][]int) int {
6+
sum := 0
7+
for _, r := range rectangles {
8+
sum += area(r)
9+
}
10+
return sum % mod
11+
}
12+
13+
func area(r []int) int {
14+
return ((r[2] - r[0]) * (r[3] - r[1])) % mod
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package problem0850
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
rectangles [][]int
13+
ans int
14+
}{
15+
16+
{
17+
[][]int{{0, 0, 2, 2}, {1, 0, 2, 3}, {1, 0, 3, 1}},
18+
6,
19+
},
20+
21+
{
22+
[][]int{{0, 0, 1000000000, 1000000000}},
23+
49,
24+
},
25+
26+
// 可以有多个 testcase
27+
}
28+
29+
func Test_rectangleArea(t *testing.T) {
30+
ast := assert.New(t)
31+
32+
for _, tc := range tcs {
33+
fmt.Printf("~~%v~~\n", tc)
34+
ast.Equal(tc.ans, rectangleArea(tc.rectangles), "输入:%v", tc)
35+
}
36+
}
37+
38+
func Benchmark_rectangleArea(b *testing.B) {
39+
for i := 0; i < b.N; i++ {
40+
for _, tc := range tcs {
41+
rectangleArea(tc.rectangles)
42+
}
43+
}
44+
}
Loading

leetcode.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 997,
4-
"Updated": "2018-07-04T05:44:33.515334967+08:00",
4+
"Updated": "2018-07-04T06:04:51.061950892+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 175,
88
"Total": 178
99
},
1010
"Medium": {
11-
"Solved": 301,
11+
"Solved": 302,
1212
"Total": 309
1313
},
1414
"Hard": {
1515
"Solved": 130,
1616
"Total": 135
1717
},
1818
"Total": {
19-
"Solved": 606,
19+
"Solved": 607,
2020
"Total": 622
2121
}
2222
},
@@ -7117,7 +7117,7 @@
71177117
"ID": 591,
71187118
"Title": "Tag Validator",
71197119
"TitleSlug": "tag-validator",
7120-
"PassRate": "30%",
7120+
"PassRate": "31%",
71217121
"Difficulty": "Hard",
71227122
"IsAccepted": true,
71237123
"IsPaid": false,
@@ -9481,7 +9481,7 @@
94819481
"ID": 788,
94829482
"Title": "Rotated Digits",
94839483
"TitleSlug": "rotated-digits",
9484-
"PassRate": "51%",
9484+
"PassRate": "50%",
94859485
"Difficulty": "Easy",
94869486
"IsAccepted": true,
94879487
"IsPaid": false,
@@ -10189,7 +10189,7 @@
1018910189
"ID": 847,
1019010190
"Title": "Shortest Path Visiting All Nodes",
1019110191
"TitleSlug": "shortest-path-visiting-all-nodes",
10192-
"PassRate": "39%",
10192+
"PassRate": "40%",
1019310193
"Difficulty": "Hard",
1019410194
"IsAccepted": true,
1019510195
"IsPaid": false,
@@ -10203,7 +10203,7 @@
1020310203
"TitleSlug": "shifting-letters",
1020410204
"PassRate": "34%",
1020510205
"Difficulty": "Medium",
10206-
"IsAccepted": false,
10206+
"IsAccepted": true,
1020710207
"IsPaid": false,
1020810208
"IsFavor": false,
1020910209
"IsNew": false,

0 commit comments

Comments
 (0)