Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created September 14, 2021 05:00
Show Gist options
  • Select an option

  • Save blacksheep557/621dcc851ff80a57844eca89eb4b1eeb to your computer and use it in GitHub Desktop.

Select an option

Save blacksheep557/621dcc851ff80a57844eca89eb4b1eeb to your computer and use it in GitHub Desktop.
function onesInfection(matrix) {
const rowSet = new Set()
const colSet = new Set()
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 1) {
rowSet.add(i)
colSet.add(j)
}
}
}
rowSet.forEach(row => transformRow(row, matrix))
colSet.forEach(col => transformCol(col, matrix))
return matrix
}
function transformRow(index, matrix) {
matrix[index] = matrix[index].map(val => 1)
}
function transformCol(index, matrix) {
matrix = matrix.map(row => row[index] = 1)
}
function covidSeating(row) {
let emptySeats = 0;
let res = 0
for (let i = 0; i < row.length; i++) {
if (row[i] == 0) {
emptySeats += 1
if (row[i - 1] && row[i - 1] == 1) emptySeats -= 1
if (row[i + 1] && row[i + 1] == 1) {
emptySeats -= 1
res += Math.ceil(emptySeats / 2)
emptySeats = 0
}
}
}
res += Math.ceil(emptySeats / 2)
return res
}
console.log(covidSeating("10001")); // 1
// Can take place in the middle.
console.log(covidSeating("00101"));// 1
// Can take place on the left.
console.log(covidSeating("0"));// 1
// Can take one place.
console.log(covidSeating("000")); // 2
// Can take places on the left and on the right.
console.log(covidSeating("100")); // 1
// Can take place on the right.
console.log(covidSeating("101")); // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment