leetcode_1349
Given a m * n matrix seats that represent seats distributions in a classroom. If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character.
Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible..
Students must be placed in seats in good condition.
Constraints:
seats contains only characters '.' and'#'.
m == seats.length
n == seats[i].length
1 <= m <= 8
1 <= n <= 8
Solutions
dynamic programming with iteration O(m * 2^2n)
Since each row only depends on the previous row, we can solve this problem by dynamic programming.
dp[i][state]
represents the total number of students that can take the exam without any cheating in firsti
rows and the arrangement of thei'th
row isstate
.An arrangement can be represented by an
integer
as the number of columns is smaller than8
.For example,
##0##1#10
can be represented as2^5 + 2^7
.
recursion with memoization
In the first version, the calculation of all states is unnecessary, a top-down strategy can save a huge amount of running time though the time complexity is the same.
graph
Last updated
Was this helpful?