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
27 changes: 27 additions & 0 deletions DisappearedNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.ArrayList;
import java.util.List;

// Approach: Temporary state change pattern with two pass solution
// Time complexity: N + N ~= O(N)
// Space complexity: O(1) no extra space except for output.
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result = new ArrayList<>();
int length = nums.length;
for (int i = 0; i < length; i ++) {
int index = Math.abs(nums[i]) - 1; // Take absolute value to avoid double marking of the index
if (nums[index] > 0) {
nums[index] *= -1; // Mark the value at the index as negative
}
}
for (int i = 0; i < length; i ++) {
if (nums[i] > 0) {
// if the number is positive then i + 1 is missed/disappeared
result.add(i + 1);
} else {
nums[i] *= -1; // Changing the number to its original value
}
}
return result;
}
}
54 changes: 54 additions & 0 deletions GameOfLife.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Approach: Temporary state change pattern to optimize space on previous solution
// Time: O(rows * columns)
// Space: O(1)
class Solution {
// Initialize directions
private static final int[][] directions = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};

public void gameOfLife(int[][] board) {
// Temporary states
// Change from 1 -> 0: Mark 2 (dies)
// Change from 0 -> 1: Mark 3 (lives)
int rows = board.length;
int columns = board[0].length;
for (int row = 0; row < rows; row ++) {
for (int column = 0; column < columns; column ++) {
int liveNeighbors = countLiveNeighbors(board, row, column);
if (board[row][column] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) {
board[row][column] = 2; // dies
}
if (board[row][column] == 0 && liveNeighbors == 3) {
board[row][column] = 3; // lives
}
}
}
// Putting back to actual values of live (1) or dead (0)
for (int row = 0; row < rows; row ++) {
for (int column = 0; column < columns; column ++) {
if (board[row][column] == 2) { // marked as dead
board[row][column] = 0;
} else if (board[row][column] == 3) { // marked as live
board[row][column] = 1;
}
}
}
}

private int countLiveNeighbors(int[][] board, int row, int column) {
int countOfLiveNeighbors = 0;
// Iterate through all neighboring indices
for (int[] direction: directions) {
int neighborRow = row + direction[0];
int neighborColumn = column + direction[1];
if (neighborRow < 0 || neighborRow >= board.length || neighborColumn < 0 || neighborColumn >= board[0].length) {
// continue with next direction if current one is out of bounds
continue;
}
if (board[neighborRow][neighborColumn] == 1 || board[neighborRow][neighborColumn] == 2) {
// 1 represents live neighbors and 2 was alive originally
countOfLiveNeighbors ++;
}
}
return countOfLiveNeighbors;
}
}
53 changes: 53 additions & 0 deletions MinMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.Arrays;

// Approach: Minimizing number of comparisions to approximately 1.5 comparisions per index.
// Time complexity: N/2 ~= O(N)
// Space complexity: O(1)

public class MinMax {
public int[] findMinMax(int[] nums) {
if (nums.length == 0) {
throw new IllegalArgumentException("No solution: Array is empty");
}
int min = nums[0];
int max = nums[0];
int index = 1;
for (; index < nums.length; index += 2) { // increment 2 indices
if (nums[index - 1] < nums[index]) {
min = Math.min(min, nums[index - 1]); // compare min with smaller number
max = Math.max(max, nums[index]); // compare max with larger number
} else {
max = Math.max(max, nums[index - 1]); // compare max with larger number
min = Math.min(min, nums[index]); // compare min with smaller number
}
}
if (index == nums.length) {
// Handle last index
min = Math.min(min, nums[nums.length - 1]);
max = Math.max(max, nums[nums.length - 1]);
}
return new int[]{min, max};
}

public static void main(String[] args) {
MinMax ob = new MinMax();
int[] nums1 = {3, 1, 0, 9, 4, 6};
System.out.println(Arrays.toString(ob.findMinMax(nums1))); // returns [0, 9]
int[] nums2 = {3};
System.out.println(Arrays.toString(ob.findMinMax(nums2))); // returns [3, 3]
int[] nums3 = {3, 1};
System.out.println(Arrays.toString(ob.findMinMax(nums3))); // returns [1, 3]
int[] nums4 = {3, 1, 0, 9};
System.out.println(Arrays.toString(ob.findMinMax(nums4))); // returns [0, 9]
int[] nums5 = {-1};
System.out.println(Arrays.toString(ob.findMinMax(nums5))); // returns [-1, -1]
int[] nums6 = {3, -1};
System.out.println(Arrays.toString(ob.findMinMax(nums6))); // returns [-1, 3]
int[] nums7 = {-1, -3, -2};
System.out.println(Arrays.toString(ob.findMinMax(nums7))); // returns [-3, -1]
int[] nums8 = {-1, 7, -3, -2, 3};
System.out.println(Arrays.toString(ob.findMinMax(nums8))); // returns [-3. 7]
int[] nums9 = {};
System.out.println(Arrays.toString(ob.findMinMax(nums9))); // throws exception with message "No solution: Array is empty"
}
}