diff --git a/FindAllDisappearedNumsInArray.java b/FindAllDisappearedNumsInArray.java new file mode 100644 index 00000000..4a24d8b3 --- /dev/null +++ b/FindAllDisappearedNumsInArray.java @@ -0,0 +1,32 @@ +// 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 +// 1: First we iterate through the array and for each number num, make the corresponding number at index [num-1] negative +// 2: Then the next iteration is to determine which remaining elements are positive +// 3: If we find a positive element, we return the index + 1. At the end, we revert the input array to its original elements +class Solution { + public List findDisappearedNumbers(int[] nums) { + List result = new ArrayList<>(); + int n = nums.length; + for (int i = 0; i < n; i++) { + int idx = Math.abs(nums[i]) - 1; + if (nums[idx] > 0) { + nums[idx] = nums[idx] * -1; + } + } + + for (int i = 0; i < n; i++) { + if (nums[i] > 0) { + result.add(i + 1); + } else { + nums[i] *= -1; + } + } + return result; + + } +} \ No newline at end of file diff --git a/GameOfLife.java b/GameOfLife.java new file mode 100644 index 00000000..005ebb30 --- /dev/null +++ b/GameOfLife.java @@ -0,0 +1,53 @@ +// Time Complexity : O(mn) +// 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 +// 1: To calculate the live cells next to a given cell without changing the original matrix, we employ a set of rules to change the value in place +// 2: For 0 -> 1 we change the value to 3 and for 1 -> 0 we change the value to 2 +// 3: Thus for each direction we are able to calculate the number of live cells, and at the end of calculation we can easily revert the numbers back to their new form +class Solution { + public void gameOfLife(int[][] board) { + int m = board.length; //rows + int n = board[0].length; // columns + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int countAlives = countAlive(board, i, j, m, n); // count all live cells around a given cell + if (board[i][j] == 1 && (countAlives < 2 || countAlives > 3)) // has to be marked dead + { + board[i][j] = 2; + } + if(board[i][j] == 0 && (countAlives == 3)) // dead cell -> alive + { + board[i][j] = 3; + } + } + } + for(int i = 0; i= 0 && nc >=0 && nr < rows && nc < columns && (board[nr][nc] == 1 || board[nr][nc] == 2)){ + count++; + } + } + return count; + } +} \ No newline at end of file diff --git a/MinMaxInArray.java b/MinMaxInArray.java new file mode 100644 index 00000000..48db79aa --- /dev/null +++ b/MinMaxInArray.java @@ -0,0 +1,47 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes on GFG +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +// 1: We consider the array in pairs and calculate the min/max between each pair +// 2: Through this approach, instead of 2n comparisons, we do 1.5n comparisons +// 3: For odd length array, we start the array at int 1 instead of 2 +class Solution { + public ArrayList getMinMax(int[] arr) { + int n = arr.length; + int min = 0, max = 0; + int i = 0; + + // Initialize min and max + if (n % 2 == 1) { + min = arr[0]; + max = arr[0]; + i = 1; + } else { + if (arr[0] < arr[1]) { + min = arr[0]; + max = arr[1]; + } else { + min = arr[1]; + max = arr[0]; + } + i=2; + } + + // Process elements in pairs + for(; 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]); + } + + }; + return new ArrayList<>(Arrays.asList(min, max)); + } +}