Skip to content
Open

Array 2 #1871

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
65 changes: 65 additions & 0 deletions GameofLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the board
// Space Complexity : O(N*M) for the new board to store the next state of the cells
// 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
// The problem is to implement the Game of Life, which is a cellular automaton.
// The board is a 2D grid where each cell can be either alive (1) or dead (0).
// The next state of each cell is determined by the number of live neighbors it has.
// We define the 8 possible directions to check for neighbors.
// We iterate through each cell in the board and count the number of live neighbors for that cell.
// Based on the count of live neighbors and the current state of the cell, we apply the rules of the Game of Life to determine the next state of the cell.
// We store the next state in a new board to avoid modifying the original board while we are still counting neighbors.
// After processing all cells, we copy the new board back to the original board to update it with the next state.
// This is O(N *M) time complexity because we are iterating through each cell and checking its neighbors, and O(N) space complexity because we are using an additional board to store the next state of the cells.
public void gameOfLife(int[][] board) {

int[][] dir = new int[][] {
{-1, -1}, {-1, 0}, {-1, 1},
{ 0, -1}, { 0, 1},
{ 1, -1}, { 1, 0}, { 1, 1}
};

int row = board.length;
int col = board[0].length;

int[][] newBoard = new int[row][col];

for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {

int count = 0;

// count live neighbors
for (int[] d : dir) {
int p = i + d[0];
int q = j + d[1];

if (p >= 0 && q >= 0 && p < row && q < col && board[p][q] == 1) {
count++;
}
}

// Game of Life rules
if (board[i][j] == 1) {
if (count == 2 || count == 3) {
newBoard[i][j] = 1;
}
} else {
if (count == 3) {
newBoard[i][j] = 1;
}
}
}
}

// copy back
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
board[i][j] = newBoard[i][j];
}
}
}
}
74 changes: 74 additions & 0 deletions GameofLife2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the board
// Space Complexity : O(1) for the in-place solution, as we are using the original board to store the next state of the cells
// 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
// The problem is to implement the Game of Life, which is a cellular automaton.
// The board is a 2D grid where each cell can be either alive (1) or dead (0).
// The next state of each cell is determined by the number of live neighbors it has.
// We define the 8 possible directions to check for neighbors.
// We iterate through each cell in the board and count the number of live neighbors for that cell.
// Based on the count of live neighbors and the current state of the cell, we apply the rules of the Game of Life to determine the next state of the cell.
// We use the original board to mark the transitions of states. We use the following encoding for the transitions:
// 0 → 1 is marked as 3 (dead to live)
// 1 → 0 is marked as 2 (live to dead)
// After processing all cells, we iterate through the board again to finalize the states. We set cells marked as 1 or 3 to 1 (alive) and cells marked
class Solution {
public void gameOfLife(int[][] board) {

int[][] dir = new int[][] {
{-1, -1}, {-1, 0}, {-1, 1},
{ 0, -1}, { 0, 1},
{ 1, -1}, { 1, 0}, { 1, 1}
};

int row = board.length;
int col = board[0].length;

// Step 1: mark transitions
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {

int count = 0;

// count live neighbors (1 or 2 means originally alive)
for (int[] d : dir) {
int p = i + d[0];
int q = j + d[1];

if (p >= 0 && q >= 0 && p < row && q < col &&
(board[p][q] == 1 || board[p][q] == 2)) {
count++;
}
}

// live → dead
if (board[i][j] == 1) {
if (count < 2 || count > 3) {
board[i][j] = 2;
}
}
// dead → live
else {
if (count == 3) {
board[i][j] = 3;
}
}
}
}

// Step 2: finalize states
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {

if (board[i][j] == 1 || board[i][j] == 3) {
board[i][j] = 1;
} else {
board[i][j] = 0;
}
}
}
}
}
41 changes: 41 additions & 0 deletions MissinNumber1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity : O(n log n) due to sorting the array
// Space Complexity : O(1) if we don't consider the output list, otherwise O(n) for the output list
// 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
// The problem is to find all the numbers in the range [1, n] that are missing from the input array.
// The approach is to first sort the input array and then iterate through the numbers from 1 to n.
// For each number, we check if it is present in the sorted array.
// If it is not present, we add it to the result list.
// We use a pointer j to keep track of our position in the sorted array.
// We compare each number i with the current number at index j in the sorted array.
// If the current number in the sorted array is greater than i, it means i is missing and we add it to the result list.
// If the current number in the sorted array is equal to i, we move the pointer j to the next position in the sorted array to check for the next number.
// We continue this process until we have checked all numbers from 1 to n.
import java.util.*;

class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {

List<Integer> result = new ArrayList<>();

Arrays.sort(nums);

int j = 0;

for (int i = 1; i <= nums.length; i++) {

if (j >= nums.length || nums[j] > i) {
result.add(i);
}

while (j < nums.length && nums[j] == i) {
j++;
}
}

return result;
}
}
44 changes: 44 additions & 0 deletions MissingNumber2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Time Complexity : O(n) due to iterating through the array twice
// Space Complexity : O(1) if we don't consider the output list, otherwise O(n) for the output list
// 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
// The problem is to find all the numbers in the range [1, n] that are missing from the input array.
// The approach is to use the input array itself to mark the presence of numbers.
// We iterate through the input array and for each number,
// we calculate its corresponding index (num)
// and mark the number at that index as negative to indicate that the number (num) is present in the array.
// After marking the presence of numbers, we iterate through the input array again and collect the indices that are still positive.
// These indices correspond to the missing numbers in the range [1, n] and we add them to the result list.
// Finally, we return the result list containing all the missing numbers
import java.util.*;

class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {

int n = nums.length;

// mark visited indices
for (int i = 0; i < n; i++) {
int num = nums[i];
int idx = Math.abs(num) - 1;

if (nums[idx] > 0) {
nums[idx] = -nums[idx];
}
}

List<Integer> result = new ArrayList<>();

// collect missing numbers
for (int i = 0; i < n; i++) {
if (nums[i] > 0) {
result.add(i + 1);
}
}

return result;
}
}