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
49 changes: 48 additions & 1 deletion Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,51 @@
// Did this code successfully run on Leetcode :
// Three line explanation of solution in plain english

// Your code here along with comments explaining your approach
// Your code here along with comments explaining your approach
// Question 1. https://leetcode.com/problems/product-of-array-except-self/description/
// Approach 1
// For each number in the array we calculate the product of other numbers
// We will skip the current element during multiplication and result store in new array
// Time complexity : O(n^2)
// Space complexity : O(1)
// It gave TLE (Time Limit Exceeded)
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];
for(int i=0;i<nums.length;++i){
int prod = 1;
for(int j=0;j<nums.length;++j){
if(i!=j){
prod = prod * nums[j];
}
}
result[i] = prod;
}
return result;
}
}
// Approach 2
// 1. First we do left to right pass store the running product before each index
// 2. Then we do right to left pass, multiple the existing product with right side products
// Time complexity : O(n)
// Space complexity : O(1)
// Did this code successfully run on Leetcode : yes
class Solution {
public int[] productExceptSelf(int[] nums) {
int prod = 1;
int[] result = new int[nums.length];
result[0] = 1;
// left to right store the running product
for(int i=1;i<nums.length;++i){
result[i] = prod * nums[i-1];
prod = prod * nums[i-1];
}
prod = 1;
// right to left multiple the existing product with current product from right side
for(int i=nums.length-2;i>=0;--i){
prod = prod * nums[i+1];
result[i] = result[i] * prod;
}
return result;
}
}
49 changes: 49 additions & 0 deletions Sample1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* https://leetcode.com/problems/diagonal-traverse/description/
* Approach : Start from top-left of matrix and traverse the matrix diagonally
* Then switch the direction when hitting the boundaries(top,left,right,bottom)
* Keep updating the ans array as we move in up or down directions.
* Time complexity : O(n*m) when n is row and m is column
* Space complexity : O(1)
* Did this code successfully run on Leetcode : yes
*/
class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[] ans = new int[m*n];
boolean dir = true;
int r = 0;
int c = 0;
for(int i=0;i<(m*n);++i){
ans[i] = mat[r][c];
if(dir){
if(r==0 && c!=n-1){
c++;
dir = false;
}else if(c==n-1){
r++;
dir = false;
}else{
c++;
r--;
}

}else{
if(c==0 && r!=m-1)
{
r++;
dir = true;
}else if(r==m-1){
c++;
dir = true;
}else{
r++;
c--;
}

}
}
return ans;
}
}
89 changes: 89 additions & 0 deletions Sample2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* https://leetcode.com/problems/spiral-matrix/description/
* Approach 1: We use 4 boundaries - top,bottom,left and right to keep track of spiral path
* At each step we traverse, right, down, left, and up then shrink the boundaries
* We stop when boundaries cross each other.
*
* Time complexity : O(n*m)
* Space complexity : O(1)
*/
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int r = matrix.length;
int c = matrix[0].length;
int top = 0;
int bottom = r-1;
int left = 0;
int right = c-1;
while(top<=bottom && left<=right){
for(int j=left; j<=right;++j){
list.add(matrix[top][j]);
}
top++;
for(int i=top;i<=bottom;++i){
list.add(matrix[i][right]);
}
right--;
if(top<=bottom){
for(int j=right;j>=left;--j){
list.add(matrix[bottom][j]);
}
}
bottom--;
if(left<=right){
for(int j=bottom;j>=top;--j){
list.add(matrix[j][left]);
}
}

left++;
}
return list;
}
}
/**
* https://leetcode.com/problems/spiral-matrix/description/
* Approach 2: We use 4 boundaries - top,bottom,left and right to keep track of spiral path
* At each step we traverse, right, down, left, and up then shrink the boundaries
* We stop when boundaries cross each other.
*
* Time complexity : O(n*m)
* Space complexity : O(depth) depth of recursion which in worst case O(m+n)
*/
class Solution {
List<Integer> ans;
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
this.ans = new ArrayList<>();
int top = 0, left = 0, bottom = m - 1, right = n - 1;
helper(matrix, top, left, bottom, right);
return ans;
}

private void helper(int[][] matrix, int top, int left, int bottom, int right) {
if(top>bottom || left>right) return;
for (int i = left; i <= right; i++) {
ans.add(matrix[top][i]);
}
top++;
for (int i = top; i <= bottom; i++) {
ans.add(matrix[i][right]);
}
right--;
if (top <= bottom) {
for (int i = right; i >= left; i--) {
ans.add(matrix[bottom][i]);
}
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--) {
ans.add(matrix[i][left]);
}
left++;
}
helper(matrix, top, left, bottom, right);
}
}