Maximum Students Taking Exam - Problem

Imagine you're a proctor overseeing an exam in a classroom with m ร— n seats arranged in a grid. Some seats are broken (marked with '#') while functional seats are marked with '.'.

Here's the challenge: students can cheat by looking at answers from their neighbors! Specifically, a student can see the answers of those sitting:

  • Directly to their left
  • Directly to their right
  • To their upper left diagonal
  • To their upper right diagonal

However, students cannot see answers from those sitting directly in front or behind them.

Your goal is to find the maximum number of students that can take the exam simultaneously without any possibility of cheating. Students can only be placed in functional seats (marked with '.').

Example: In a classroom where students are seated like chess pieces, you need to ensure no student can peek at their neighbor's paper!

Input & Output

example_1.py โ€” Basic Classroom
$ Input: seats = [["#",".","#","#",".","#"],[".","#","#","#","#","."],["#",".","#","#",".","#"]]
โ€บ Output: 4
๐Ÿ’ก Note: The optimal arrangement places students at positions (0,1), (0,4), (1,0), and (1,5). No student can see another student's answers due to the seating pattern and broken seats blocking some sight lines.
example_2.py โ€” All Broken Seats
$ Input: seats = [["#","#"],["#","#"],["#","#"]]
โ€บ Output: 0
๐Ÿ’ก Note: All seats are broken, so no students can take the exam. The maximum number of students is 0.
example_3.py โ€” Single Row
$ Input: seats = [[".","#",".",".","#","."]]
โ€บ Output: 3
๐Ÿ’ก Note: In a single row, we can place students at positions (0,0), (0,2), and (0,5). Position (0,3) cannot be used because it would be adjacent to the student at (0,2).

Visualization

Tap to expand
Exam Hall: Maximum Students Without CheatingClassroom Layout:ร—S1S2ร—S3ร—S4Legend:Student (Safe)Broken SeatEmpty (Safe)Sight Lines (Cheating Possibilities):โ† Same row conflict avoided โ†’โ†™ Diagonal sight lineโœ… Maximum 4 students can take exam safely!Students placed at: (0,1), (0,4), (1,0), (1,5)
Understanding the Visualization
1
Identify Safe Zones
Mark all functional seats and identify potential cheating sight lines
2
Row-by-Row Placement
For each row, determine all valid student arrangements (no adjacent seats)
3
Check Diagonal Conflicts
Ensure no student in current row can see diagonally to previous row
4
Optimize Transitions
Use dynamic programming to find the maximum valid placement across all rows
Key Takeaway
๐ŸŽฏ Key Insight: This is a constrained optimization problem where we use bitmask DP to efficiently explore all valid seating arrangements row by row, ensuring no cheating is possible through strategic placement.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m ร— 4^n ร— 2^n)

m rows, up to 4^n transitions between valid masks, 2^n possible masks per row

n
2n
โš  Quadratic Growth
Space Complexity
O(m ร— 2^n)

DP table stores maximum students for each row and valid mask combination

n
2n
โš  Quadratic Space

Constraints

  • m == seats.length
  • n == seats[i].length
  • 1 โ‰ค m โ‰ค 8
  • 1 โ‰ค n โ‰ค 8
  • seats[i][j] is either '.' or '#'
Asked in
Google 45 Microsoft 32 Amazon 28 Meta 15
34.4K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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