LeetCode 836 Rectangle Overlap problem Elixir solution
LeetCode problem link: 836. Rectangle Overlap
You can find the problem description and solutions in C++, Java, JS, TS and Python programming languages supported by LeetCode here
LeetCode 836 Rectangle Overlap: short Elixir description of the solution
This solution uses the Interval Overlap pattern, which is the basic operation behind the broader Interval Intersection pattern.
We use pattern matching to extract the rectangle coordinates from rec1 and rec2. Then we check whether the rectangles have a positive overlap on both the X-axis and the Y-axis. The strict < comparison ensures that rectangles touching only at an edge or corner are not considered overlapping. The solution runs in O(1) time and uses O(1) space.
Time complexity: O(1)
Space complexity: O(1)
LeetCode 836. Rectangle Overlap: Elixir solution
defmodule Solution do
@spec is_rectangle_overlap(rec1 :: [integer], rec2 :: [integer]) :: boolean
def is_rectangle_overlap(rec1, rec2) do
[x1, y1, x2, y2] = rec1
[a1, b1, a2, b2] = rec2
max(x1, a1) < min(x2, a2) and max(y1, b1) < min(y2, b2)
end
end