Problem · Array

Rectangle Fit Queries

Learn this problem
MediumAirbnbNEW GRADOA

Problem statement

You are given an integer matrix operations. Process its rows from left to right. Each row has one of two forms:

  • [0, a, b]: create and save a rectangle of size a × b.
  • [1, a, b]: determine whether every rectangle saved by earlier operations can fit inside a box of size a × b.

For a query, test each saved rectangle separately; the rectangles do not need to fit in the box at the same time. You may rotate any saved rectangle by 90 degrees, changing dimensions x × y to y × x.

Return a boolean array containing the answers to all query operations in the order they appear.

Function

canFitSavedRectangles(operations: int[][]) → boolean[]

Examples

Example 1

operations = [[1,1,1]]return = [true]

No rectangles have been saved before the query, so every saved rectangle fits and the answer is true.

Example 2

operations = [[0,1,3],[0,4,2],[1,3,4],[1,3,2]]return = [true,false]
  1. Save rectangles of sizes 1 × 3 and 4 × 2.
  2. For the 3 × 4 box, the first rectangle fits as-is, and the second fits after rotation to 2 × 4. The first query therefore returns true.
  3. For the 3 × 2 box, the first rectangle fits after rotation to 3 × 1, but the second rectangle does not fit in either orientation. The second query therefore returns false.

The final result is [true,false].

Constraints

  • 1 ≤ operations.length ≤ 10^5
  • operations[i].length = 3
  • operations[i][0] is either 0 or 1.
  • 1 ≤ operations[i][1] ≤ 10^5
  • 1 ≤ operations[i][2] ≤ 10^5

More Airbnb problems

drafts saved locally
public boolean[] canFitSavedRectangles(int[][] operations) {
    // write your code here
}
operations[[1,1,1]]
expected[true]
checking account