From 2b33efdfe53aae3a685bc94585ff201697a7d9c9 Mon Sep 17 00:00:00 2001 From: sandeepkumarks Date: Fri, 19 Jun 2026 12:03:14 -0700 Subject: [PATCH] Added solutions --- DiagonalTraverse.java | 48 +++++++++++++++++++++++++++++++++++++++++++ ProductOfArray.java | 26 +++++++++++++++++++++++ SpiralMatrix.java | 43 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 DiagonalTraverse.java create mode 100644 ProductOfArray.java create mode 100644 SpiralMatrix.java diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..b5f12d1e --- /dev/null +++ b/DiagonalTraverse.java @@ -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; + } +} \ No newline at end of file diff --git a/ProductOfArray.java b/ProductOfArray.java new file mode 100644 index 00000000..d807ecab --- /dev/null +++ b/ProductOfArray.java @@ -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; + } +} \ No newline at end of file diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..d22dc74b --- /dev/null +++ b/SpiralMatrix.java @@ -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 spiralOrder(int[][] matrix) { + List result = new ArrayList(); + + 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; + } +} \ No newline at end of file