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
21 changes: 21 additions & 0 deletions DisappearedNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<>();
HashSet<Integer> set = new HashSet<>();
int n = nums.length;

for (int i = 0; i < n; i++) {
set.add(nums[i]); // add all the elements to has set
}

for (int i = 1; i <= n; i++) {
if (!set.contains(i)) { //check whether set contains the nu,ber or not
result.add(i);
}
}
return result;
}
}

Time complexity : O(n)
space complexity: O(n)
52 changes: 52 additions & 0 deletions LifeGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution {
int[][] dirs;
int m,n;

public void gameOfLife(int[][] board) {

this.dirs = new int[][]{{-1,-1}, {-1,0}, {-1, 1}, {0, -1}, {0, 1}, {1,-1}, {1,0}, {1,1}};

this.m = board.length;
this.n = board[0].length;

for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
int count = getCount(board, i, j); // number of alive cells around it
if(board[i][j] == 0 && count == 3){
board[i][j] = 3; // dead -> alive;
}else if(board[i][j] == 1 && (count < 2 || count >3)){
board[i][j] = 2; // alive -> dead;
}
}
}

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;
}
}
}
}

private int getCount(int[][] board, int i, int j){
int count = 0;

for(int[] dir: dirs){
int r = i + dir[0];
int c = j + dir[1];

if(r>=0 && c>=0 && r<m && c<n){
if(board[r][c] == 1 || board[r][c] == 2) count++;
}
}

return count;
}
}


Time complexity : O(m*n)
Space Complexity : O(1)