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
91 changes: 91 additions & 0 deletions DiagonalTraverse.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
62 changes: 62 additions & 0 deletions Matrix.java
Original file line number Diff line number Diff line change
@@ -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<Integer> spiralOrder(int[][] matrix) {
List<Integer> 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;
}
}
32 changes: 32 additions & 0 deletions ProductExceptSelf.java
Original file line number Diff line number Diff line change
@@ -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<nums.length;i++)
{
lrProduct = nums[i-1] * lrProduct;
ans[i] = lrProduct;
}
//same as before product next to right of len is 1
int rlProduct = 1;
//insated of having separate array, update the ans array to multply right to left product
for(int j = nums.length-2;j>=0;j--)
{
rlProduct = nums[j+1]*rlProduct;
ans[j] = ans[j] * rlProduct;
}
return ans;
}
}