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

// Traverse the matrix one element at a time while alternating between upward-right and downward-left directions.
// When the traversal hits a boundary, move to the next valid starting cell and flip the direction.
// Store each visited element in the result array until all rows * cols elements are processed.

class DiagonalTraverse {
public int[] findDiagonalOrder(int[][] mat) {
int rows = mat.length;
int cols = mat[0].length;
int[] result = new int[rows * cols];

boolean isUpward = true;
int row = 0, col = 0;

for(int i = 0; i < rows * cols; i++) {
result[i] = mat[row][col];

if(isUpward) {
if(col == cols - 1) {
row++;
isUpward = false;
} else if(row == 0) {
col++;
isUpward = false;
} else {
row--;
col++;
}
} else {
if(row == rows - 1) {
col++;
isUpward = true;
} else if(col == 0) {
row++;
isUpward = true;
} else {
row++;
col--;
}
}
}

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

// Build prefix products in the result array, where result[i] stores the product of all elements to the left of i.
// Traverse from right to left while maintaining a running suffix product.
// Multiply the prefix product and suffix product for each index to get the product of all elements except itself.

class ProductOfArray {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
result[0] = 1;

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

int rp = 1;
for(int i = nums.length - 2; i >= 0; i--) {
result[i] = result[i] * rp * nums[i + 1];
rp = rp * nums[i + 1];
}

return result;
}
}
43 changes: 43 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Time Complexity : O(m * n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :

// Maintain four boundaries (top, bottom, left, right) representing the current layer of the matrix.
// Traverse the top row, right column, bottom row, and left column in order, then shrink the boundaries.
// Continue processing inner layers until all elements have been visited in spiral order.

class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<Integer>();

int left = 0, top = 0, right = matrix[0].length-1, bottom = matrix.length-1;

while(left <= right && top <= bottom) {
for(int i=left; i<=right; i++) {
result.add(matrix[top][i]);
}
top++;

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

if(bottom>=top) {
for(int i=right; i>=left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}

if(left<=right) {
for(int i=bottom; i>=top; i--) {
result.add(matrix[i][left]);
}
left++;
}
}

return result;
}
}