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
54 changes: 54 additions & 0 deletions DiagonalTraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 the input array by setting boolean variable for upwards and downwards motion and play accordingly
depending upon edge cases.Make sure to insert each incoming matrix's element into output array.Usual upwards
traversal includes column++ and row--,but, we need to handle edge cases where column shouldnt exceed bounds and
when row=0. Similarly, for downwards traversal,usual route is row++ and column--, but,check edge cases where
row is about to exceed boundary and when column is 0.
*/
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int rowLen = mat.length;
int colLen = mat[0].length;

boolean direction = true; //upwards
int[] traversedArray = new int[rowLen * colLen];
int r = 0 , c = 0;

for(int i = 0 ; i < rowLen * colLen ; i++) {
traversedArray[i] = mat[r][c];
if(direction) {
if(c == colLen - 1) {
r++;
direction = false;
} else if(r == 0) {
c++;
direction = false;
}
else {
r--;
c++;
}

}
else { //downwards
if(r == rowLen - 1) {
c++;
direction = true;
} else if(c == 0) {
r++;
direction = true;
}
else {
r++;
c--;
}
}
}
return traversedArray;
}
}
32 changes: 32 additions & 0 deletions ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english
/*
compute left pass and right pass products by using single resultant array by considering running product
except the index of the current element and then eventually multiply the results of both these passes to
get the overall product.
*/

class Solution {
public int[] productExceptSelf(int[] nums) {
int len = nums.length;
int[] productArr = new int[len];
int product = 1;

productArr[0] = 1;

for(int i = 1 ; i < len ; i++) {
product = product * nums[i - 1];
productArr[i] = product;
}

product = 1;

for(int i = len - 2 ; i >= 0 ; i--) {
product = product * nums[i + 1];
productArr[i] *= product;
}
return productArr;
}
}
44 changes: 44 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 take four pointers to help us traverse in all 4 directions. To achieve spiral, we leverage any 2 pointers
to traverse, at the same time depending upon the desired way of output.We also make sure of the boundary
conditions are in check and not crossing each other and subsequently add the matrix elements to our
output list.
*/


class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int rowLen = matrix.length;
int colLen = matrix[0].length;

int top = 0 , left = 0 , right = colLen - 1, bottom = rowLen - 1;
List<Integer> spiralList = new ArrayList<>();

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

for(int i = top ; i <= bottom ; i++)
spiralList.add(matrix[i][right]);
right--;

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

if(left <= right) {
for(int i = bottom ; i >= top ; i--)
spiralList.add(matrix[i][left]);
}
left++;
}
return spiralList;
}
}