diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..370cdcbc --- /dev/null +++ b/Problem1.java @@ -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 findDisappearedNumbers(int[] nums) { + List res = new ArrayList<>(); + + for(int i = 0;i 0) { + nums[idx-1] *= -1; + } + } + + for(int i = 0;i 0) { + res.add(i+1); + } else { + nums[i] *= -1; + } + } + + return res; + } +} diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..993d0014 --- /dev/null +++ b/Problem2.java @@ -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 3) { + board[i][j] = 2; + } + } else if(currCell == 0){ + if(liveNeighbors == 3) { + board[i][j] = 3; + } + } + } + } + + for(int i = 0;i= 0 && nr < board.length && nc >= 0 && nc < board[row].length && (board[nr][nc] == 2 || board[nr][nc] == 1)) { + cntLive++; + } + } + + return cntLive; + } +}