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
94 changes: 94 additions & 0 deletions DiagonalMatrixTraversal1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* url: https://leetcode.com/problems/diagonal-traverse/
* 498. Diagonal Traverse
* Time Complexity: O(m*n)
* Space Complexity: O(m*n)
*/
public class DiagonalMatrixTraversal1 {

public int[] DiagonalMatrixTraversal(int[][] matrix) {

int n = matrix[0].length; // column
int m = matrix.length; // row

if (m == 0 || n == 0) {
return new int[0];
}
if (m == 1) {
return matrix[0];
}
if (n == 1) {
int[] result = new int[m];
for (int k = 0; k < m; k++) {
result[k] = matrix[k][0];
}
return result;
}

int[] result = new int[m * n];

String direction = "down";
int i = 0;
int j = 0;
int count = -1;
while (i < m && j < n) {
if ((i == 0 && j == 0) || (i == m - 1 && j == n - 1)) { // first and last
result[++count] = matrix[i][j];
j++;
}
if (direction.equals("down") && i >= 0 && j < n && i < m && j >= 0) {
result[++count] = matrix[i][j];
if (j == 0 || i == m - 1) {
if(i < m - 1) { // traverse down if we are not at the last row, else traverse right j
i++;
} else {
j++;
}
direction = "up";
continue;
}
i++;
j--;
}

if (direction.equals("up") && i < m && j >= 0 && i >= 0 && j < n) {
result[++count] = matrix[i][j];
if (i == 0 || j == n - 1) { // traverse right if we are not at the last column, else traverse down i
if (j < n - 1) {
j++;
} else {
i++;
}

direction = "down";
continue;
}
i--;
j++;
}

}

return result;
}

public static void main(String[] args) {
int[] result = new DiagonalMatrixTraversal1().DiagonalMatrixTraversal(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

result = new DiagonalMatrixTraversal1().DiagonalMatrixTraversal(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

result = new DiagonalMatrixTraversal1().DiagonalMatrixTraversal(new int[][]{{1, 2}, {4, 5}, {7, 8}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
}
}
78 changes: 78 additions & 0 deletions MatrixSpiralTraversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* url: https://leetcode.com/problems/spiral-matrix/
* 54. Spiral Matrix
* Time Complexity: O(m*n)
* Space Complexity: O(m*n)
*/
public class MatrixSpiralTraversal {

public int[] spiralTraversal(int[][] matrix) {
int n = matrix[0].length; // column
int m = matrix.length; // row

int[] result = new int[m * n];

int i = 0, j = 0;
int count = -1;
while (result.length - 1 > count) {
// top
while (j < n && matrix[i][j] != 101) {
result[++count] = matrix[i][j];
matrix[i][j] = 101;
j++;
}

// right
j--; // reset j to the last valid column
i++;
while (i < m && matrix[i][j] != 101) {
result[++count] = matrix[i][j];
matrix[i][j] = 101;
i++;
}
// bottom
j--;
i--; // reset i to the last valid row
while (j >= 0 && matrix[i][j] != 101) {
result[++count] = matrix[i][j];
matrix[i][j] = 101;
j--;
}

// left
i--;
j++; // reset j to the last valid column
while (i >= 0 && matrix[i][j] != 101) {
result[++count] = matrix[i][j];
matrix[i][j] = 101;
i--;
}
i++;
j++;

}
return result;

}

public static void main(String[] args) {
int[] result = new MatrixSpiralTraversal().spiralTraversal(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

result = new MatrixSpiralTraversal().spiralTraversal(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

result = new MatrixSpiralTraversal().spiralTraversal(new int[][]{{1, 2}, {4, 5}, {7, 8}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
}

}
81 changes: 81 additions & 0 deletions MatrixSpiralTraversal1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import java.util.ArrayList;
import java.util.List;
/**
* url: https://leetcode.com/problems/spiral-matrix/
* 54. Spiral Matrix
* Time Complexity: O(m*n)
* Space Complexity: O(m*n)
* Uses list
*/
public class MatrixSpiralTraversal1 {

public List<Integer> spiralTraversal(int[][] matrix) {
int n = matrix[0].length; // column
int m = matrix.length; // row

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

int i = 0, j = 0;
while (list.size() < m * n) {
// top
while (j < n && matrix[i][j] != 101) {
list.add(matrix[i][j]);
matrix[i][j] = 101;
j++;
}

// right
j--; // reset j to the last valid column
i++;
while (i < m && matrix[i][j] != 101) {
list.add(matrix[i][j]);
matrix[i][j] = 101;
i++;
}
// bottom
j--;
i--; // reset i to the last valid row
while (j >= 0 && matrix[i][j] != 101) {
list.add(matrix[i][j]);
matrix[i][j] = 101;
j--;
}

// left
i--;
j++; // reset j to the last valid column
while (i >= 0 && matrix[i][j] != 101) {
list.add(matrix[i][j]);
matrix[i][j] = 101;
i--;
}
i++;
j++;

}

return list;

}

public static void main(String[] args) {
List<Integer> result = new MatrixSpiralTraversal1().spiralTraversal(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

result = new MatrixSpiralTraversal1().spiralTraversal(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

result = new MatrixSpiralTraversal1().spiralTraversal(new int[][]{{1, 2}, {4, 5}, {7, 8}});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
}

}
40 changes: 40 additions & 0 deletions ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

/**
* 238. Product of Array Except Self
* Time Complexity: O(n^2)
* Space Complexity: O(n)
*/
public class ProductOfArrayExceptSelf {

public int[] productExceptSelf(int[] nums) {
int[] result = new int[nums.length];

for (int i = 0; i < nums.length; i++) {
int product = 1;
for (int j = 0; j < nums.length; j++) {
// for any i, calculate all j's except i

if (i == j) {
continue;
}
product = product * nums[j];
}
result[i] = product;
}
return result;
}

public static void main(String[] args) {
int[] result = new ProductOfArrayExceptSelf().productExceptSelf(new int[]{1, 2, 3, 4, 5});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
result = new ProductOfArrayExceptSelf().productExceptSelf(new int[]{-1, 1, 0, -3, 3});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

}
}
44 changes: 44 additions & 0 deletions ProductOfArrayExceptSelf1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 238. Product of Array Except Self
* Time Complexity: O(n)
* Space Complexity: O(1) if we don't consider the output array, otherwise O(n)
*/
public class ProductOfArrayExceptSelf1 {

public int[] productExceptSelf(int[] nums) {
int n = nums.length - 1;
int[] result = new int[nums.length];
// LPS
int product = 1;
for (int i = 0; i <= n; i++) {
if (i != 0) {
product = product * nums[i - 1];
}
result[i] = product;
}
// RPS
product = 1;
for (int i = n; i >= 0; i--) {
if (i != n) {
product = product * nums[i + 1];
}
result[i] = result[i] * product;
}

return result;
}

public static void main(String[] args) {
int[] result = new ProductOfArrayExceptSelf1().productExceptSelf(new int[]{1, 2, 3, 4, 5});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");
result = new ProductOfArrayExceptSelf1().productExceptSelf(new int[]{-1, 1, 0, -3, 3});
for (int r : result) {
System.out.print(r + " ");
}
System.out.println("");

}
}