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


// Your code here along with comments explaining your approach
/*
I can use the element of the nums and go to its respective idx and change it value to negative.
In the 2nd pass, I will check if there are any numbers where are positive.
If there are it means that these index ~ numbers are missing
*/

import java.util.ArrayList;
import java.util.List;

public class Problem1 {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();

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

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

return res;
}
}
66 changes: 66 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Time Complexity : O(M*N) + O(M*N) ~ O(M*N)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No


// Your code here along with comments explaining your approach
/*
If 1 is live, but dead I marked it as 2. If 0 becomes alive I marked it as 3.
I used the temporary state change pattern. I first counted the live neighbors by equating it with 2 or 1, where 2 indicates it was alive in the initital states.
Once I had the liveNeighbors I applied the rules to change the state temporarily and then at the end, if it 3 I changed to 1, if it is 2 changed it to 0
*/

public class Problem2 {
private static int[][] dir = {{-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0,-1}};

public void gameOfLife(int[][] board) {
// 1 -> 0 ~ 2
// 0 -> 1 ~ 3

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

for(int i = 0;i<m;i++) {
for(int j = 0;j<n;j++) {
int currCell = board[i][j];
int liveNeighbors = countLiveNeighbours(board, i, j);
if(currCell == 1) {
if(liveNeighbors < 2) {
board[i][j] = 2;
} else if(liveNeighbors > 3) {
board[i][j] = 2;
}
} else if(currCell == 0){
if(liveNeighbors == 3) {
board[i][j] = 3;
}
}
}
}

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 countLiveNeighbours(int[][] board, int row, int col) {
int cntLive = 0;
for(int[] d : dir) {
int nr = row + d[0];
int nc = col + d[1];

if(nr >= 0 && nr < board.length && nc >= 0 && nc < board[row].length && (board[nr][nc] == 2 || board[nr][nc] == 1)) {
cntLive++;
}
}

return cntLive;
}
}