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
63 changes: 63 additions & 0 deletions DiagonalTraverseMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Time Complexity : O(M*N)
// Space Complexity : O(M*N)
// 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 Solution {
public int[] findDiagonalOrder(int[][] mat) {
// for up r-- and c++
// for down r++ and c--
// use a flag to determine if to go up or down - true is up and false is down
// edge cases - a. to move to next element in row for going down and going up
// for first anf last row
// b. go to next element in col for first and last col

boolean flag = true;
int[] result = new int[mat.length * mat[0].length];
int r = 0, c = 0;
for(int i = 0; i < mat.length * mat[0].length; i++)
{
result[i] = mat[r][c];
if(flag)
{
if(r == 0 && c != mat[0].length - 1)
{
c++;
flag = false;
}
else if(c == mat[0].length - 1)
{
r++;
flag = false;
}
else
{
r--;
c++;
}
}
else
{
if(c == 0 && r != mat.length - 1)
{
r++;
flag = true;
}
else if(r == mat.length - 1)
{
c++;
flag = true;
}
else
{
r++;
c--;
}
}
}

return result;
}
}
29 changes: 29 additions & 0 deletions ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Time Complexity : O(N)
// Space Complexity : O(N)
// 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 Solution {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
int rp = 1;
//left pass
result[0] = 1;
for(int i = 1; i < nums.length; i++)
{
rp *= nums[i-1];
result[i] = rp;
}
// right pass
// reset running product
rp = 1;
for(int i = nums.length - 2; i>= 0; i--)
{
rp *= nums[i+1];
result[i] *= rp;
}
return result;
}
}
54 changes: 54 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.List;
import java.util.ArrayList;
// Time Complexity : O(M*N)
// Space Complexity : O(M*N)
// 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 Solution {
public List<Integer> spiralOrder(int[][] matrix) {
// use left,right, top and buttom pointer to maintain the spiral
// top prints rows from left to right
// right prints cols from top to bottom
// bottom prints rows from right to left
// left prints cols from bottom to top
List<Integer> result = new ArrayList<>();
int left = 0, right = matrix[0].length - 1;
int top = 0, bottom = matrix.length - 1;
while(left <= right && top <= bottom)
{
// top row
for(int i = left; i <= right; i++)
{
result.add(matrix[top][i]);
}
top++;
// right col
for(int i = top; i <= bottom; i++)
{
result.add(matrix[i][right]);
}
right--;
// bottom row
if(top <= bottom)
{
for(int i = right; i >= left; i--)
{
result.add(matrix[bottom][i]);
}
bottom--;
}
// left col
if(left <= right && top <= bottom)
{
for(int i = bottom; i >= top; i--)
{
result.add(matrix[i][left]);
}
left++;
}
}
return result;
}
}