diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..4f30df50 --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,57 @@ +// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the matrix +// Space Complexity : O(1) +// Did this code successfully run on Leetcode :Yes +// Three line explanation of solution in plain english +// 1. We traverse the matrix in a diagonal manner, starting from the top-left corner. +// 2. We use a boolean variable to keep track of the direction of traversal (up-right or down-left). +// 3. We update the row and column indices based on the current direction +// and the boundaries of the matrix. We store the elements in the result array as we traverse. +// The auxiliary space is O(1) as we are using only a few variables to keep track of the current position and direction, +// and the result array is not considered auxiliary space as it is required for the output. + +// Your code here along with comments explaining your approac + +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + + boolean direction = true; // true = up-right, false = down-left + + int row = mat.length; + int col = mat[0].length; + + int[] result = new int[row * col]; + + int r = 0, c = 0; + + for (int i = 0; i < row * col; i++) { + + result[i] = mat[r][c]; + + if (direction) { + if (c == col - 1) { + r++; + direction = false; + } else if (r == 0) { + c++; + direction = false; + } else { + r--; + c++; + } + } else { + if (r == row - 1) { + c++; + direction = true; + } else if (c == 0) { + r++; + direction = true; + } else { + r++; + c--; + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/ProductofArray.java b/ProductofArray.java new file mode 100644 index 00000000..0d1cccb1 --- /dev/null +++ b/ProductofArray.java @@ -0,0 +1,37 @@ + +// Time Complexity : O(N) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode :Yes +// Three line explanation of solution in plain english +// 1. Calculate the product of all elements to the left of each element +// 2. Calculate the product of all elements to the right of each element +// 3. Multiply the left and right products for each element +// Product of all no's of element to left * element +// Product of all no's of element to right * element +// Auxuliary space is not used as we are storing the left product in the result array and using a variable to store the right product. + +// Your code here along with comments explaining your approach +class Solution { + public int[] productExceptSelf(int[] nums) { + + int[] result = new int[nums.length]; + + int rightProduct = 1; + result[0] = 1; + + // Left products + for (int i = 1; i < nums.length; i++) { + rightProduct = rightProduct * nums[i - 1]; + result[i] = rightProduct; + } + + // Right products + rightProduct = 1; + for (int i = nums.length - 2; i >= 0; i--) { + rightProduct = rightProduct * nums[i + 1]; + result[i] = result[i] * rightProduct; + } + + return result; + } +} \ No newline at end of file diff --git a/SprialMatrix.java b/SprialMatrix.java new file mode 100644 index 00000000..75873aab --- /dev/null +++ b/SprialMatrix.java @@ -0,0 +1,57 @@ +// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the matrix +// Did this code successfully run on Leetcode :Yes +// Three line explanation of solution in plain english +// 1. We maintain four pointers to keep track of the current boundaries of the matrix (rowBegin, rowEnd, colBegin, colEnd). +// 2. We traverse the matrix in a spiral manner, moving right, down, left, and up, +// while updating the boundaries after each traversal. +// 3. We continue this process until the pointers cross each other, indicating that we have traversed the entire matrix. +// We store the elements in the result list as we traverse. +// The auxiliary space is O(1) as we are using only a few variables to keep track of the current position and boundaries, and the result list is not considered auxiliary space as it is required for the output. + +// Your code here along with comments explaining your approac +import java.util.*; + +class Solution { + public List spiralOrder(int[][] matrix) { + + List res = new ArrayList<>(); + + int rowBegin = 0; + int colBegin = 0; + int rowEnd = matrix.length - 1; + int colEnd = matrix[0].length - 1; + + while (rowBegin <= rowEnd && colBegin <= colEnd) { + + // move right + for (int j = colBegin; j <= colEnd; j++) { + res.add(matrix[rowBegin][j]); + } + rowBegin++; + + // move down + for (int j = rowBegin; j <= rowEnd; j++) { + res.add(matrix[j][colEnd]); + } + colEnd--; + + // move left + if (rowBegin <= rowEnd) { + for (int j = colEnd; j >= colBegin; j--) { + res.add(matrix[rowEnd][j]); + } + rowEnd--; + } + + // move up + if (colBegin <= colEnd) { + for (int j = rowEnd; j >= rowBegin; j--) { + res.add(matrix[j][colBegin]); + } + colBegin++; + } + } + + return res; + } +} \ No newline at end of file