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
32 changes: 32 additions & 0 deletions FindAllDisappearedNumsInArray.java
Original file line number Diff line number Diff line change
@@ -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<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> 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;

}
}
53 changes: 53 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -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<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;
}
}
}

}

public int countAlive(int[][] board, int i, int j, int rows, int columns){
int count = 0;
int[][] directions = new int[][] {{0,1}, {0,-1}, {-1, 0}, {1,0}, {-1,1}, {-1, -1}, {1,1}, {1,-1}};
for(int[] dir: directions){
int nr = i + dir[0]; // new row index
int nc = j + dir[1]; // new column index
if(nr >= 0 && nc >=0 && nr < rows && nc < columns && (board[nr][nc] == 1 || board[nr][nc] == 2)){
count++;
}
}
return count;
}
}
47 changes: 47 additions & 0 deletions MinMaxInArray.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<n; i+=2){
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]);
}

};
return new ArrayList<>(Arrays.asList(min, max));
}
}