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:
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.
Read other posts