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
30 changes: 30 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(n)
// Space Complexity : O(1) (output list not counted)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No, just need to use abs() while marking.


// Your code here along with comments explaining your approach in three sentences only
// Since values are from 1 to n, we use each value as an index and mark that index as visited by making it negative.
// After marking, any position that still has a positive number means that (index+1) never appeared in the array.
// Finally, we collect all such missing numbers into the answer list.

import java.util.*;

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

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

for (int i = 0; i < nums.length; i++) {
int idx = Math.abs(nums[i]) - 1;
if (nums[idx] > 0) nums[idx] = -nums[idx];
}

for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) res.add(i + 1);
}

return res;
}
}
54 changes: 54 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Only thing is handling odd length array properly.


// Your code here along with comments explaining your approach in three sentences only
// Instead of comparing every element with both min and max, we compare elements in pairs.
// For each pair, we first decide which one is smaller and which one is bigger, then update min and max accordingly.
// This reduces comparisons and gives better than 2*(n-2) comparisons.

class Solution {
public int[] findMinMax(int[] nums) {

int n = nums.length;
int min, max;
int i = 0;

// init min and max based on first one or first two
if (n % 2 == 0) {
if (nums[0] < nums[1]) {
min = nums[0];
max = nums[1];
} else {
min = nums[1];
max = nums[0];
}
i = 2;
} else {
min = nums[0];
max = nums[0];
i = 1;
}

// compare in pairs
while (i < n - 1) {

int a = nums[i];
int b = nums[i + 1];

if (a < b) {
if (a < min) min = a;
if (b > max) max = b;
} else {
if (b < min) min = b;
if (a > max) max = a;
}

i += 2;
}

return new int[]{min, max};
}
}
56 changes: 56 additions & 0 deletions problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Time Complexity : O(m*n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Need to carefully count live neighbors using old state only.


// Your code here along with comments explaining your approach in three sentences only
// We update the board in-place by using extra marker states so we don’t lose original info.
// 2 means dead -> live (was 0, will be 1) and 3 means live -> dead (was 1, will be 0).
// After first pass, we convert markers to final 0/1 values in second pass.

class Solution {
public void gameOfLife(int[][] board) {

int m = board.length;
int n = board[0].length;

int[] dr = {-1,-1,-1,0,0,1,1,1};
int[] dc = {-1,0,1,-1,1,-1,0,1};

for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {

int live = 0;

// count live neighbors based on old values
for (int k = 0; k < 8; k++) {
int nr = r + dr[k];
int nc = c + dc[k];

if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
// old live is 1 or 3 (3 means it was live before update)
if (board[nr][nc] == 1 || board[nr][nc] == 3) {
live++;
}
}
}

// apply rules with markers
if (board[r][c] == 1) {
if (live < 2 || live > 3) board[r][c] = 3; // live -> dead
} else {
if (live == 3) board[r][c] = 2; // dead -> live
}
}
}

// convert markers to final state
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (board[r][c] == 2) board[r][c] = 1;
else if (board[r][c] == 3) board[r][c] = 0;
}
}
}
}