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 ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//Time Complexity: O(n)
//Space Complexity: O(n)
//Approach: Find running product for both left pass and right pass and add product of these two into the final result to get the
//product of all of the number except self
class Solution {
public int[] productExceptSelf(int[] nums)
{
int n = nums.length;

//Validate the inputs
if (nums == null || nums.length == 0) return new int[n];

int[] rProduct = new int[n];
int[] lProduct = new int[n];
lProduct[0] = 1;
rProduct[n-1] = 1;
int runProduct = 1;

//Right pass to find running product from left to right
for(int i=1; i < n; i++)
{
runProduct = nums[i-1] * runProduct;
lProduct[i] = runProduct;
}

//Reset running product befor right pass
runProduct = 1;

//Left pass to find running product from right to left
for(int i=n-2; i >= 0; i--)
{
runProduct = nums[i+1] * runProduct;
rProduct[i] = runProduct;
}

//Add right product * left product in original array
for(int i = 0; i < n; i++)
{
nums[i] = rProduct[i] * lProduct[i];
}

return nums;
}
}
59 changes: 59 additions & 0 deletions SpiralMatrix
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//Time Complexity : O(m * n)
//Space Complexity: O(1)
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {

List<Integer> result = new ArrayList<>();

//Validate inputs
if (matrix == null || matrix.length == 0) return result;

int top = 0, left = 0;
int right = matrix[0].length - 1;
int bottom = matrix.length - 1;

while(left <= right && top <= bottom)
{
//Left to right
for(int i = left; i <= right; i++)
{
result.add(matrix[top][i]);
}

//Move the top
top++;

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

//Move the right
right--;

// Recheck the condition since top is modified after initial
if(top <= bottom){
// Right to Left
for(int i = right; i >= left; i--){
result.add(matrix[bottom][i]);
}
}

//Move bottom
bottom--;

//Recheck this condition since the right is modified after above
if(left <= right){
//Bottom to Top iteration
for(int i = bottom; i >= top; i--){
result.add(matrix[i][left]);
}
}

//Move Left
left++;
}
return result;
}
}
59 changes: 59 additions & 0 deletions TraverseDiagonal
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//Time Complexity: O(mxn)
//Space Complexity: O(1)
class Solution {
int[] result;
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
this.result = new int[m * n];

//Validate inputs
if(mat == null || mat.length == 0) return result;

boolean upward = true;
int i = 0, row = 0, col = 0;

while(i < m*n)
{
result[i] = mat[row][col];

// Going upward direction
if(upward)
{
if(col == n-1) // Right most element is reached - This needs to be checked as there is a chance for 0, n-1
{
row++;
upward = false;
}
else if(row == 0) // Initially when we move from first element to right
{
col++;
upward = false;
}
else{ // Regular upward movement
row--;
col++;
}
}
else
{
if(row == m-1)
{
col++;
upward = true;
}
else if(col == 0)
{
row++;
upward = true;
}
else{
row++;
col--;
}
}
i++;
}
return result;
}
}