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
44 changes: 44 additions & 0 deletions diagonal-traverse.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 use a boolean variable dir to decide whether to move up-right or down-left along the diagonals. When the traversal hits a boundary (top row, bottom row, left column, or right column), it changes direction and adjusts the row or column accordingly until all elements are visited.

class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[] output = new int[m*n];
int row =0;
int col =0;
// If this is true we go up and if this is false we go down
boolean dir = true;
for(int i=0; i<output.length; i++) {
output[i] = mat[row][col];
if(dir) {
if(col == n-1) {
row++;
dir= false;
} else if (row == 0) {
col ++;
dir = false;
} else {
row --;
col ++;
}
} else {
if (row == m-1) {
col++;
dir = true;
} else if (col ==0) {
row++;
dir = true;
} else {
row++;
col --;
}
}
}
return output;

}
}
28 changes: 28 additions & 0 deletions product-of-array-except-self.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity :O(n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : yes
// Three line explanation of solution in plain english : First, the code initializes an output array of the same size as the input array nums to store the result, and a variable runMul to keep track of running products.
// The first loop goes left to right through the array, setting each output[i] to the product of all elements before index i.
// runMul is updated at each step by multiplying it with the current number.The second loop goes right to left, multiplying each output[i] by the product of all elements after index i.
// Again, runMul is updated at each step to accumulate the running product from the right.Finally, the output array contains the product of all elements of nums except the element itself at each index, and it is returned.


class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] output = new int[n];
int runMul =1;
for(int i=0; i<n; i++) {
output[i] = runMul;
runMul *= nums[i];

}

runMul = 1;
for(int i=n-1; i>=0; i--) {
output[i] *= runMul;
runMul *= nums[i];
}
return output;
}
}
57 changes: 57 additions & 0 deletions spiral-matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 keep track of the current direction (RIGHT, DOWN, LEFT, UP) using an enum. We use bounds (rightBound, downBound, leftBound, upperBound) to know when to turn and shrink the spiral. In each iteration, we add the current element to the output list and moves the row/column according to the direction. When a bound is reached, it changes direction and updates the corresponding bound until all elements are added.

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> output = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;
enum Direction {UP, DOWN, LEFT, RIGHT}
int rightBound = n-1;
int downBound = m-1;
int leftBound = 0;
int upperBound = 1;
int row = 0;
int col = 0;
Direction dir = Direction.RIGHT;
for(int i=0; i<m*n; i++) {
output.add(matrix[row][col]);
if (dir == Direction.RIGHT) {
if(col == rightBound) {
row++;
rightBound--;
dir = Direction.DOWN;
} else {
col++;
}
} else if (dir == Direction.DOWN) {
if (row == downBound) {
col--;
downBound--;
dir = Direction.LEFT;
} else {
row++;
}
} else if (dir == Direction.LEFT) {
if (col == leftBound) {
row--;
leftBound++;
dir = Direction.UP;
} else {
col--;
}
} else {
if (row == upperBound) {
col++;
upperBound++;
dir = Direction.RIGHT;
} else {
row--;
}
}
}
return output;
}
}