Maximum Students Taking Exam - Problem

Given an m × n matrix seats that represents seat distributions in a classroom. If a seat is broken, it is denoted by '#' character, otherwise it is denoted by '.' character.

Students can see the answers of those sitting next to them on the left, right, upper left and upper right, but they cannot see the answers of students sitting directly in front or behind them.

Return the maximum number of students that can take the exam together without any cheating being possible.

Note: Students must be placed in seats that are in good condition (marked with '.').

Input & Output

Example 1 — Basic Case
$ Input: seats = [[".","#"],["#","#"],["#","."],["#","#"],[".","#"]]
Output: 1
💡 Note: Only one student can be placed at seats[0][0] or seats[2][1] or seats[4][0] without any cheating possibility
Example 2 — Multiple Students
$ Input: seats = [[".",".","."],[".",".","."],[".",".","."]]
Output: 4
💡 Note: Students can be placed at (0,0), (0,2), (2,0), (2,2) in a checkerboard pattern to avoid cheating
Example 3 — All Broken Seats
$ Input: seats = [["#","#"],["#","#"]]
Output: 0
💡 Note: No students can be placed since all seats are broken

Constraints

  • 1 ≤ seats.length ≤ 8
  • 1 ≤ seats[r].length ≤ 8
  • seats[r][c] is either '.' or '#'

Visualization

Tap to expand
Maximum Students: Prevent Diagonal & Side CheatingInput Matrix.#....Optimal SolutionS#SStudents placed diagonally apart to prevent cheatingMaximum Students: 2Green = Student, Red = Broken, White = Empty
Understanding the Visualization
1
Input
Classroom with available (.) and broken (#) seats
2
Constraint
Students can cheat diagonally and sideways
3
Solution
Place maximum students without cheating
Key Takeaway
🎯 Key Insight: Use bitmask DP to efficiently track valid seating arrangements row by row
Asked in
Google 15 Facebook 12 Microsoft 8
23.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen