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
59 changes: 59 additions & 0 deletions DiagonalTraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Time Complexity : O(MXN), Go over each element of the matrix once
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Approach :
// We need to identify whether the diagonal is going in upward direction or downward direction. To identify that,
// we can increment it, if the diagonal is even, we move downwards and increment row by 1 and decrement col by 1.
// If the digonal is odd, we move upwards, increment col by 1 and decrement row by 1;
// We will have to put additional checks when row or col becomes 0 or size, to avoid index of bounds error.
public class DiagonalTraverse {

public int[] findDiagonalOrder(int[][] matrix) {

int m = matrix.length;
if(m == 0) return new int[0];
int n = matrix[0].length;
if(n == 0) return new int[0];
int total = m * n;
int[] result = new int[total];

int diagonal = 1;
int index = 0;
int row = 0, col = 0;
while (index < total) {
result[index] = matrix[row][col];
index++;
if (diagonal % 2 == 0) {
// To handle scenario where the cell is in both first col and last row
// Dont go out of bounds when in last row
if (col == 0 && row != m - 1) {
row++;
diagonal++;
} else if (row == m - 1) {
col++;
diagonal++;
} else {
row++;
col--;
}
} else {
// To handle scenario where the cell is in both first row and last col
// Dont go out of bounds when in last col
if (row == 0 && col != n - 1) {
col++;
diagonal++;
} else if (col == n - 1) {
row++;
diagonal++;
} else {
row--;
col++;
}

}
}

return result;
}

}
30 changes: 30 additions & 0 deletions ProductExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity : O(N), go through all elements to find the product
// Space Complexity : O(1), no additional space used
// Did this code successfully run on Leetcode : Yes
// Approach :

// We can keep prefix product at index i without including value at index i. So in the first pass, we can multiply values
// from left to right, where the product at [i] is of values at [i-1]. Similarly, we multiply values from right to left
// and multiply it with exiting product at [i];
public class ProductExceptSelf {

public int[] productExceptSelf(int[] nums) {
int size = nums.length;
int[] product = new int[size];

int temp = 1;
product[0] = temp;
for(int i=1; i < size; i++){
product[i] = temp * nums[i-1];
temp *= nums[i-1];
}

temp = 1;
for(int i= size-2; i >= 0; i--){
product[i] = product[i] * temp * nums[i+1];
temp *= nums[i+1];
}

return product;
}
}
61 changes: 61 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Time Complexity : O(MXN), Go over each element of the matrix once
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Approach :
// To traverse in spiral order, we need to move from left to right direction, then top to bottom, them right to left and
// finally bottom to up. So we will use 4 pointers top, bottom, left and right. Since the spiral would be shrinking at
// every level, we need to shrink our boundaries too, thus reduce our pointers by 1 each time.
import java.util.ArrayList;
import java.util.List;

class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
List<Integer> result = new ArrayList<>();
if (m == 1 && n == 1) {
result.add(matrix[0][0]);
return result;
}

int top = 0, left = 0;
int bottom = m - 1, right = n - 1;

//Keep adding elements till the boundaries cross each other
// top == bottom or left == right is valid case
while (top <= bottom && left <= right) {

for (int i = left; i <= right; i++) {
result.add(matrix[top][i]);
}
top++;
// check boundaries since pointer was updated in last line
if (isInvalidBounds(top, bottom)) break;

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

if (isInvalidBounds(left, right)) break;

for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;

if (isInvalidBounds(top, bottom)) break;

for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}

return result;
}

public boolean isInvalidBounds(int start, int end) {
return start > end;
}
}