From 5fd8c85325ddb33a9c40dbc2db308355247bcadd Mon Sep 17 00:00:00 2001 From: Pranav Sorte Date: Wed, 26 Nov 2025 21:05:55 -0800 Subject: [PATCH] done --- Problem1.java | 34 ++++++++++++++++++++++++++++++ Problem2.java | 52 +++++++++++++++++++++++++++++++++++++++++++++ Problem3.java | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java create mode 100644 Problem3.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..0189d42d --- /dev/null +++ b/Problem1.java @@ -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=0;i--) { + res[i] = (runningProduct * nums[i+1]) * res[i]; + runningProduct *= nums[i+1]; + } + return res; + } +} diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..0849d861 --- /dev/null +++ b/Problem2.java @@ -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 spiralOrder(int[][] matrix) { + List 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; + } +}