Skip to the code
+25 XP

#25 Valid Seating Chart

medium

An event app lets organizers assign guests from table groups 1 to n to an n x n seating chart, grid. A 0 means the seat is empty.

The chart is valid if no row and no column has the same non-zero group number twice. Write validSeatingChart(grid) that returns true if the chart is valid and false otherwise.

Charts for big venues can be 1,500 seats on each side, so comparing every pair of seats in a row or a column is too slow.

Example 1

Input: grid = [[1,2,0],[0,3,1],[2,0,3]]
Output: true

Every row and every column has different non-zero numbers.

Example 2

Input: grid = [[1,2],[1,0]]
Output: false

Column 0 has group 1 twice.

Constraints

  • 1 <= n <= 1,500
  • 0 <= grid[r][c] <= n
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).