Ctrl K

Three Strips of Tiles

Medium
There is a square field of size × size with the origin in the bottom-left corner. Rectangular tiles lie on it: tiles[i] = [x1, y1, x2, y2], where (x1, y1) is the tile's bottom-left corner and (x2, y2) its top-right corner. Tiles never overlap, though they may touch along edges. You may make two cuts across the whole field, either both horizontal or both vertical. The cuts split the field into three strips, and the following must hold: - no tile is cut: each one lies entirely within a single strip; - each of the three strips contains at least one tile. Return True if such cuts exist, and False otherwise.
Example 1
size=6, tiles=[[0, 0, 2, 3], [2, 1, 4, 6], [4, 0, 6, 2], [4, 3, 6, 5]] → True

Vertical cuts at x = 2 and x = 4 give three strips: the first holds [0, 0, 2, 3], the second [2, 1, 4, 6], the third the remaining two tiles.

Example 2
size=4, tiles=[[0, 0, 2, 2], [2, 0, 4, 2], [0, 2, 2, 4], [2, 2, 4, 4]] → False

This is a 2×2 grid of tiles: on either axis they form only two strips, never three.

Example 3
size=5, tiles=[[0, 0, 5, 1], [1, 1, 3, 3], [0, 3, 2, 5], [3, 4, 5, 5]] → True
Constraints
  • 3 ≤ size ≤ 10⁹
  • 3 ≤ len(tiles) ≤ 10⁵
  • 0 ≤ x1 < x2 ≤ size
  • 0 ≤ y1 < y2 ≤ size
  • No two tiles overlap
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumDistinct Orderings With Repeats MediumAnagram Buckets Medium
loading Python…
Input
size=6, tiles=[[0, 0, 2, 3], [2, 1, 4, 6], [4, 0, 6, 2], [4, 3, 6, 5]]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 51 tests on the server, hidden ones included.