Skip to content
Open

Array-1 #1910

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
51 changes: 51 additions & 0 deletions DiagonalTraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 498. Diagonal Traverse
// https://leetcode.com/problems/diagonal-traverse/description/

/**
* Time Complexity : O(n * m) where n = row, m = cols. Since we store each element in resultant array
* Space Complexity : O(1) since no extra linear space and answer array is not considered as auxillary space
*/

class Solution {
public int[] findDiagonalOrder(int[][] mat) {
int n = mat.length;
int m = mat[0].length;

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

boolean dir = true;

int row = 0;
int col = 0;

for(int i = 0 ; i < n * m ; i++){
ans[i] = mat[row][col];

if(dir){ // if upward
if(row == 0 && col != m - 1){ // if upper boundary and not right boundary
dir = false; // change direction
col++;
}else if(col == m - 1){ // if right boundary
dir = false; // change direction
row++;
}else{
row--;
col++;
}
}else{ // if downward
if(col == 0 && row != n - 1){ // if left boundary and not bottom boudnary
dir = true; // change direction
row++;
}else if(row == n - 1){ // if bottom boundary
dir = true; // change direction
col++;
}else{
row++;
col--;
}
}
}

return ans;
}
}
58 changes: 58 additions & 0 deletions ProductOfArrayExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 238. Product of Array Except Self
// https://leetcode.com/problems/product-of-array-except-self/description/

// Solution 1 => Brute force

/**
* Time Complexity : O(n^2) since for every element we traverse entire array again
* Space Complexity : O(1) since no extra linear space and answer array is not considered as auxillary space
*/

class Solution {
public int[] productExceptSelf(int[] nums) {

int ans[] = 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];
}
}

ans[i] = prod;
}

return ans;
}
}

// Solution 2 => Prefix & Suffix

/**
* Time Complexity : O(n) since for every element
* Space Complexity : O(1) since no extra linear space and answer array is not considered as auxillary space
*/

class Solution {
public int[] productExceptSelf(int[] nums) {

int [] ans = new int[nums.length];

ans[0] = 1;

int suffix = 1; // store suffix product

for(int i = 1; i < nums.length ; i++){ // prefix
ans[i] = ans[i - 1] * nums[i - 1];
}

for(int j = nums.length - 2 ; j >= 0 ; j--){ // suffix
suffix = suffix * nums[j + 1];
ans[j] = ans[j] * suffix;
}

return ans;
}
}
75 changes: 75 additions & 0 deletions SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 54. Spiral Matrix
// https://leetcode.com/problems/spiral-matrix/description/

/**
* Time Complexity : O(n * m) where n = row, m = cols. Since we store each element in resultant array
* Space Complexity : O(1) since no extra linear space and answer array is not considered as auxillary space
*/

class Solution {

public List<Integer> spiralOrder(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;

// variables to track rows and cols
int rowS = 0;
int rowE = n - 1;
int colS = 0;
int colE = m - 1;

String dir = "right";

List<Integer> ans = new ArrayList();

while (rowS <= rowE && colS <= colE) {
if (dir.equals("right")) {
int c = colS;

while (c <= colE) {
ans.add(matrix[rowS][c]);
c++;
}

dir = "down";

rowS++;
} else if (dir.equals("down")) {
int r = rowS;

while (r <= rowE) {
ans.add(matrix[r][colE]);
r++;
}

dir = "left";

colE--;
} else if (dir.equals("left")) {
int c = colE;

while (c >= colS) {
ans.add(matrix[rowE][c]);
c--;
}

dir = "up";

rowE--;
} else if (dir.equals("up")) {
int r = rowE;

while (r >= rowS) {
ans.add(matrix[r][colS]);
r--;
}

dir = "right";

colS++;
}
}

return ans;
}
}