Skip to content

Instantly share code, notes, and snippets.

@kavitshah8
Created February 8, 2017 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kavitshah8/01be7856f973ae62e8a435e28c3f7539 to your computer and use it in GitHub Desktop.
Save kavitshah8/01be7856f973ae62e8a435e28c3f7539 to your computer and use it in GitHub Desktop.
CareerCup
function isZeroArr(arr) {
if (!arr.length) return false
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] != 0) return false;
}
return true;
}
function isPossible(row, folks) {
var rowLen = row.length
if (!rowLen) return false
if (rowLen < folks) return false
if (rowLen == 1 && (folks == 1 || folks == 0)) return true
// Boundary
if (row[0] == 0) {
let t = row.slice(0, folks + 2)
if(isZeroArr(t)) return true
}
if (row[rowLen - 1] == 0) {
let s = row.slice(rowLen - folks - 1, rowLen)
if(isZeroArr(s)) return true
}
// Middle
let expectedEmptySeats = folks + 2;
let numEmptySeats = 0;
for (let i = 1; i < rowLen - 1; i++) {
if (row[i] == 0) numEmptySeats++
if (numEmptySeats >= expectedEmptySeats) return true
if (row[i] == 1) numEmptySeats = 0
}
return false
}
var ar1 = [0];
console.log(isPossible(ar1, 0)) // true
console.log(isPossible(ar1, 1)) // true
console.log(isPossible(ar1, 2)) // false
// Boundary
var ar2 = [1,0,0,1,0,0,1,0,0];
console.log(isPossible(ar2, 1)) // true
console.log(isPossible(ar2, 2)) // false
// Middle
var arr3 = [1, 0, 0, 0, 0, 0, 1, 0, 0];
console.log(isPossible(arr3, 3)) // true
console.log(isPossible(arr3, 4)) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment