#1349

Maximum Students Taking Exam

candidate master · 1335 · lc hard +32 · verified · 53.4% accepted · 896 likes · top 44%

Description

You are given an m * n classroom seating matrix seats where '.' is a usable seat and '#' is broken. A student at one seat can see the answers of any neighbor directly to the left, right, upper-left, or upper-right (but not directly in front or behind). Place as many students as possible in working seats so that no student can see another's work. Return the maximum number of students that can sit for the exam.

Example 1:

Input: seats = [["#",".","#","#",".","#"],
[".","#","#","#","#","."],
["#",".","#","#",".","#"]]
Output: 4
Explanation: Teacher can place 4 students in available seats so they don't cheat on the exam.

Example 2:

Input: seats = [[".","#"],
["#","#"],
["#","."],
["#","#"],
[".","#"]]
Output: 3
Explanation: Place all students in available seats.

Example 3:

Input: seats = [["#",".",".",".","#"],
[".","#",".","#","."],
[".",".","#",".","."],
[".","#",".","#","."],
["#",".",".",".","#"]]
Output: 10
Explanation: Place students in available seats in column 1, 3 and 5.

Code

1
2
3