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
34 changes: 34 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time Complexity : O(n) + O(n) ~ O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
/*
Build a prefix product array where res[i] stores the product of all numbers to the left of i.
Then traverse from right side and multiply each res[i] with the running product of elements to the right of i.
This way each index gets: (product of all left elements) × (product of all right elements) — without using division.
*/

public class Problem1 {
public int[] productExceptSelf(int[] nums) {
if(nums.length == 0) {
return new int[]{};
}
int n = nums.length;
int[] res = new int[n];
int runningProduct = 1;
res[0] = 1;
for(int i = 1;i<n;i++) {
res[i] = runningProduct * nums[i-1];
runningProduct = res[i];
}

runningProduct = 1;
for(int i = n-2;i>=0;i--) {
res[i] = (runningProduct * nums[i+1]) * res[i];
runningProduct *= nums[i+1];
}
return res;
}
}
52 changes: 52 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity : O(M*N)
// Space Complexity : O(M*N)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
/*
Traverse the matrix diagonally, switching between upward and downward directions.
When moving up, bounce off the top row or last column; when moving down, bounce off the bottom row or first column.
Collect each cell in order while flipping the direction whenever a boundary is hit.
*/

public class Problem2 {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;

int[] res = new int[m*n];
int row = 0;
int col = 0;
boolean goingUp = true;

for(int i = 0;i<m*n;i++) {
res[i] = mat[row][col];
if(goingUp) {
if(col == n-1) {
goingUp = false;
row++;
} else if(row == 0) {
col++;
goingUp = false;
} else {
row--;
col++;
}
} else {
if(row == m-1) {
col++;
goingUp = true;
} else if(col == 0) {
row++;
goingUp = true;
} else {
col--;
row++;
}
}
}

return res;
}
}
58 changes: 58 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Time Complexity : O(m * n)
// Space Complexity : O(M*N)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
/*
You maintain four boundaries (top, bottom, left, right) and traverse the matrix layer by layer.
In each loop, you move right → down → left → up, shrinking the boundaries after each direction.
You repeat this until all boundaries cross, ensuring every element is added exactly once in spiral order.
*/


import java.util.ArrayList;
import java.util.List;

public class Problem3 {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;

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

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

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

right--;

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

bottom--;
}

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

left++;
}
}
return res;
}
}