1
var maxStudents = function (seats) {2
if (!seats.length) return 0;4
// precalculate the seat that will be off the matrix5
const lastPos = 1 << seats[0].length;6
// encode the classroom into binary where 1 will be an available seat and 0 will be a broken seat7
const classroom = seats.map((row) =>8
row.reduce((a, c, i) => (c === "#" ? a : a | (1 << i)), 0)11
// create a store to store the max seating for each row arrangment12
// each row will be off +1 of seats rows13
const dp = new Array(seats.length + 1).fill(null).map(() => new Map());14
// since no one can sit at row=-1 set the max number of no one sitting on that row as 017
// iterate through every row in seats matrix18
// remember that dp's rows are +1 of seats rows19
for (let row = 0; row < seats.length; row++) {20
// start with no one sitting on the row22
// this will count the number of students sitting on this row23
// we will increment this up as we add students to the row26
while (queue.length > 0) {27
// this will store all the different arrangments possible when we add 1 more student from the previous30
// iterate through all the previous arrangements31
// calculate the maximum number of students that can sit on current and all previous rows32
// try adding a student to all possible available seats meeting the rules of the game33
for (let arrangement of queue) {34
// this calculates the maximum number of students that can sit with this row arrangment35
// if the arrangement doesn't allow any students to sit in the previous row this will be handled36
// by the 0 case in the previous row37
// *see helper function that checks to see if the arrangement conflicts with the previous arrangment39
for (let [prevArrang, count] of dp[row]) {40
if (conflicts(prevArrang, arrangement)) continue;41
max = Math.max(max, count + numStudents);43
dp[row + 1].set(arrangement, max);45
// try adding an additional student to all elements in the row checking if it conflicts46
// arrangement | i => adds the student to the arrangement47
for (let i = 1; i < lastPos; i <<= 1) {48
if (canSit(classroom[row], arrangement, i))49
next.push(arrangement | i);58
// return the maximum value from the last row59
// the last row holds the maximum number of students for each arrangment60
return Math.max(...dp[seats.length].values());63
// this checks if the there are any students to the left or right of current arrangment64
// curr << 1 => checks if there are any students to the left65
// curr >> 1 => check if there are any students to the right66
function conflicts(prev, curr) {67
return prev & (curr << 1) || prev & (curr >> 1);70
// this checks if we can place the new student in this spot71
// row & newStudent => checks if the classroom's row's seat is not broken72
// arrangment & newStudent => will return truthy if there is a student sitting in that position (so we ! it because we don't want anyone sitting there)73
// arrangement & (newStudent << 1) => will return truthy if there is a student sitting to the left of that position (so we ! it because we don't want anyone sitting there)74
// arrangement & (newStudent >> 1) => will return truthy if there is a student sitting to the right of that position (so we ! it because we don't want anyone sitting there)75
function canSit(row, arrangement, newStudent) {78
!(arrangement & newStudent) &&79
!(arrangement & (newStudent << 1)) &&80
!(arrangement & (newStudent >> 1))