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

// 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;
}
}
67 changes: 67 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions MinAndMax.java
Original file line number Diff line number Diff line change
@@ -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<Integer> getMinMax(int[] arr) {
ArrayList<Integer> 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;
}
}