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
61 changes: 61 additions & 0 deletions Leetcode289.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Solution {
// This method will count all the ones in the neighbouring indices
private int countOnes(int[][] board, int row, int col) {
int[][] ids = new int[][] { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 },
{ 1, 1 } };
int count = 0;
for (int[] id : ids) {
int r = row + id[0];
int c = col + id[1];
if (r >= 0 && r < board.length && c >= 0 && c < board[0].length) {
if (board[r][c] == 1 || board[r][c] == 2) {
count += 1;
}
}
}

return count;
}

public void gameOfLife(int[][] board) {
// 1 -> 2 -> 0 if < 2 1's
// 1 -> 1 -> 1 if =2,3 1's
// 1 -> 2 -> 0 if >3 1's
// 0 -> 3 -> 1 if =3 1's
int m = board.length;
int n = board[0].length;

for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
int count = countOnes(board, r, c);
if (board[r][c] == 0 && count == 3) {
board[r][c] = 3; // convert dead state to temp state (3) indicating it was in dead state earlier
// and needs to move to live state.

} else if (board[r][c] == 1) {
if (count < 2 || count > 3) {
board[r][c] = 2; // convert live state to temp state (2) indicating it was in live state earlier
// and needs to move to dead state.

}
}

}
;
}

// At this point the board matrix will have the temp state so we'll convert 2->0
// and 3->1
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 2) {
board[i][j] = 0;
} else if (board[i][j] == 3) {
board[i][j] = 1;
}

}
}

}
}
28 changes: 28 additions & 0 deletions Leetcode448.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TC - O(n)
// SC - O(1)
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();

for (int i = 0; i < nums.length; i++) {
int val = nums[i];
if (val < 0) {
val = val * (-1);
}
if (nums[val - 1] > 0) {
nums[val - 1] = -1 * nums[val - 1];
}

}

for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
res.add(i + 1);
}

}

return res;

}
}