From 5ce9881a8e033e98f45cc3c1181b96eb70fd0c9c Mon Sep 17 00:00:00 2001 From: tmujje Date: Tue, 4 May 2021 21:26:07 -0400 Subject: [PATCH 1/2] Array-1 Assignment Submission --- DiagonalTraverse.java | 53 ++++++++++++++++++++++++++++++++ ProductOfArrayExceptItself.java | 26 ++++++++++++++++ SpiralMatrixTraverse.java | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 DiagonalTraverse.java create mode 100644 ProductOfArrayExceptItself.java create mode 100644 SpiralMatrixTraverse.java diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..fb5d165d --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,53 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(m * n) since we are iterating through the entire matrix in a diagonal way +//Space Complexity: O(1) since we are not taking any extra space except result array + +public int[] findDiagonalOrder(int[][] mat) +{ + + if(mat == null || mat.length == 0) return new int[0]; + int m = mat.length; + int n = mat[0].length; + int[] res = new int[m*n]; + int row = 0; int col = 0; + int i = 0; + int dir = 1; + while(i < m*n){ + res[i] = mat[row][col]; + + //Moving Upward direction + if(dir == 1){ + if(col == n-1){ // right most element + row++; + dir = -1; + } + else if(row == 0){ + col++; + dir = -1; + } + else{ + row--; + col++; + } + } + // Moving Downward direction + else{ + if(row == m-1){ //Left last element + col++; + dir = 1; + } + else if(col == 0){ + row++; + dir = 1; + }else{ + row++; + col--; + } + } + i++; + } + + return res; + } \ No newline at end of file diff --git a/ProductOfArrayExceptItself.java b/ProductOfArrayExceptItself.java new file mode 100644 index 00000000..deeefe31 --- /dev/null +++ b/ProductOfArrayExceptItself.java @@ -0,0 +1,26 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are iterating through the array couple of times +//Space Complexity: O(1) since we are not taking any extra space and only using result array which will not be considered extra space + +public int[] productExceptSelf(int[] nums) { + int[] result = new int[nums.length]; + + int rProduct = 1; + result[0] = 1; + + //Product of elements left to the current element + for(int i = 1; i < nums.length; i++){ + rProduct = nums[i-1] * rProduct; + result[i] = rProduct; + } + rProduct = 1; + //LeftProduct * Product of elements right to the current element + for(int j = nums.length - 2; j >= 0; j--){ + rProduct = nums[j + 1] * rProduct; + result[j] = result[j] * rProduct; + } + + return result; +} \ No newline at end of file diff --git a/SpiralMatrixTraverse.java b/SpiralMatrixTraverse.java new file mode 100644 index 00000000..562647e0 --- /dev/null +++ b/SpiralMatrixTraverse.java @@ -0,0 +1,54 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(m * n) since we are iterating through the dp matrix +//Space Complexity: O(1) since we are not using extra space except resultList + +public List spiralOrder(int[][] matrix) { + ArrayList returnList = new ArrayList<>(); + if(matrix == null || matrix.length == 0) return returnList; + int left = 0; + int right = matrix[0].length - 1; + int top = 0; + int bottom = matrix.length - 1; + + while(left <= right && top <= bottom){ + + //Left to right iteration + for(int i = left; i <= right; i++){ + returnList.add(matrix[top][i]); + } + //Squeeze the top + top++; + + // Top to Bottom Iteration + for(int i = top; i <= bottom; i++){ + returnList.add(matrix[i][right]); + } + + //Squeeze right + right--; + + // Recheck the condition since top is modified after squeeze + if(top <= bottom){ + // Right to Left Iteration + for(int i = right; i >= left; i--){ + returnList.add(matrix[bottom][i]); + } + } + //Squeeze bottom + bottom--; + + //Recheck this condition since the right is modified after squeeze + if(left <= right){ + //Bottom to Top iteration + for(int i = bottom; i >= top; i--){ + returnList.add(matrix[i][left]); + } + } + //Squeeze Left + left++; + } + + return returnList; +} \ No newline at end of file From 28bc4b1fbc478899ea5ea5e0d494cecce32f446b Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Mon, 31 Aug 2026 12:38:14 -0400 Subject: [PATCH 2/2] Complete Array-1 assignment --- DiagonalTraverse.java | 53 ----------------------------- ProductOfArrayExceptItself.java | 26 --------------- ProductOfArrayExceptSelf.java | 44 ++++++++++++++++++++++++ SpiralMatrix | 59 +++++++++++++++++++++++++++++++++ SpiralMatrixTraverse.java | 54 ------------------------------ TraverseDiagonal | 59 +++++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+), 133 deletions(-) delete mode 100644 DiagonalTraverse.java delete mode 100644 ProductOfArrayExceptItself.java create mode 100644 ProductOfArrayExceptSelf.java create mode 100644 SpiralMatrix delete mode 100644 SpiralMatrixTraverse.java create mode 100644 TraverseDiagonal diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java deleted file mode 100644 index fb5d165d..00000000 --- a/DiagonalTraverse.java +++ /dev/null @@ -1,53 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(m * n) since we are iterating through the entire matrix in a diagonal way -//Space Complexity: O(1) since we are not taking any extra space except result array - -public int[] findDiagonalOrder(int[][] mat) -{ - - if(mat == null || mat.length == 0) return new int[0]; - int m = mat.length; - int n = mat[0].length; - int[] res = new int[m*n]; - int row = 0; int col = 0; - int i = 0; - int dir = 1; - while(i < m*n){ - res[i] = mat[row][col]; - - //Moving Upward direction - if(dir == 1){ - if(col == n-1){ // right most element - row++; - dir = -1; - } - else if(row == 0){ - col++; - dir = -1; - } - else{ - row--; - col++; - } - } - // Moving Downward direction - else{ - if(row == m-1){ //Left last element - col++; - dir = 1; - } - else if(col == 0){ - row++; - dir = 1; - }else{ - row++; - col--; - } - } - i++; - } - - return res; - } \ No newline at end of file diff --git a/ProductOfArrayExceptItself.java b/ProductOfArrayExceptItself.java deleted file mode 100644 index deeefe31..00000000 --- a/ProductOfArrayExceptItself.java +++ /dev/null @@ -1,26 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(n) since we are iterating through the array couple of times -//Space Complexity: O(1) since we are not taking any extra space and only using result array which will not be considered extra space - -public int[] productExceptSelf(int[] nums) { - int[] result = new int[nums.length]; - - int rProduct = 1; - result[0] = 1; - - //Product of elements left to the current element - for(int i = 1; i < nums.length; i++){ - rProduct = nums[i-1] * rProduct; - result[i] = rProduct; - } - rProduct = 1; - //LeftProduct * Product of elements right to the current element - for(int j = nums.length - 2; j >= 0; j--){ - rProduct = nums[j + 1] * rProduct; - result[j] = result[j] * rProduct; - } - - return result; -} \ No newline at end of file diff --git a/ProductOfArrayExceptSelf.java b/ProductOfArrayExceptSelf.java new file mode 100644 index 00000000..13f776df --- /dev/null +++ b/ProductOfArrayExceptSelf.java @@ -0,0 +1,44 @@ +//Time Complexity: O(n) +//Space Complexity: O(n) +//Approach: Find running product for both left pass and right pass and add product of these two into the final result to get the +//product of all of the number except self +class Solution { + public int[] productExceptSelf(int[] nums) + { + int n = nums.length; + + //Validate the inputs + if (nums == null || nums.length == 0) return new int[n]; + + int[] rProduct = new int[n]; + int[] lProduct = new int[n]; + lProduct[0] = 1; + rProduct[n-1] = 1; + int runProduct = 1; + + //Right pass to find running product from left to right + for(int i=1; i < n; i++) + { + runProduct = nums[i-1] * runProduct; + lProduct[i] = runProduct; + } + + //Reset running product befor right pass + runProduct = 1; + + //Left pass to find running product from right to left + for(int i=n-2; i >= 0; i--) + { + runProduct = nums[i+1] * runProduct; + rProduct[i] = runProduct; + } + + //Add right product * left product in original array + for(int i = 0; i < n; i++) + { + nums[i] = rProduct[i] * lProduct[i]; + } + + return nums; + } +} \ No newline at end of file diff --git a/SpiralMatrix b/SpiralMatrix new file mode 100644 index 00000000..1bad6daa --- /dev/null +++ b/SpiralMatrix @@ -0,0 +1,59 @@ +//Time Complexity : O(m * n) +//Space Complexity: O(1) +class Solution { + public List spiralOrder(int[][] matrix) { + + List result = new ArrayList<>(); + + //Validate inputs + if (matrix == null || matrix.length == 0) return result; + + int top = 0, left = 0; + int right = matrix[0].length - 1; + int bottom = matrix.length - 1; + + while(left <= right && top <= bottom) + { + //Left to right + for(int i = left; i <= right; i++) + { + result.add(matrix[top][i]); + } + + //Move the top + top++; + + //Top to bottom + for(int i = top; i <= bottom; i++) + { + result.add(matrix[i][right]); + } + + //Move the right + right--; + + // Recheck the condition since top is modified after initial + if(top <= bottom){ + // Right to Left + for(int i = right; i >= left; i--){ + result.add(matrix[bottom][i]); + } + } + + //Move bottom + bottom--; + + //Recheck this condition since the right is modified after above + if(left <= right){ + //Bottom to Top iteration + for(int i = bottom; i >= top; i--){ + result.add(matrix[i][left]); + } + } + + //Move Left + left++; + } + return result; + } +} \ No newline at end of file diff --git a/SpiralMatrixTraverse.java b/SpiralMatrixTraverse.java deleted file mode 100644 index 562647e0..00000000 --- a/SpiralMatrixTraverse.java +++ /dev/null @@ -1,54 +0,0 @@ -// Did this code successfully run on Leetcode : Yes -// Any problem you faced while coding this : No - -//Time Complexity : O(m * n) since we are iterating through the dp matrix -//Space Complexity: O(1) since we are not using extra space except resultList - -public List spiralOrder(int[][] matrix) { - ArrayList returnList = new ArrayList<>(); - if(matrix == null || matrix.length == 0) return returnList; - int left = 0; - int right = matrix[0].length - 1; - int top = 0; - int bottom = matrix.length - 1; - - while(left <= right && top <= bottom){ - - //Left to right iteration - for(int i = left; i <= right; i++){ - returnList.add(matrix[top][i]); - } - //Squeeze the top - top++; - - // Top to Bottom Iteration - for(int i = top; i <= bottom; i++){ - returnList.add(matrix[i][right]); - } - - //Squeeze right - right--; - - // Recheck the condition since top is modified after squeeze - if(top <= bottom){ - // Right to Left Iteration - for(int i = right; i >= left; i--){ - returnList.add(matrix[bottom][i]); - } - } - //Squeeze bottom - bottom--; - - //Recheck this condition since the right is modified after squeeze - if(left <= right){ - //Bottom to Top iteration - for(int i = bottom; i >= top; i--){ - returnList.add(matrix[i][left]); - } - } - //Squeeze Left - left++; - } - - return returnList; -} \ No newline at end of file diff --git a/TraverseDiagonal b/TraverseDiagonal new file mode 100644 index 00000000..2b73b261 --- /dev/null +++ b/TraverseDiagonal @@ -0,0 +1,59 @@ +//Time Complexity: O(mxn) +//Space Complexity: O(1) +class Solution { + int[] result; + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + this.result = new int[m * n]; + + //Validate inputs + if(mat == null || mat.length == 0) return result; + + boolean upward = true; + int i = 0, row = 0, col = 0; + + while(i < m*n) + { + result[i] = mat[row][col]; + + // Going upward direction + if(upward) + { + if(col == n-1) // Right most element is reached - This needs to be checked as there is a chance for 0, n-1 + { + row++; + upward = false; + } + else if(row == 0) // Initially when we move from first element to right + { + col++; + upward = false; + } + else{ // Regular upward movement + row--; + col++; + } + } + else + { + if(row == m-1) + { + col++; + upward = true; + } + else if(col == 0) + { + row++; + upward = true; + } + else{ + row++; + col--; + } + } + i++; + } + return result; + } +} \ No newline at end of file