diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..00404d0b --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,91 @@ +// 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 +// we start with r=c=0, with upward direction +//for up we decrese row, increse col, when out of boundary adjust row and column and make down +//for down increase row, decrese col, when out of boundary adjust row and column and make down +//have counetr to exit when all traversed +// Your code here along with comments explaining your approach +class DiagonalTraverse { + public int[] findDiagonalOrder(int[][] mat) { + int[] ans = new int[mat.length*mat[0].length]; + int counter = 0; + //initial row, column + int r = 0; + int c = 0; + //intial up + boolean up = true; + //boundary + int rLen = mat.length-1; + int cLen = mat[0].length-1; + while(true) + { + if(up) + { + //when up add to ans, decrese row, increase col + ans[counter] = mat[r][c]; + counter++; + r--; + c++; + + //boundary check, adjust r and col, make down + //cental diagonal amy be up or down + if(r<0 && c>cLen) + { + c = cLen; + r = 1; + up = false; + } + else if(r<0) + { + r = 0; + c = c; + up = false; + } + else if(c > cLen) + { + r = r + 2; + c = cLen; + up = false; + } + } + else + { + //down, incrase row, decrease col + ans[counter] = mat[r][c]; + counter++; + r++; + c--; + + //boundary check, adjust + //cental diagonal amy be up or down + if(c<0 && r>rLen) + { + r = rLen; + c = 1; + up = true; + } + else if(c<0) + { + c = 0; + r = r; + up = true; + } + else if(r>rLen) + { + r = rLen; + c = c + 2; + up = true; + } + + } + //if counter==mat len return + if(counter==ans.length) + { + break; + } + } + return ans; + } +} \ No newline at end of file diff --git a/Matrix.java b/Matrix.java new file mode 100644 index 00000000..412ed612 --- /dev/null +++ b/Matrix.java @@ -0,0 +1,62 @@ +// 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 +//traverse left to right,top to bottome, right to left, bottom to top +//update boundaries and do boundary check everytime +// Your code here along with comments explaining your approach +class Matrix { + public List spiralOrder(int[][] matrix) { + List list = new LinkedList<>(); + int lB = 0; + int rB = matrix[0].length-1; + int uB = 0; + int loB = matrix.length-1; + int counter = 0; + int totalLen = matrix.length * matrix[0].length; + + while(counter < totalLen) + { + //left to right + if(uB <= loB) { + for(int i = lB; i <= rB; i++) + { + list.add(matrix[uB][i]); + counter++; + } + uB++; + } + + //top to bottom + if(lB <= rB) { + for(int i = uB; i <= loB; i++) + { + list.add(matrix[i][rB]); + counter++; + } + rB--; + } + + //right to left + if(uB <= loB) { + for(int i = rB; i >= lB; i--) + { + list.add(matrix[loB][i]); + counter++; + } + loB--; + } + + //bottom to top + if(lB <= rB) { + for(int i = loB; i >= uB; i--) + { + list.add(matrix[i][lB]); + counter++; + } + lB++; + } + } + return list; + } +} \ No newline at end of file diff --git a/ProductExceptSelf.java b/ProductExceptSelf.java new file mode 100644 index 00000000..47ad0bd2 --- /dev/null +++ b/ProductExceptSelf.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Three line explanation of solution in plain english +//Here we move left to right keeping the track of product till the previous element +//same we can do from right to left and keep track of element till the next element +//we will multiply those to get the product +//Instead fo keeping track of right to left, we just had the rlProduct which has the value of products till next element +// we mutiply that to existing lrProduct to get answer +class ProductExceptSelf { + public int[] productExceptSelf(int[] nums) { + int[] ans = new int[nums.length]; + //always the product next to 0th element 1 + int lrProduct = 1; + ans[0] = lrProduct; + //left to right product except self + for(int i = 1;i=0;j--) + { + rlProduct = nums[j+1]*rlProduct; + ans[j] = ans[j] * rlProduct; + } + return ans; + } +} \ No newline at end of file