2 minutes
Leetcode 836
This solution is pretty similar to my other solution Leetcode: 223 This is the part in the other solution which I based this solution on:
// A = rec1[0], B = rec1[1], C = rec1[2], D = rec1[3]
// E = rec2[0], F = rec2[1], G = rec2[2], H = rec2[3]
left, right := max(A, E), min(G, C)
up, down := min(D, H), max(F, B)
if right > left && up > down {
sum -= (right - left) * (up - down) // overlap
}
The idea of this solution can be shown using some images:
We can get the input [1, 1, 3, 3], [2, 2, 7, 6]
and graph it:
And when the left, right, up, down
lines are shown we get the overlap. We can find the left, right, up, down
lines by doing the folowing:
Left =
the maximum ofrec1 x1
andrec2 x1
, and as you can seerec2 x1
isleft
because it is greater thanrec1 x1
Right =
the minimum ofrec1 x2
andrec2 x2
. As we can seeright
isrec1 x1
.Down =
the maximum ofrec1 y1
andrec2 y1
. As we can also see the max isrec2 y1
.Up =
the minimum ofrec1 y2
andrec2 y2
. Al we can see the min isrec1 y2
.
And now you can see that this is how we find whether there is an overlap.
func isRectangleOverlap(rec1 []int, rec2 []int) bool {
left, right := max(rec1[0], rec2[0]), min(rec1[2], rec2[2])
up, down := min(rec1[3], rec2[3]), max(rec1[1], rec2[1])
if right > left && up > down {
return true
}
return false
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
Read other posts