Skip to content

Instantly share code, notes, and snippets.

@blacksheep557
Created October 7, 2021 05:19
Show Gist options
  • Select an option

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

Select an option

Save blacksheep557/de4d65b0c64c0d22ee3d93433b08248a to your computer and use it in GitHub Desktop.
function nQueenPlacement(board) {
let rowPositions = [];
let colPositions = [];
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if (board[i][j] === 1) {
if (rowPositions.indexOf(i) === -1 && colPositions.indexOf(j) === -1 && pairModulusDiffers(rowPositions, colPositions, i, j)) {
rowPositions.push(i);
colPositions.push(j);
}
else { return false; }
}
}
}
return true;
}
function pairModulusDiffers(rowPositions, colPositions, row, col) {
for (let i = 0; i < rowPositions.length; i++) {
if (Math.abs(rowPositions[i] - row) === Math.abs(colPositions[i] - col)) {
return false;
}
}
return true;
}
function join(wordsArr) {
let min = Infinity
while (wordsArr.length > 1) {
const merged = mergeWords(wordsArr.shift(), wordsArr.shift())
wordsArr.unshift(merged[0])
min = Math.min(merged[1], min)
}
return [wordsArr[0], min]
}
function mergeWords(word1, word2) {
let ctr = 0;
for (let i = 0; i < Math.min(word2.length, word1.length); i++) {
let z = word1.substring(word1.length - 1 - i)
let x = word2.substring(0, i + 1)
if (word1.substring(word1.length - 1 - i) === word2.substring(0, i + 1)) ctr = i
}
return [word1.substring(0, word1.length - 1 - ctr + 1) + word2.substring(ctr), ctr + 1]
}
console.log(join(["oven", "envier", "erase", "serious"])) //➞ ["ovenvieraserious", 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment