diff --git a/DisappearedNumbers.java b/DisappearedNumbers.java new file mode 100644 index 00000000..2392a82a --- /dev/null +++ b/DisappearedNumbers.java @@ -0,0 +1,31 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class Solution { + public List findDisappearedNumbers(int[] nums) { + List res = new ArrayList(); + + // null check + if (nums == null || nums.length == 0) return res; + + /* TEMPORARY STAGE CHANGE PATTERN */ + // mark all the indices for which value is encountered + for (int i = 0; i < nums.length; i++) { + int idx = Math.abs(nums[i])-1; + if (nums[idx] > 0) // if not already visited + nums[idx] *= -1; // mark visited + } + + // unmark visited elements & add the unvisited elements in the result + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0) + nums[i] *= -1; + else + res.add(i+1); + } + + return res; + } +} \ No newline at end of file diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..b923a940 --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,67 @@ +// Time Complexity : O(m*n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +class Solution { + int m; + int n; + int[][] dirs; + + public void gameOfLife(int[][] board) { + m = board.length - 1; + n = board[0].length - 1; + + // null matrix check + if (board == null || m < 0) + return; + + // all possible directions to calculate neighbors + dirs = new int[][] {{0,1}, {0,-1}, {1,0}, {-1,0}, {-1,-1}, {1,1}, {-1,1}, {1,-1}}; + + + int lives = 2; // 0 -> 1 + int died = 3; // 1 -> 0 + + for (int i=0; i <= m; i++) { + for (int j=0; j <= n; j++) { + int liveNeighbors = countLiveNeighbor(board, i, j); //count live neighbors + int cell = board[i][j]; + + /* Rules */ + if (cell == 1 && liveNeighbors < 2) { + board[i][j] = died; + } else if (cell == 1 && liveNeighbors > 3) { + board[i][j] = died; + } else if (cell == 0 && liveNeighbors == 3) { + board[i][j] = lives; + } + } + } + + // changing back the temporary states to 0 or 1 + for (int i=0; i <= m; i++) { + for (int j=0; j <= n; j++) { + if (board[i][j] == lives) + board[i][j] = 1; + if (board[i][j] == died) + board[i][j] = 0; + } + } + + } + + + private int countLiveNeighbor(int[][] board, int i, int j){ + int res = 0; + for(int[] dir : dirs) { + int nr = dir[0] + i; // new row + int nc = dir[1] + j; // new column + + if (nr>=0 && nc>=0 && nr<=m && nc<=n && (board[nr][nc]==1 || board[nr][nc]==3)){ + res++; + } + } + return res; + } +} \ No newline at end of file diff --git a/MinAndMax.java b/MinAndMax.java new file mode 100644 index 00000000..fb8dcf8a --- /dev/null +++ b/MinAndMax.java @@ -0,0 +1,34 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +/** + * Comparing in pairs + * Edge case: Odd length + * */ +class Solution { + public ArrayList getMinMax(int[] arr) { + ArrayList result = new ArrayList<>(); + int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, i =0; + int n = arr.length; + if(n % 2 == 1){ // for odd length, set min & max as the first element + min = arr[i]; + max = arr[i]; + i = 1; + } + while (i < n-1){ + if(arr[i] > arr[i+1]){ + max = Math.max(max, arr[i]); + min = Math.min(min, arr[i+1]); + }else{ + max = Math.max(max, arr[i+1]); + min = Math.min(min, arr[i]); + } + i+=2; + } + result.add(min); + result.add(max); + return result; + } +}