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
28 changes: 28 additions & 0 deletions Solution1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TC: O(n) SC: O(1)
// Ran on Leetcode : Yes
/*
Calculate running product, one containing product of all the element left to the element and one right of the element and multiply them both to get product of all the elements except self.
*/
class Solution1 {
public int[] productExceptSelf(int[] nums) {
int result[] = new int[nums.length];
result[0] = 1;

int rp = 1;

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

rp = 1;
// Calc running product
for(int i = nums.length - 2; i >= 0; i--){
rp = rp * nums[i + 1];
result[i] = result[i] * rp;
}

return result;
}
}
57 changes: 57 additions & 0 deletions Solution2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// TC: O(m*n) SC: O(1)
// Ran on leetcode: Yes
/*
Move up first checking for right and top boundry and move down looking for left and bottom boundry in diagonal manner
*/
class Solution2 {
public int[] findDiagonalOrder(int[][] mat) {
int rowLen = mat.length;
int colLen = mat[0].length;

int[] result = new int[rowLen * colLen];

boolean dir = true;

int row = 0;
int col = 0;

for(int i = 0; i < rowLen * colLen; i++){
result[i] = mat[row][col];

//move up
if(dir){
if(col == colLen - 1){
//right boundry
row++;
dir = false;
} else if(row == 0){
//top boundry
col++;
dir = false;
} else {
//move up
row--;
col++;
}

//move down
} else {
if(row == rowLen - 1){
//left boundry
col++;
dir = true;
} else if(col == 0){
//bottom boundry
row++;
dir = true;
} else {
//move down
row++;
col--;
}
}
}

return result;
}
}
52 changes: 52 additions & 0 deletions Solution3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// TC: O(m * n) SC: O(1)
// Ran on leetcode - Yes
/*
Traverse left to right, Traverse top to bottom, Traverse right to left and Traverse bottom to top from outside in recursively till there are no elements left
*/
class Solution3 {
public List<Integer> spiralOrder(int[][] matrix) {
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
List<Integer> re = new ArrayList<>();

// Check the base condition is still valid
while(left <= right && bottom >= top){

//Traverse left to right
if(left <= right && bottom >= top){
for(int i = left; i <= right; i++){
re.add(matrix[top][i]);
}
top++;
}

//Traverse top to bottom
if(left <= right && bottom >= top){
for(int i = top; i <= bottom; i++){
re.add(matrix[i][right]);
}
right--;
}

//Traverse right to left
if(left <= right && bottom >= top){
for(int i = right; i >= left; i--){
re.add(matrix[bottom][i]);
}
bottom--;
}

//Traverse bottom to top
if(left <= right && bottom >= top){
for(int i = bottom; i >= top; i--){
re.add(matrix[i][left]);
}
left++;
}
}

return re;
}
}