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
36 changes: 36 additions & 0 deletions DisappearedNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity : O(N)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Approach
// Sort and search linearly - O(nlogn) time, O(1) space
// keep hash set and search linearly for missing value - O(n) time, O(n) space
// Do inplace negative marking - O(n) time, O(1) space.
// Marking nums[nums[i]] as negative indicates nums[i] is present in the array.
import java.util.ArrayList;
import java.util.List;

class DisappearedNumbers {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
int temp = Math.abs(nums[i]-1);
// Mark it negative only if it is positive else, in case of duplicates,
// -(- val) will become positive
if(nums[temp] > 0){
nums[temp] *= -1;
}
}

// Value which are still positive do not have a corresponding number in the array.
for(int i = 0; i < nums.length; i++){
if(nums[i] > 0){
result.add(i+1);
}
}

return result;
}
}
67 changes: 67 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Time Complexity : O(M*N), Go through each cell in the board
// Space Complexity : O(1),
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No

// Approach:
// We can do this by counting live neighbors at each cell. Copy the matrix and apply the rules to each cell and decide whether
// the cell is live or dead. Extra space is needed - O(M*N).
// To do it in place, we need to identify the cells that have gone from live to dead and vice versa, so we use another
// number 2 to represent 1-> 0 and 3 as to represent state 0->1.
// 1 --> 0 = 2
// 0 --> 1 = 3
public class GameOfLife {
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;

// directions for finding 8 neighbors
int[][] dirs = {{0,1},{1,0}, {0,-1}, {-1,0}, {-1,1}, {1,-1}, {1,1}, {-1,-1}};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int liveNeighbors = getLiveNeighborsCount(board, i, j, dirs);
// Apply rules if cell is live
if(board[i][j] == 1){
if(liveNeighbors < 2 || liveNeighbors > 3){
board[i][j] = 2;
}
}else {
// Apply rules if cell is dead
if(liveNeighbors == 3){
board[i][j] = 3;
}
}
}
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// replace 3 with 1 and 2 with 0, the state they actually represents
if(board[i][j] == 3){
board[i][j] = 1;
}

if(board[i][j] == 2){
board[i][j] = 0;
}
}
}
}

private int getLiveNeighborsCount(int[][] board, int i, int j, int[][] dirs) {
int count = 0;
for (int[] dir : dirs){
int newRow = dir[0] + i;
int newCol = dir[1] + j;
if(newRow < 0 || newCol < 0 ) continue;
if(newRow >= board.length || newCol >= board[0].length) continue;

// count if there are live neighbors + the neighbors that were live before getting updated.
if(board[newRow][newCol] == 1 || board[newRow][newCol] == 2){
count++;
}
}

return count;
}
}