Skip to content
Open

Array1 #1978

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
57 changes: 57 additions & 0 deletions DiagonalTraverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the matrix
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :Yes
// Three line explanation of solution in plain english
// 1. We traverse the matrix in a diagonal manner, starting from the top-left corner.
// 2. We use a boolean variable to keep track of the direction of traversal (up-right or down-left).
// 3. We update the row and column indices based on the current direction
// and the boundaries of the matrix. We store the elements in the result array as we traverse.
// The auxiliary space is O(1) as we are using only a few variables to keep track of the current position and direction,
// and the result array is not considered auxiliary space as it is required for the output.

// Your code here along with comments explaining your approac

class Solution {
public int[] findDiagonalOrder(int[][] mat) {

boolean direction = true; // true = up-right, false = down-left

int row = mat.length;
int col = mat[0].length;

int[] result = new int[row * col];

int r = 0, c = 0;

for (int i = 0; i < row * col; i++) {

result[i] = mat[r][c];

if (direction) {
if (c == col - 1) {
r++;
direction = false;
} else if (r == 0) {
c++;
direction = false;
} else {
r--;
c++;
}
} else {
if (r == row - 1) {
c++;
direction = true;
} else if (c == 0) {
r++;
direction = true;
} else {
r++;
c--;
}
}
}

return result;
}
}
37 changes: 37 additions & 0 deletions ProductofArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

// Time Complexity : O(N)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode :Yes
// Three line explanation of solution in plain english
// 1. Calculate the product of all elements to the left of each element
// 2. Calculate the product of all elements to the right of each element
// 3. Multiply the left and right products for each element
// Product of all no's of element to left * element
// Product of all no's of element to right * element
// Auxuliary space is not used as we are storing the left product in the result array and using a variable to store the right product.

// Your code here along with comments explaining your approach
class Solution {
public int[] productExceptSelf(int[] nums) {

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

int rightProduct = 1;
result[0] = 1;

// Left products
for (int i = 1; i < nums.length; i++) {
rightProduct = rightProduct * nums[i - 1];
result[i] = rightProduct;
}

// Right products
rightProduct = 1;
for (int i = nums.length - 2; i >= 0; i--) {
rightProduct = rightProduct * nums[i + 1];
result[i] = result[i] * rightProduct;
}

return result;
}
}
57 changes: 57 additions & 0 deletions SprialMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity : O(n*m) where n is the number of rows and m is the number of columns in the matrix
// Did this code successfully run on Leetcode :Yes
// Three line explanation of solution in plain english
// 1. We maintain four pointers to keep track of the current boundaries of the matrix (rowBegin, rowEnd, colBegin, colEnd).
// 2. We traverse the matrix in a spiral manner, moving right, down, left, and up,
// while updating the boundaries after each traversal.
// 3. We continue this process until the pointers cross each other, indicating that we have traversed the entire matrix.
// We store the elements in the result list as we traverse.
// The auxiliary space is O(1) as we are using only a few variables to keep track of the current position and boundaries, and the result list is not considered auxiliary space as it is required for the output.

// Your code here along with comments explaining your approac
import java.util.*;

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {

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

int rowBegin = 0;
int colBegin = 0;
int rowEnd = matrix.length - 1;
int colEnd = matrix[0].length - 1;

while (rowBegin <= rowEnd && colBegin <= colEnd) {

// move right
for (int j = colBegin; j <= colEnd; j++) {
res.add(matrix[rowBegin][j]);
}
rowBegin++;

// move down
for (int j = rowBegin; j <= rowEnd; j++) {
res.add(matrix[j][colEnd]);
}
colEnd--;

// move left
if (rowBegin <= rowEnd) {
for (int j = colEnd; j >= colBegin; j--) {
res.add(matrix[rowEnd][j]);
}
rowEnd--;
}

// move up
if (colBegin <= colEnd) {
for (int j = rowEnd; j >= rowBegin; j--) {
res.add(matrix[j][colBegin]);
}
colBegin++;
}
}

return res;
}
}