858. Exam Hall Seating

An exam hall is a grid seats with m rows and n columns. A cell holding '.' is a usable seat and a cell holding '#' is a broken seat nobody can use. A student can peek at the paper of someone sitting directly to their left, directly to their right, to their upper-left or to their upper-right. Students cannot peek straight ahead, behind, or at lower diagonals.

Place students on usable seats so that nobody can peek at anybody else, and return the largest number of students that can be seated. An empty hall (all seats broken) gives 0.

Example 1

Input:
seats = [[".","#",".","."],[".",".",".","."],["#",".",".","#"]]
Output:
5
Explanation:

Seats are the '.' cells; placing students at the positions chosen by the best arrangement gives 5 students with nobody seeing a neighbour's paper to the left, right, upper-left or upper-right.

Example 2

Input:
seats = [[".",".","."]]
Output:
2
Explanation:

Only one row with three seats: students cannot sit side by side, so the two end seats give the best total of 2.

Constraints

  • 1 ≤ m, n ≤ 8
  • seats[i][j] is either '.' or '#'

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 400 msC++ 100 msJava 200 msJavaScript 200 msTypeScript 200 ms

Expected complexity

Time
O(m * 4^n)
Space
O(2^n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…