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
42 changes: 42 additions & 0 deletions Array_Product_Except_Self.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time Complexity : O(N) -> To traverse N elements of input array
// Space Complexity : O(1) -> Since the ouput array that we created is used to store left product of each input element and then update each left product in output array with right product * left product and finally return as output. So, no extra space used
// Did this code successfully run on Leetcode : Yes
// We created an output array to store the left product of each element in the input array. Then we traverse the input array from the end to calculate the right product and update the output array by multiplying the right product with the left product already stored in the output array.

// Your code here along with comments explaining your approach
class Solution {
public int[] productExceptSelf(int[] nums) {
// Base condition - If array is null, no product
if(nums == null || nums.length == 0){
return new int[0];
}

// Create a new output array
// In that output array, we will store left product of each element first
// Then, calculate right product of each element and update output array with left*right product
int[] output = new int[nums.length];
// Initialize a product to 1 - left product of 1st element in input array will be 1
int product = 1;

/////LEFT PRODUCT//////
// Calculate left product of each element in input array and store in output array
for(int i=0; i < nums.length; i++){
output[i] = product; // For element at index i, store the left product in output array
// calculate and update left product for next element
product = product * nums[i];
}

/////RIGHT PRODUCT//////
// Calculate right product of each element and update output array with left*right product. start at last element of input array to calculate right product
// Reset product = 1 as right product of last element in input array will be 1
product = 1;
for(int i = nums.length - 1; i >= 0; i--){
// We have the right product for element i - We multiply right product with left product in output array at index i and update output array
//output[i] = left product * right product;
output[i] = output[i] * product;
// calculate and update right product for previous element
product = product * nums[i];
}
return output;
}
}
68 changes: 68 additions & 0 deletions Diagonal_Traverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Time Complexity : O(m * n) -> To traverse 'm' rows and 'n' columns in the given matrix
// Space Complexity : O(1) -> An array created is returned as an output so no extra space used
// Did this code successfully run on Leetcode : Yes
// We traverse the given matrix in upward and downward diagonal direction based on the current position of row and column pointers. We use a direction pointer to decide whether to traverse in upward or downward direction. We fill in the output array with elements from the matrix as we traverse it.

// Your code here along with comments explaining your approach
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
// Base condition
if(mat == null || mat.length == 0 || mat[0].length == 0){
return new int[0]; // return empty array
}

// Number of rows and columns
int m = mat.length; // 'm' rows
int n = mat[0].length; // 'n' columns

// Initialize pointers to traverse matrix of size m * n
int i = 0;
int j = 0;
int dirs = 1; // dirs pointer to decide direction to traverse in matrix

// Initialize index pointer to fill in the output array with elements
int index = 0;

// Create an output array - size is equal to number of elements in input array - m * n
int[] output = new int[m * n];

// Traverse the given matrix based on upward/downward direction and fill in output array until index < m * n (length of matrix)
while(index < m * n){
output[index] = mat[i][j];
index += 1;
// Traversing in upward direction
if(dirs == 1){
// last column - upward to downward direction change, next row
if(j == n - 1){
dirs -= 1; // direction change to downward
i++;
// 1st row - upward to downward direction change, next column
}else if(i == 0){
dirs -= 1; // direction change to downward
j++;
// except 1st row and last column - tarversal to upward direction is done by decrementing row and incrementing column
}else{
i--;
j++;
}
// Traversing in downward direction
}else{
// last row - downward to upward direction change, next column
if(i == m - 1){
dirs += 1; // direction change to upward
j++;
// 1st column - downward to upward direction change, next row
}else if(j == 0){
dirs += 1; // direction change to upward
i++;
// except last row and 1st column - traversal to downward direction is done by incrementing row and decrementing column
}else{
i++;
j--;
}
}

}
return output;
}
}
6 changes: 0 additions & 6 deletions Sample.java

This file was deleted.

65 changes: 65 additions & 0 deletions Spiral_Matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Time Complexity : O(m * n) -> To traverse m * n elements in given matrix
// Space Complexity : O(1) -> Constant space. The output array/list created is returned as output so not considered as extra space
// Did this code successfully run on Leetcode : Yes
// We are traversing the matrix in spiral order by maintaining four pointers - top, bottom, left and right. We traverse the matrix in four directions - left to right, top to bottom, right to left and bottom to top and update the pointers accordingly until all elements are traversed

// Your code here along with comments explaining your approach
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
// Base condition
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return new ArrayList<>();
}

// 1. Create output Array
List<Integer> output = new ArrayList<>();

// 2. Length of rows and columns
int m = matrix.length; // rows
int n = matrix[0].length; // columns

// 3. Initialize the pointers
int top = 0; int left = 0;
int bottom = m - 1; int right = n - 1;

// 4. Traverse the matrix elements in spiral order and store in output list
while(top <= bottom && left <= right){
// top row: left -> right
// No need to check for top, bottom or left, right bounds as we just checked. So no IF condition added here
for(int x = left; x <= right; x++){
output.add(matrix[top][x]);
}
top++;

// right column: top -> bottom
// No need for (top <= bottom) condition as its getting checked in for loop. No need for (left <= right) as no change in left, right pointers yet
for(int x = top; x <= bottom; x++){
output.add(matrix[x][right]);
}
right--;

// bottom row: right -> left
// We need (top <= bottom) condition here, as top pointer is changed above. No need for (left <= right) as its being checked in for loop
if(top <= bottom){
for(int x = right; x >= left; x--){
output.add(matrix[bottom][x]);
}
bottom--;
}

// left column: bottom -> top
// We need (left <= right) condition here, as right pointer is changed above. No need for (top <= bottom) as its being checked in for loop
if(left <= right){
for(int x = bottom; x >= top; x--){
output.add(matrix[x][left]);
}
left++;
}
}

// 5. Return the output array/list
return output;

}

}