223. Rectangle Area
This consumes quite some brain juice esp. seeing it the first time, it is deserved to be a medium.
1class Solution:
2 def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
3 maxleft = max(ax1, bx1)
4 minright = min(ax2, bx2)
5 maxbottom = max(ay1, by1)
6 mintop = min(ay2, by2)
7
8 overlap = 0
9
10 if maxleft >= minright or maxbottom >= mintop: # no overlap
11 overlap = 0
12 else:
13 overlap = (minright - maxleft) * (mintop - maxbottom)
14
15 return (ax2 - ax1) * (ay2 - ay1) + (bx2 - bx1) * (by2 - by1) - overlap