diff --git a/FindAllNoDisappearedInArray.java b/FindAllNoDisappearedInArray.java new file mode 100644 index 00000000..d2da45e3 --- /dev/null +++ b/FindAllNoDisappearedInArray.java @@ -0,0 +1,34 @@ +import java.util.List; +import java.util.ArrayList; +// Time Complexity : O(N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach +class Solution { + public List findDisappearedNumbers(int[] nums) { + for(int i = 0; i < nums.length; i++) + { + int numIndex = Math.abs(nums[i]) - 1; + if(nums[numIndex] > 0) + { + nums[numIndex] *= -1; + } + } + List result = new ArrayList<>(); + for(int i = 0; i < nums.length; i++) + { + if(nums[i] < 0) + { + nums[i] *= -1; + } + else + { + result.add(i+1); + } + } + return result; + } +} diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..8fd97c10 --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,71 @@ +// Time Complexity : O(M*N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + +class Solution { + public void gameOfLife(int[][] board) { + // 0 - dead, 1 - live + // 1 -> < 2 live 1 -> 0 + // 1 -> == 2 or == 3 live 1 -> 1 + // 1 -> >3 live 1 -> 0 + // 0 -> == 3 live 1 -> 1 + // temporary pattern + //map 0 -> 1 -> 2 + //map 1 -> 0 -> 3 + for(int i = 0; i < board.length; i++) + { + for(int j = 0; j < board[0].length; j++) + { + int liveNeighbors = checkLive(i, j, board); + if(board[i][j] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) + { + board[i][j] = 3; + } + else if(board[i][j] == 0 && liveNeighbors == 3) + { + board[i][j] = 2; + } + } + } + + for(int i = 0; i < board.length; i++) + { + for(int j = 0; j < board[0].length; j++) + { + if(board[i][j] == 2) + { + board[i][j] = 1; + } + else if(board[i][j] == 3) + { + board[i][j] = 0; + } + } + } + } + + private static int checkLive(int row, int col, int[][] board) + { + int[][] neighbors = new int[][]{{0,-1},{0,1},{-1,0},{-1,-1},{-1,1},{1,0},{1,-1},{1,1}}; + int result = 0; + + for(int[] neighbor : neighbors) + { + int nr = row + neighbor[0]; + int nc = col + neighbor[1]; + if(nr >= 0 && nc >= 0 && nr < board.length && nc < board[0].length) + { + if(board[nr][nc] == 1 || board[nr][nc] == 3) + { + result++; + } + } + } + + return result; + } +} \ No newline at end of file