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
61 changes: 61 additions & 0 deletions DiagonalMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Time Complexity :O(M*N)
// Space Complexity : O(1) without considering result space.
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach

class DiagonalMatrix {
public int[] findDiagonalOrder(int[][] matrix) {
if (matrix == null) {
throw new IllegalArgumentException("Input matrix is null");
}
if (matrix.length == 0 || matrix[0].length == 0) {
return new int[0];
}

int rows = matrix.length;
int cols = matrix[0].length;
int[] result = new int[rows * cols];
int r = 0;
int c = 0;
boolean up = true;

for (int i = 0; i < result.length; i++) {
result[i] = matrix[r][c];
if(up) { // Move Up
if (c == cols - 1) {
// Reached last column. Now move to below cell in the same column.
// This condition needs to be checked first due to top right corner cell.
r++;
up=false;
} else if (r == 0) {
// Reached first row. Now move to next cell in the same row.
c++;
up=false;
} else {
// Somewhere in middle. Keep going up diagonally.
r--;
c++;
}
} else { // Move Down
if (r == rows - 1) {
// Reached last row. Now move to next cell in same row.
// This condition needs to be checked first due to bottom left corner cell.
c++;
up=true;
} else if (c == 0) {
// Reached first columns. Now move to below cell in the same column.
r++;
up=true;
} else {
// Somewhere in middle. Keep going down diagonally.
r++;
c--;
}
}
}

return result;
}
}
37 changes: 37 additions & 0 deletions Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Time Complexity : O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english
/*Calculate the product on the left of all the numbers in the array.
Going from right to left calculate the product on the right of the number and multiply with the product obtained before at each position
*/

// Your code here along with comments explaining your approach

/*
## Problem 1 - Leetcode 238.Product of Array Except Self

*/

class Product {

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

int[] leftProduct = new int[nums.length];
result[0] = 1;

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

int rightProduct = 1;
for (int i = nums.length - 2; i >= 0; i--) {
rightProduct *= nums[i + 1];
result[i] = result[i] * rightProduct;
}

return result;

}
}
55 changes: 55 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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
//Take four pointers ->left, top, right, and bottom.
//And navigate left to right, top to bottom, right to left, bottom to top in that order.
//Increase or decrease the pointers accordingly.

// Your code here along with comments explaining your approach
class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();

int m = matrix.length;
int n = matrix[0].length;

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

while (left <= right && top <= bottom) {

for (int j = left; j <= right; j++) {
result.add(matrix[top][j]);
}
top++;


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


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

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

}

return result;

}
}