diff --git a/DiagonalMatrix.java b/DiagonalMatrix.java new file mode 100644 index 00000000..a8c1d288 --- /dev/null +++ b/DiagonalMatrix.java @@ -0,0 +1,61 @@ +// Time Complexity :O(M*N) +// Space Complexity : O(1) without considering result space. +// 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 + +class DiagonalMatrix { + public int[] findDiagonalOrder(int[][] matrix) { + if (matrix == null) { + throw new IllegalArgumentException("Input matrix is null"); + } + if (matrix.length == 0 || matrix[0].length == 0) { + return new int[0]; + } + + int rows = matrix.length; + int cols = matrix[0].length; + int[] result = new int[rows * cols]; + int r = 0; + int c = 0; + boolean up = true; + + for (int i = 0; i < result.length; i++) { + result[i] = matrix[r][c]; + if(up) { // Move Up + if (c == cols - 1) { + // Reached last column. Now move to below cell in the same column. + // This condition needs to be checked first due to top right corner cell. + r++; + up=false; + } else if (r == 0) { + // Reached first row. Now move to next cell in the same row. + c++; + up=false; + } else { + // Somewhere in middle. Keep going up diagonally. + r--; + c++; + } + } else { // Move Down + if (r == rows - 1) { + // Reached last row. Now move to next cell in same row. + // This condition needs to be checked first due to bottom left corner cell. + c++; + up=true; + } else if (c == 0) { + // Reached first columns. Now move to below cell in the same column. + r++; + up=true; + } else { + // Somewhere in middle. Keep going down diagonally. + r++; + c--; + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/Product.java b/Product.java new file mode 100644 index 00000000..276090cf --- /dev/null +++ b/Product.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 +/*Calculate the product on the left of all the numbers in the array. +Going from right to left calculate the product on the right of the number and multiply with the product obtained before at each position +*/ + +// Your code here along with comments explaining your approach + +/* +## Problem 1 - Leetcode 238.Product of Array Except Self + + */ + +class Product { + + public int[] productExceptSelf(int[] nums) { + int[] result = new int[nums.length]; + + int[] leftProduct = new int[nums.length]; + result[0] = 1; + + for (int i = 1; i < nums.length; i++) { + result[i] = result[i - 1] * nums[i - 1]; + } + + int rightProduct = 1; + for (int i = nums.length - 2; i >= 0; i--) { + rightProduct *= nums[i + 1]; + result[i] = result[i] * rightProduct; + } + + return result; + + } +} \ No newline at end of file diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..5f12f3a2 --- /dev/null +++ b/SpiralMatrix.java @@ -0,0 +1,55 @@ +// Time Complexity : O(m*n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Three line explanation of solution in plain english +//Take four pointers ->left, top, right, and bottom. +//And navigate left to right, top to bottom, right to left, bottom to top in that order. +//Increase or decrease the pointers accordingly. + +// Your code here along with comments explaining your approach +class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List result = 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 j = left; j <= right; j++) { + result.add(matrix[top][j]); + } + top++; + + + for (int i = top; i <= bottom; i++) { + result.add(matrix[i][right]); + } + right--; + + + if (top <= bottom) { + for (int j = right; j >= left; j--) { + result.add(matrix[bottom][j]); + } + bottom--; + } + + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result.add(matrix[i][left]); + } + left++; + } + + } + + return result; + + } +}