KatInterview

Gravity Grid: Pushing Tokens Up from Below and Spotting Matching Lines

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingsimulationmatrixstate-tracking

Last updated

Simulate a grid where tokens are pushed in from the bottom of a column, and after each push report success and whether any full column or full row is single-typed.

Gravity Grid: Pushing Tokens Up from Below and Spotting Matching Lines

A grid has R rows and C columns. Every cell is either empty or contains a token of type "A" or "B". Row 0 is the bottom row and row R - 1 is the top row. Columns are numbered 0 to C - 1.

How tokens enter

Tokens are always pushed in from the bottom of a column:

  • Pushing a token into column c moves every token already in that column up by one row, and the new token is placed in row 0 of column c.
  • As a result, the tokens in any column always fill rows 0, 1, ..., k - 1 for some k, with no gaps.
  • If column c already contains R tokens, the push fails and the grid is left exactly as it was.

Task

You are given a list of pushes. Each push is a pair [c, p]: push a token of type p into column c. After handling every push, successful or not, record three booleans describing the grid at that moment:

  1. ok: true if the push succeeded, false if it failed because column c was full.
  2. uniform_column: true if at least one column is completely full (all R cells occupied) and all its tokens have the same type; otherwise false.
  3. uniform_row: true if at least one row is completely full (all C cells occupied) and all its tokens have the same type; otherwise false.

Return the list of these triples, one for each push, in the order the pushes were given.

Details

  • A column that is only partly filled never counts as uniform, even if all its current tokens match.
  • A row that is only partly filled never counts as uniform. Row r is completely filled exactly when every column holds more than r tokens.
  • After a failed push, the grid is unchanged, so uniform_column and uniform_row must equal their values from the grid state before that push.
  • A row that was uniform can stop being uniform after a push, because the push shifts tokens in one column upward. The flags must reflect the grid as it is right now.

Input

  • R: number of rows.
  • C: number of columns.
  • ops: list of pushes, each [c, p] with column index c and token type p ("A" or "B").

Output

A list with one entry per push, each entry being [ok, uniform_column, uniform_row] with boolean values, describing the grid right after that push.

Constraints

  • 1 <= R <= 50
  • 1 <= C <= 50
  • 1 <= len(ops) <= 10^4
  • 0 <= c < C
  • p is either "A" or "B"

Examples

Example 1

Input: simulate_gravity_grid(2, 2, [[0, 'A'], [1, 'A'], [0, 'B'], [1, 'B'], [1, 'A']])

Output: [[True, False, False], [True, False, True], [True, False, False], [True, False, True], [False, False, True]]

Hints

Hint 1

Store each column as a list where index r is the token in row r. A push is then an insertion at index 0, and a column is full when its length equals R.

Hint 2

Row r is completely filled only if every column has more than r tokens. Only then compare the tokens at index r across all columns.

Hint 3

A brute-force scan of the whole grid after each push is about 2.5 * 10^7 cell checks in total at the limits, which is fine. For a faster version, note that a full column never changes again, so its uniformity only needs to be decided once.