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