Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #850: Rectangle Area II

In this guide, we solve Leetcode #850 Rectangle Area II in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given a 2D array of axis-aligned rectangles. Each rectangle[i] = [xi1, yi1, xi2, yi2] denotes the ith rectangle where (xi1, yi1) are the coordinates of the bottom-left corner, and (xi2, yi2) are the coordinates of the top-right corner.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Segment Tree, Array, Ordered Set, Line Sweep

Intuition

The constraints allow a direct scan that keeps only the essential state.

By translating the requirements into a clean loop, the logic stays easy to reason about.

Approach

Iterate through the data once, updating the state needed to compute the answer.

Return the final state after the traversal is complete.

Steps:

  • Parse the input.
  • Iterate and update state.
  • Return the computed answer.

Example

Input: rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]] Output: 6 Explanation: A total area of 6 is covered by all three rectangles, as illustrated in the picture. From (1,1) to (2,2), the green and red rectangles overlap. From (1,0) to (2,3), all three rectangles overlap.

Python Solution

class Node: def __init__(self): self.l = self.r = 0 self.cnt = self.length = 0 class SegmentTree: def __init__(self, nums): n = len(nums) - 1 self.nums = nums self.tr = [Node() for _ in range(n << 2)] self.build(1, 0, n - 1) def build(self, u, l, r): self.tr[u].l, self.tr[u].r = l, r if l != r: mid = (l + r) >> 1 self.build(u << 1, l, mid) self.build(u << 1 | 1, mid + 1, r) def modify(self, u, l, r, k): if self.tr[u].l >= l and self.tr[u].r <= r: self.tr[u].cnt += k else: mid = (self.tr[u].l + self.tr[u].r) >> 1 if l <= mid: self.modify(u << 1, l, r, k) if r > mid: self.modify(u << 1 | 1, l, r, k) self.pushup(u) def pushup(self, u): if self.tr[u].cnt: self.tr[u].length = self.nums[self.tr[u].r + 1] - self.nums[self.tr[u].l] elif self.tr[u].l == self.tr[u].r: self.tr[u].length = 0 else: self.tr[u].length = self.tr[u << 1].length + self.tr[u << 1 | 1].length @property def length(self): return self.tr[1].length class Solution: def rectangleArea(self, rectangles: List[List[int]]) -> int: segs = [] alls = set() for x1, y1, x2, y2 in rectangles: segs.append((x1, y1, y2, 1)) segs.append((x2, y1, y2, -1)) alls.update([y1, y2]) segs.sort() alls = sorted(alls) tree = SegmentTree(alls) m = {v: i for i, v in enumerate(alls)} ans = 0 for i, (x, y1, y2, k) in enumerate(segs): if i: ans += tree.length * (x - segs[i - 1][0]) tree.modify(1, m[y1], m[y2] - 1, k) ans %= int(1e9 + 7) return ans

Complexity

The time complexity is O(n). The space complexity is O(1) to O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy