Problem · Matrix

Place Ordered Figures on a Grid

Learn this problem
MediumxAI logoxAIFULLTIMEOA

Problem statement

Create an n x m grid initially filled with 0. Place the requested figures in their given order. Figure figures[i] is drawn using the integer i + 1.

Figure cells

Coordinates below are offsets from a candidate anchor (row, col).

  • A: (0,0).
  • B: (0,0), (0,1), (0,2).
  • C: (0,0), (0,1), (1,0), (1,1).
  • D: (0,0), (1,0), (1,1), (2,0).
  • E: (0,1), (1,0), (1,1), (1,2).

Figures may not be rotated and may not overlap an already occupied cell. For each figure, choose a valid anchor with the smallest row index; among those, choose the smallest column index. It is guaranteed that every figure can be placed.

Return the grid after all figures are drawn.

Function

placeFigures(n: int, m: int, figures: char[]) → int[][]

Examples

Example 1

n = 4m = 4figures = ["D","B","A","C"]return = [[1,2,2,2],[1,1,3,0],[1,4,4,0],[0,4,4,0]]

D occupies the first valid top-left anchor. B then fits first at row 0, column 1; the later figures follow the same row-major priority.

Example 2

n = 3m = 5figures = ["A","D","E"]return = [[1,2,0,0,0],[0,2,2,3,0],[0,2,3,3,3]]

After A and D, the earliest valid anchor for E is row 1, column 2.

Constraints

  • 1 <= n, m <= 100.
  • 1 <= figures.length <= n * m.
  • Every entry of figures is one of A, B, C, D, or E.
  • All requested figures can be placed under the stated rules.

More xAI problems

drafts saved locally
public int[][] placeFigures(int n, int m, char[] figures) {
    // Write your code here.
}
n4
m4
figures["D","B","A","C"]
expected[[1,2,2,2],[1,1,3,0],[1,4,4,0],[0,4,4,0]]
checking account