diff --git a/Leetcode_238.java b/Leetcode_238.java new file mode 100644 index 00000000..87c57cf8 --- /dev/null +++ b/Leetcode_238.java @@ -0,0 +1,106 @@ +//way1 +//linear-o(n^2) +class Solution { + public int[] productExceptSelf(int[] nums) { + int n=nums.length; + + int[] ans=new int[n]; + + for(int i=0;i 3 constant; SC: O(n) +class Solution { + public int[] productExceptSelf(int[] nums) { + int n=nums.length; + + //left product array + int[] leftp=new int[n]; + leftp[0]=1; + int p=1; + + for(int i=1;i=0;i--){ + p=p*nums[i+1]; + rightp[i]=p; + } + + //product at every place + int[] ans=new int[n]; + + for(int i=0;i 2 constant; SC: O(n) +class Solution { + public int[] productExceptSelf(int[] nums) { + int n=nums.length; + + //left product array + int[] leftp=new int[n]; + leftp[0]=1; + int p=1; + + for(int i=1;i=0;i--){ + // p=p*nums[i+1]; + // rightp[i]=p; + // } + + //product at every place + int[] ans=new int[n]; + int pd=1; + ans[n-1]=leftp[n-1]; + + for(int i=n-2;i>=0;i--){ + pd=pd*nums[i+1]; + ans[i]=(leftp[i]*pd); + } + + return ans; + } +} diff --git a/Leetcode_498.java b/Leetcode_498.java new file mode 100644 index 00000000..79a25a85 --- /dev/null +++ b/Leetcode_498.java @@ -0,0 +1,48 @@ +//top: flag=true +//bottom: flag=false +//TC: O(m*n) + +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m=mat.length; + int n=mat[0].length; + + int r=0,c=0; + boolean flag=true; + + int[] ans=new int[m*n]; + ans[0]=mat[0][0]; + int i=1; + while(i<(m*n)){ + if(flag){ + if(r==0 && !(c==n-1)){ + c++; + flag=false; + }else if(c==n-1){ + r++; + flag=false; + }else{ + r--; + c++; + } + + }else{ + if(c==0 && !(r==m-1)){ + r++; + flag=true; + }else if(r==m-1){ + c++; + flag=true; + }else{ + r++; + c--; + } + + } + ans[i]=mat[r][c]; + i+=1; + } + + return ans; + } +} diff --git a/Leetcode_54.java b/Leetcode_54.java new file mode 100644 index 00000000..1d79f8a6 --- /dev/null +++ b/Leetcode_54.java @@ -0,0 +1,139 @@ +//way1 +//initiate top=0,left=0,right=col-1,bottom=row-1 +//boundaries are shrinking every time. +//So, inc top and left; dex right and bottom +//Do a traversal based on above four values +//tc: O(m*n); sc:O(1) +class Solution { + public List spiralOrder(int[][] matrix) { + int m=matrix.length; + int n=matrix[0].length; + + int top=0,left=0,bottom=m-1,right=n-1; + List ans=new ArrayList<>(); + int idx=0; + + while(top<=bottom && left<=right){ + //right + + if(top<=bottom && left<=right){ + for(int i=left;i<=right;i++) + { + ans.add(matrix[top][i]); + idx++; + + } + top++; + } + + + //down + if(top<=bottom && left<=right){ + for(int i=top;i<=bottom;i++){ + ans.add(matrix[i][right]); + idx++; + + } + right--; + } + + + //left + if(top<=bottom && left<=right){ + for(int i=right;i>=left;i--){ + ans.add(matrix[bottom][i]); + idx++; + + } + bottom--; + } + + + //up + if(top<=bottom && left<=right){ + for(int i=bottom;i>=top;i--){ + ans.add(matrix[i][left]); + idx++; + + } + left++; + + } + + } + + return ans; + } +} + +//way2 +//initiate top=0,left=0,right=col-1,bottom=row-1 +//boundaries are shrinking every time. +//So, inc top and left; dex right and bottom +//Do a traversal based on above four values +//tc: O(m*n); sc:O(1) + +class Solution { + public List spiralOrder(int[][] matrix) { + int m=matrix.length; + int n=matrix[0].length; + + int top=0,left=0,bottom=m-1,right=n-1; + List ans=new ArrayList<>(); + + helper(matrix,top,left,bottom,right,ans); + return ans; + } + + private void helper(int[][] matrix, int top,int left, int bottom, int right,List ans){ + + if(top>bottom || left>right){ + return; + } + + //right + + if(top<=bottom && left<=right){ + for(int i=left;i<=right;i++) + { + ans.add(matrix[top][i]); + } + top++; + } + + + //down + if(top<=bottom && left<=right){ + for(int i=top;i<=bottom;i++){ + ans.add(matrix[i][right]); + + } + right--; + } + + + //left + if(top<=bottom && left<=right){ + for(int i=right;i>=left;i--){ + ans.add(matrix[bottom][i]); + + } + bottom--; + } + + + //up + if(top<=bottom && left<=right){ + for(int i=bottom;i>=top;i--){ + ans.add(matrix[i][left]); + + } + left++; + + } + + helper(matrix,top,left,bottom,right,ans); + + + } +}