Skip to content

Commit 246b884

Browse files
committed
rectangle_area
1 parent 7d207fa commit 246b884

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
179179
#### [220. Contains Duplicate III (unsolved)](https://github.com/hitzzc/go-leetcode/tree/master/contains_duplicate_III)
180180
#### [221. Maximal Square](https://github.com/hitzzc/go-leetcode/tree/master/maximal_square)
181181
#### [222. Count Complete Tree Nodes](https://github.com/hitzzc/go-leetcode/tree/master/count_complete_tree_nodes)
182+
#### [223. Rectangle Area](https://github.com/hitzzc/go-leetcode/tree/master/rectangle_area)
182183

183184

184185

rectangle_area/rectangle_area.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package rectangle_area
2+
3+
func computeArea(A int, B int, C int, D int, E int, F int, G int, H int) int {
4+
if C <= E || G <= A || D <= F || H <= B {
5+
return (C-A)*(D-B) + (G-E)*(H-F)
6+
}
7+
left := max(A, E)
8+
bottom := max(B, F)
9+
right := min(C, G)
10+
top := min(D, H)
11+
return (C-A)*(D-B) + (G-E)*(H-F) - (right-left)*(top-bottom)
12+
}
13+
14+
func min(i, j int) int {
15+
if i < j {
16+
return i
17+
}
18+
return j
19+
}
20+
21+
func max(i, j int) int {
22+
if i > j {
23+
return i
24+
}
25+
return j
26+
}

0 commit comments

Comments
 (0)