From cd44d7ff251267fd589726990af393dcdb35bbaf Mon Sep 17 00:00:00 2001 From: Manassa2000 Date: Mon, 27 Oct 2025 21:55:04 -0400 Subject: [PATCH] Array-1 --- DiagonalTraverse.java | 46 +++++++++++++++++++++++++++++++++++ ProductOfArrayExceptSelf.java | 21 ++++++++++++++++ SpiralMatrix.java | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 DiagonalTraverse.java create mode 100644 ProductOfArrayExceptSelf.java create mode 100644 SpiralMatrix.java diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..69099e33 --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,46 @@ +// Time Complexity :O(m*n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[] result = new int[m*n]; + int row = 0; + int col = 0; + boolean direction = true; + + for(int i = 0; i < m * n; i++) { + result[i] = mat[row][col]; + if(direction) {//move up + if(col == n - 1) {//reached last column + row++; + direction = false; + } + else if(row == 0) {//reached top row + col++; + direction = false; + } + else { + row--; + col++; + } + } else {//move down + if(row == m - 1) {//reached bottom row + col++; + direction = true; + } + else if(col == 0) {//reached first column + row++; + direction = true; + } + else { + row++; + col--; + } + } + } + return result; + } +} + diff --git a/ProductOfArrayExceptSelf.java b/ProductOfArrayExceptSelf.java new file mode 100644 index 00000000..b1f59e57 --- /dev/null +++ b/ProductOfArrayExceptSelf.java @@ -0,0 +1,21 @@ +// Time Complexity :O(n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes + +class Solution { + public int[] productExceptSelf(int[] nums) { + int[] result = new int[nums.length]; + int product = 1; + result[0] = 1;//no left side product for first element + for(int i=1; i=0; i--){//product of right side + product = product*nums[i+1]; + result[i] = result[i] * product; + } + return result; + } +} diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..c61d50be --- /dev/null +++ b/SpiralMatrix.java @@ -0,0 +1,44 @@ +// Time Complexity :O(m*n) +// Space Complexity :O(1) +// Did this code successfully run on Leetcode :yes +import java.util.ArrayList; +import java.util.List; + +class Solution { + public List spiralOrder(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int top = 0; + int bottom = m-1; + int left = 0; + int right = n-1; + List result = new ArrayList<>(); + + while(top <= bottom && left <= right){ + for(int i=left; i<=right; i++){//top row + result.add(matrix[top][i]); + } + top++;//update top + for(int i=top; i<=bottom; i++){//right column + result.add(matrix[i][right]); + } + right--;//update right + if(top <= bottom) + { + for(int i=right; i>=left; i--){//bottom row + result.add(matrix[bottom][i]); + } + bottom--;//update bottom + } + if(left <= right) + { + for(int i=bottom; i>=top; i--){//left column + result.add(matrix[i][left]); + } + left++;//update left + } + } + return result; + } +} +