diff --git a/Sample.java b/Sample.java index f10cd8d1..b91f4d1c 100644 --- a/Sample.java +++ b/Sample.java @@ -3,4 +3,51 @@ // Did this code successfully run on Leetcode : // Three line explanation of solution in plain english -// Your code here along with comments explaining your approach \ No newline at end of file +// Your code here along with comments explaining your approach +// Question 1. https://leetcode.com/problems/product-of-array-except-self/description/ +// Approach 1 +// For each number in the array we calculate the product of other numbers +// We will skip the current element during multiplication and result store in new array +// Time complexity : O(n^2) +// Space complexity : O(1) +// It gave TLE (Time Limit Exceeded) +class Solution { + public int[] productExceptSelf(int[] nums) { + int[] result = new int[nums.length]; + for(int i=0;i=0;--i){ + prod = prod * nums[i+1]; + result[i] = result[i] * prod; + } + return result; + } +} \ No newline at end of file diff --git a/Sample1.java b/Sample1.java new file mode 100644 index 00000000..e7cdecc8 --- /dev/null +++ b/Sample1.java @@ -0,0 +1,49 @@ +/** + * https://leetcode.com/problems/diagonal-traverse/description/ + * Approach : Start from top-left of matrix and traverse the matrix diagonally + * Then switch the direction when hitting the boundaries(top,left,right,bottom) + * Keep updating the ans array as we move in up or down directions. + * Time complexity : O(n*m) when n is row and m is column + * 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[] ans = new int[m*n]; + boolean dir = true; + int r = 0; + int c = 0; + for(int i=0;i<(m*n);++i){ + ans[i] = mat[r][c]; + if(dir){ + if(r==0 && c!=n-1){ + c++; + dir = false; + }else if(c==n-1){ + r++; + dir = false; + }else{ + c++; + r--; + } + + }else{ + if(c==0 && r!=m-1) + { + r++; + dir = true; + }else if(r==m-1){ + c++; + dir = true; + }else{ + r++; + c--; + } + + } + } + return ans; + } +} diff --git a/Sample2.java b/Sample2.java new file mode 100644 index 00000000..d202a75a --- /dev/null +++ b/Sample2.java @@ -0,0 +1,89 @@ +/** + * https://leetcode.com/problems/spiral-matrix/description/ + * Approach 1: We use 4 boundaries - top,bottom,left and right to keep track of spiral path + * At each step we traverse, right, down, left, and up then shrink the boundaries + * We stop when boundaries cross each other. + * + * Time complexity : O(n*m) + * Space complexity : O(1) + */ +class Solution { + public List spiralOrder(int[][] matrix) { + List list = new ArrayList<>(); + int r = matrix.length; + int c = matrix[0].length; + int top = 0; + int bottom = r-1; + int left = 0; + int right = c-1; + while(top<=bottom && left<=right){ + for(int j=left; j<=right;++j){ + list.add(matrix[top][j]); + } + top++; + for(int i=top;i<=bottom;++i){ + list.add(matrix[i][right]); + } + right--; + if(top<=bottom){ + for(int j=right;j>=left;--j){ + list.add(matrix[bottom][j]); + } + } + bottom--; + if(left<=right){ + for(int j=bottom;j>=top;--j){ + list.add(matrix[j][left]); + } + } + + left++; + } + return list; + } +} +/** + * https://leetcode.com/problems/spiral-matrix/description/ + * Approach 2: We use 4 boundaries - top,bottom,left and right to keep track of spiral path + * At each step we traverse, right, down, left, and up then shrink the boundaries + * We stop when boundaries cross each other. + * + * Time complexity : O(n*m) + * Space complexity : O(depth) depth of recursion which in worst case O(m+n) + */ +class Solution { + List ans; + public List spiralOrder(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + this.ans = new ArrayList<>(); + int top = 0, left = 0, bottom = m - 1, right = n - 1; + helper(matrix, top, left, bottom, right); + return ans; + } + + private void helper(int[][] matrix, int top, int left, int bottom, int right) { + if(top>bottom || left>right) return; + for (int i = left; i <= right; i++) { + ans.add(matrix[top][i]); + } + top++; + for (int i = top; i <= bottom; i++) { + ans.add(matrix[i][right]); + } + right--; + if (top <= bottom) { + for (int i = right; i >= left; i--) { + ans.add(matrix[bottom][i]); + } + bottom--; + } + if (left <= right) { + for (int i = bottom; i >= top; i--) { + ans.add(matrix[i][left]); + } + left++; + } + helper(matrix, top, left, bottom, right); + } +}