[
math
geometry
]
Leetcode 0223 Rectangle Area
Problem statement
https://leetcode.com/problems/rectangle-area/
Solution
First, we need to find area of intersection of rectangles. It is equal to length of projections on $x$ axis and on $y$ axis: 2 one dimensional problems. To evaluate the length of intersection of two segments $[x_1, y_1]$ and $[x_2, y_2]$, just evaluate $\min[0,\min(y_1,y_2),\max(x_1,x_2)]$
Complexity
Time and space complexity is O(1)
.
Code
class Solution:
def computeArea(self, A, B, C, D, E, F, G, H):
return (C-A)*(D-B)+(G-E)*(H-F)-max(0,min(C,G)-max(A,E))*max(0,min(D,H)-max(B,F))