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
49 changes: 49 additions & 0 deletions DiagonalTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Time Complexity : O(mn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes

// Your code here along with comments explaining your approach
// 1: We maintain a boolean flag that holds the value of whether we are traversing upwards or downwards
// 2: For upwards conditions, we increment/decrement the row and column accordingly, while keeping in mind the condition of reaching the last column
// 3: For downwards conditions, we increment/decrement the row and column accordingly, while keeping in mind the condition of reaching the last row
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length; // number of columns
int n = mat[0].length; // number of rows;

int[] result = new int[m * n];
int row = 0, col = 0; // pointers to move through matrix array

boolean flag = true; // starting in upwards direction

for (int idx = 0; idx < m * n; idx++) {
result[idx] = mat[row][col];
if (flag) { // upwards
if (row == 0 && col != n - 1) {
col++;
flag = false;
} else if (col == n - 1) {
flag = false;
row++;
} else {
row--;
col++;
}
} else { //downwards
if (col == 0 && row != m - 1) {
row++;
flag = true;
} else if (row == m - 1) {
flag = true;
col++;
} else {
row++;
col--;
}

}
}
return result;

}
}
34 changes: 34 additions & 0 deletions ProductExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity : O(n) average
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes

// Your code here along with comments explaining your approach
// 1: We go through two passes for each element in the array, calculating the left and right product for each element
// 2: For each pass, we maintain a running product that contains either the left or right product that we have come across thus far
// 3: After calculating the left array, we use the same array to find the runningProduct and thus the rightProduct and can return the same array
class Solution {
public int[] productExceptSelf(int[] nums) {
int rp = 1;
int n = nums.length;
int[] result = new int[n];
// left pass,
// we add 1 to the beginning of the array
result[0] = 1;

for (int i = 1; i < n; i++) {
rp = rp * nums[i - 1];
result[i] = rp;
}

// right pass
rp = 1;
for (int i = n - 2; i >= 0; i--) {
rp = rp * nums[i + 1];
result[i] = result[i] * rp;

}

return result;

}
}
51 changes: 51 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity : O(mn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes

// Your code here along with comments explaining your approach
// 1: We have 4 variables to maintain our position in the matrix array
// 2: For each type of traversal, we have a for loop that uses the necessary conditions to determine which elements to add in the output ArrayList
// 3: Since we are using a while loop, we have additional checks with if statements to ensure that our pointers are still valid in the matrix
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> li = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;

int top = 0;
int bottom = m - 1;
int left = 0;
int right = n - 1;

while (top <= bottom && left <= right) {
// top row
for (int j = left; j <= right; j++) {
li.add(matrix[top][j]);
}
top++;

// right column
for (int i = top; i <= bottom; i++) {
li.add(matrix[i][right]);
}
right--;

// bottom row
if (top <= bottom) {
for (int j = right; j >= left; j--) {
li.add(matrix[bottom][j]);
}
}
bottom--;

// left column
if (left <= right) {
for (int j = bottom; j >= top; j--) {
li.add(matrix[j][left]);
}
}
left++;
}
return li;
}
}