Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Leetcode_238.java
Original file line number Diff line number Diff line change
@@ -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<n;i++){
int p=1;
for(int j=0;j<n;j++){
if(i!=j){
p*=nums[j];
}
}
ans[i]=p;
}

return ans;
}
}

//way2
//for every index:
//identify left product--leftproduct array
//identify right product--right product array
//multiply leftproduct and right product
//TC: O(3n)--> 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<n;i++){
p=p*nums[i-1];
leftp[i]=p;
}

//right product array
int[] rightp=new int[n];
rightp[n-1]=1;
p=1;

for(int i=n-2;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<n;i++){
ans[i]=(leftp[i]*rightp[i]);
}

return ans;
}
}

//way3
//for every index:
//identify left product--leftproduct array
//identify right product--right product array
//multiply leftproduct and right product
//TC: O(2n)--> 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<n;i++){
p=p*nums[i-1];
leftp[i]=p;
}

//right product array--instead of this, let's go with a constant product variable
// int[] rightp=new int[n];
// rightp[n-1]=1;
// p=1;

// for(int i=n-2;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;
}
}
48 changes: 48 additions & 0 deletions Leetcode_498.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
139 changes: 139 additions & 0 deletions Leetcode_54.java
Original file line number Diff line number Diff line change
@@ -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<Integer> spiralOrder(int[][] matrix) {
int m=matrix.length;
int n=matrix[0].length;

int top=0,left=0,bottom=m-1,right=n-1;
List<Integer> 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<Integer> spiralOrder(int[][] matrix) {
int m=matrix.length;
int n=matrix[0].length;

int top=0,left=0,bottom=m-1,right=n-1;
List<Integer> 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<Integer> 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);


}
}