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
46 changes: 46 additions & 0 deletions DiagnolTraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Time: O(rows * columns)
// Space: O(1)
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int rows = mat.length;
int columns = mat[0].length;
boolean dir = true; // true: upwards, false: downwards.
int[] result = new int[rows * columns];
int index = 0; // for result
// Start at 0,0
int row = 0;
int column = 0;
for (int i = 0; i < rows * columns; i ++) { // traverse through entire matrix
// Add value at current row and column to the result and increment index.
result[index ++] = mat[row][column];
if (dir) {
// moving up
if (column == columns - 1) {
dir = false; // move down
row += 1;
} else if (row == 0) {
dir = false; // move down
column += 1;
} else {
// move to next diagonal index in upward direction
row --;
column ++;
}
} else {
// moving down
if (row == rows - 1) {
dir = true; // move up
column += 1;
} else if (column == 0) {
dir = true; // move up
row += 1;
} else {
// move to next diagonal index in downward direction
row ++;
column --;
}
}
}
return result;
}
}
22 changes: 22 additions & 0 deletions ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Approach: Running product (Two-pass solution)
// Time: N + N ~= O(N)
// Space: O(1)
class Solution {
public int[] productExceptSelf(int[] nums) {
int runningProduct = 1;
int[] result = new int[nums.length];
result[0] = runningProduct;
// left + 1 to right
for (int i = 1; i < nums.length; i ++) {
runningProduct *= nums[i - 1];
result[i] = runningProduct; // assign running product
}
runningProduct = 1;
// right - 1 to left
for (int i = nums.length - 2; i >= 0; i --) {
runningProduct *= nums[i + 1];
result[i] *= runningProduct; // multiply with running product
}
return result;
}
}
48 changes: 48 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;

// Time: O(rows * columns)
// Space: O(1)
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int rows = matrix.length;
int columns = matrix[0].length;
List<Integer> result = new ArrayList<>();
// Initialize directions
int top = 0;
int bottom = rows - 1;
int left = 0;
int right = columns - 1;
while (top <= bottom && left <= right) {
if (top <= bottom && left <= right) {
// move right
for (int i = left; i <= right; i ++) {
result.add(matrix[top][i]);
}
top ++;
}
if (top <= bottom && left <= right) {
// move down
for (int i = top; i <= bottom; i ++) {
result.add(matrix[i][right]);
}
right --;
}
if (top <= bottom && left <= right) {
// move left
for (int i = right; i >= left; i --) {
result.add(matrix[bottom][i]);
}
bottom --;
}
if (top <= bottom && left <= right) {
// move up
for (int i = bottom; i >= top; i --) {
result.add(matrix[i][left]);
}
left ++;
}
}
return result;
}
}