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
39 changes: 39 additions & 0 deletions Diagonal-traverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[] res = new int[m*n];
boolean flag=true;
int r=0;
int c=0;
for(int i=0;i<m*n;i++){
res[i] = mat[r][c];
//up
if(flag){
if(r==0 && c!=n-1){
c++;
flag=false;
}else if(c==n-1){
r++;
flag=false;
}else{
r--;
c++;
}
}else{ //down
if(r==m-1){
c++;
flag=true;
}else if(c==0){
r++;
flag=true;
}else{
r++;
c--;
}
}
}
return res;

}
}
29 changes: 29 additions & 0 deletions Product-of-array-except-self.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
//Space complexity O(2n)
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
int[] res = new int[n];
//left
int rp= 1;
left[0] = rp;
for(int i=1;i<n;i++){
left[i] = nums[i-1]*rp;
rp = left[i];
}
//right
rp=1;
right[n-1]=rp;
for(int i=n-2;i>=0;i--){
right[i]=nums[i+1]*rp;
rp = right[i];
}

//result
for(int i=0;i<n;i++){
res[i] = left[i] * right[i];
}
return res;
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Note: Please solve it without division and in O(n).
Follow up:
Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

* 1st approach - use 2 loops O(n^2) where if(i!=j) res[]*=nums[j]
* 2nd approach = var rp=1, take left product array product of element in left side of nums[i], right product array product of element in right side of nums[i].
* Take product of left array & right array.

## Problem 2

Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
Expand All @@ -33,6 +37,10 @@ Input:

Output: [1,2,4,7,5,3,6,8,9]

* 2 directions in traversal given by boolean flag
* do the edge condition where direction is changing


## Problem 3
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Expand Down Expand Up @@ -64,3 +72,7 @@ Input:

]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

* we need to consider the elements in Spiral. don't repeat the elements.
* 4 directions - 4 pointers - top, bottom, left, right.
* form loop around how is the movement.
51 changes: 51 additions & 0 deletions Spiral-matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int top=0, bottom=m-1, left=0, right=n-1;
List<Integer> list = new ArrayList<>();
while(top<=bottom && left<=right){
//top

for(int i=left;i<=right;i++){
list.add(matrix[top][i]);

}
top++;



//right
if(top<=bottom && left<=right){
for(int i=top;i<=bottom;i++){
list.add(matrix[i][right]);

}
right--;
}


//bottom
if(top<=bottom && left<=right){
for(int i=right;i>=left;i--){
list.add(matrix[bottom][i]);

}
bottom--;
}


//left
if(top<=bottom && left<=right){
for(int i=bottom;i>=top;i--){
list.add(matrix[i][left]);

}
left++;
}


}
return list;
}
}