diff --git a/DiagonalTraverseMatrix.java b/DiagonalTraverseMatrix.java new file mode 100644 index 00000000..74d7e288 --- /dev/null +++ b/DiagonalTraverseMatrix.java @@ -0,0 +1,63 @@ +// Time Complexity : O(M*N) +// Space Complexity : O(M*N) +// 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 Solution { + public int[] findDiagonalOrder(int[][] mat) { + // for up r-- and c++ + // for down r++ and c-- + // use a flag to determine if to go up or down - true is up and false is down + // edge cases - a. to move to next element in row for going down and going up + // for first anf last row + // b. go to next element in col for first and last col + + boolean flag = true; + int[] result = new int[mat.length * mat[0].length]; + int r = 0, c = 0; + for(int i = 0; i < mat.length * mat[0].length; i++) + { + result[i] = mat[r][c]; + if(flag) + { + if(r == 0 && c != mat[0].length - 1) + { + c++; + flag = false; + } + else if(c == mat[0].length - 1) + { + r++; + flag = false; + } + else + { + r--; + c++; + } + } + else + { + if(c == 0 && r != mat.length - 1) + { + r++; + flag = true; + } + else if(r == mat.length - 1) + { + c++; + flag = true; + } + else + { + r++; + c--; + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/ProductOfArrayExceptSelf.java b/ProductOfArrayExceptSelf.java new file mode 100644 index 00000000..2505fbdd --- /dev/null +++ b/ProductOfArrayExceptSelf.java @@ -0,0 +1,29 @@ +// Time Complexity : O(N) +// Space Complexity : O(N) +// 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 Solution { + public int[] productExceptSelf(int[] nums) { + int[] result = new int[nums.length]; + int rp = 1; + //left pass + result[0] = 1; + for(int i = 1; i < nums.length; i++) + { + rp *= nums[i-1]; + result[i] = rp; + } + // right pass + // reset running product + rp = 1; + for(int i = nums.length - 2; i>= 0; i--) + { + rp *= nums[i+1]; + result[i] *= rp; + } + return result; + } +} \ No newline at end of file diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..603c8923 --- /dev/null +++ b/SpiralMatrix.java @@ -0,0 +1,54 @@ +import java.util.List; +import java.util.ArrayList; +// Time Complexity : O(M*N) +// Space Complexity : O(M*N) +// 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 Solution { + public List spiralOrder(int[][] matrix) { + // use left,right, top and buttom pointer to maintain the spiral + // top prints rows from left to right + // right prints cols from top to bottom + // bottom prints rows from right to left + // left prints cols from bottom to top + List result = new ArrayList<>(); + int left = 0, right = matrix[0].length - 1; + int top = 0, bottom = matrix.length - 1; + while(left <= right && top <= bottom) + { + // top row + for(int i = left; i <= right; i++) + { + result.add(matrix[top][i]); + } + top++; + // right col + for(int i = top; i <= bottom; i++) + { + result.add(matrix[i][right]); + } + right--; + // bottom row + if(top <= bottom) + { + for(int i = right; i >= left; i--) + { + result.add(matrix[bottom][i]); + } + bottom--; + } + // left col + if(left <= right && top <= bottom) + { + for(int i = bottom; i >= top; i--) + { + result.add(matrix[i][left]); + } + left++; + } + } + return result; + } +} \ No newline at end of file