Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions findDisappearedNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {number[]} nums
* @return {number[]}
Intuition:
Since the array contains numbers from 1 to n and has length n, every value corresponds to a valid index in the array (value − 1).
I am using the input array itself as a hash map by marking visited numbers as negative. For each number, we treat it's absolute value as a position and mark that index negative to indicate that this number exists.
After the first pass, any index that remains positive means its corresponding number (index + 1) was never seen in the array, so we add it to the result. This lets us find all missing numbers in O(n) time without extra space.
*/
var findDisappearedNumbers = function(nums) {
// first pass
for(let i = 0; i< nums.length; i++){
const currNum = nums[i];
const indexSupposedToBeIn = Math.abs(currNum)-1;
if(nums[indexSupposedToBeIn] > 0) nums[indexSupposedToBeIn] *= -1;
}
const result = [];
// second pass
for(let i = 0; i<nums.length;i++){
if(nums[i]>0){
result.push(i+1);
}
}
return result;
};
54 changes: 54 additions & 0 deletions gameOfLife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @param {number[][]} board
* @return {void} Do not return anything, modify board in-place instead.
Inuition:

The key idea is to update the board in-place without losing the original state needed to count neighbors.

First scan each cell and determine its fate based on its live neighbors. Instead of changing 1 → 0 or 0 → 1 immediately, we temporarily mark transitions ('Y' for 1→0 and 'X' for 0→1).This allows us to still correctly count original live neighbors while processing the board.

After the first pass, we do a second pass to finalize the state by converting 'Y'to 0 and'X'` to 1. This ensures the rules of the Game of Life are applied correctly using only constant extra space.
*/
var gameOfLife = function (board) {
const m = board.length;
const n = board[0].length;
// 8 directions
const directions = [
[0, -1], [0, 1], [1, 0], [-1, 0],
[1, 1], [-1, -1], [1, -1], [-1, 1]
];

const getNeighborCount = (row, col) => {
let count = 0;
for (let [rowAdd, colAdd] of directions) {
const newRow = row + rowAdd;
const newCol = col + colAdd;
if (newRow < 0 || newCol < 0 || newRow >= m || newCol >= n) continue;
if (board[newRow][newCol] === 1 || board[newRow][newCol] === 'Y') count++;
}
return count;
}

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
// get the live neighbors count
const count = getNeighborCount(i, j);
// if 0 turns to 1, mark it as X else Y
if (board[i][j] === 1) {
// dies by over and under population
if (count < 2 || count > 3) board[i][j] = 'Y'; // 1-> 0
} else if (board[i][j] === 0) {
if (count === 3) board[i][j] = 'X'; // 0 -> 1
}

}
}

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (board[i][j] === 'Y') board[i][j] = 0;
else if (board[i][j] === 'X') board[i][j] = 1;
}
}
return board;
};