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
29 changes: 29 additions & 0 deletions Problem31.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
List<Integer> result = new ArrayList<>();

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

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

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

return result;
}
}
44 changes: 44 additions & 0 deletions Problem32.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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
public class Solution {

public int[] findMinAndMax(int[] nums) {
int n = nums.length;
int min, max;
int i;

// Handle first pair (or first element if odd length)
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 = max = nums[0];
i = 1;
}

// Process pairs
while (i < n - 1) {
if (nums[i] < nums[i + 1]) {
min = Math.min(min, nums[i]);
max = Math.max(max, nums[i + 1]);
} else {
min = Math.min(min, nums[i + 1]);
max = Math.max(max, nums[i]);
}
i += 2;
}

return new int[]{min, max};
}
}
53 changes: 53 additions & 0 deletions Problem33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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
class Solution {
int[][] dirs;
int m, n;

public void gameOfLife(int[][] board) {
this.dirs = new int[][]{{-1,-1}, {-1,0}, {-1, 1}, {0, -1}, {0, 1}, {1,-1}, {1,0}, {1,1}};
this.m = board.length;
this.n = board[0].length;

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int count = getCount(board, i, j);
if (board[i][j] == 0 && count == 3) {
board[i][j] = 3;
} else if (board[i][j] == 1 && (count < 2 || count > 3)) {
board[i][j] = 2;
}
}
}

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;
}
}
}
}

private int getCount(int[][] board, int i, int j) {
int count = 0;

for (int[] dir: dirs) {
int r = i + dir[0];
int c = j + dir[1];

if (r >= 0 && c >= 0 && r < m && c < n) {
if (board[r][c] == 1 || board[r][c] == 2) count++;
}
}

return count;
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.