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
52 changes: 52 additions & 0 deletions DiagonalTraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
// TC - O(m*n)
// SC - O(m*n)
/*
To traverse matrix diagonally, we need to keep track of the direction we are moving in and what diagonal index to move to next.
We use flag to keep track of direction, so flag is true when e move up the matrix and false otherwise.
If flag is true and we reach the topmost row set the flag to false and incr column index
*/
int n = mat.length;
int m = mat[0].length;

int[] result = new int[n*m];
int r = 0, c = 0;
boolean flag = true;

for (int i = 0; i< n*m; i++) {
result[i]= mat[r][c];
if (flag) {
//If flag is true and we reach the topmost row and column is not the last, set the flag to false and incr column index
if (r == 0 && c != m - 1) {
flag = false;
c++;
} else if (c == m - 1) {
//If flag is true and we reach the last column, set the flag to false and incr row index
flag = false;
r++;
} else {
// else we keep moving up diagonally by decr row and incr col
r--;
c++;
}
} else {
if (c == 0 && r != n - 1) {
//If flag is false and we reach the topmost column and row is not the last, set the flag to true and incr row index
r++;
flag = true;
} else if (r == n - 1) {
//If flag is false and we reach the last row, set the flag to true and incr col index
flag = true;
c++;
} else {
// else we keep moving down diagonally by incr row and decr col
r++;
c--;
}
}
}

return result;
}
}
28 changes: 28 additions & 0 deletions ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
// TC - O(n)
// SC - O(1) (not including result array)
/*
To calculate product of all elements except self, we first run a loop from left to right
and calculate prouduct with each element to the left (which will be stored at index - 1 as we have already calculated that).
And then we pass the array right to left and keep a runningSum initialize with the rightmost element and then keep
multiplying it with values at the index. We also keep multiplying the runningSum value at each index.
*/

int[] product = new int[nums.length];
product[0] = 1;

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

int runningProduct = nums[nums.length - 1];

for (int i = nums.length - 2; i >= 0; i--) {
product[i] = product[i] * runningProduct;
runningProduct *= nums[i];
}

return product;
}
}
53 changes: 53 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.*;

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
// TC - O(m*n)
// SC - O(m*n)
/*
To visit matrix in a spiral we take 4 pointers top, bottom, left, and right. We iterate from left to right (print top row and incr top pointer)
, then top to bottom (print right column and decrement right pointer), right to left (print bottom row and decr bottom pointer) and
bottom to top (print left row and incr left pointer) in each loop.
*/
List<Integer> list = new ArrayList<>();
int n = matrix.length;
int m = matrix[0].length;
int top = 0;
int bottom = n - 1;
int left = 0;
int right = m - 1;

// termination condition if top and bottom cross or right and left pointers cross
while (top <= bottom && left <= right) {
//print top row and incr top pointer
for (int j = left; j <= right; j++) {
list.add(matrix[top][j]);
}
top++;

//print right column and decrement right pointer
for (int i = top; i <= bottom; i++) {
list.add(matrix[i][right]);
}
right--;

//check if top and bottom not invalid and print bottom row and decr bottom pointer
if (top <= bottom) {
for (int j = right; j >= left; j--) {
list.add(matrix[bottom][j]);
}
bottom--;
}

//check if left and right not invalid and print left row and incr left pointer
if (left <= right) {
for (int i = bottom; i >= top; i--) {
list.add(matrix[i][left]);
}
left++;
}
}

return list;
}
}