From 631fcfca1f3fb34e00678a4a19ea81fbd1f1dd65 Mon Sep 17 00:00:00 2001 From: rvuyyuru7 Date: Sat, 29 Nov 2025 19:01:31 -0600 Subject: [PATCH] Arrays-1 complete --- DiagnolTraverse.java | 46 +++++++++++++++++++++++++++++++++ ProductOfArrayExceptSelf.java | 22 ++++++++++++++++ SpiralMatrix.java | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 DiagnolTraverse.java create mode 100644 ProductOfArrayExceptSelf.java create mode 100644 SpiralMatrix.java diff --git a/DiagnolTraverse.java b/DiagnolTraverse.java new file mode 100644 index 00000000..5e79b100 --- /dev/null +++ b/DiagnolTraverse.java @@ -0,0 +1,46 @@ +// Time: O(rows * columns) +// Space: O(1) +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int rows = mat.length; + int columns = mat[0].length; + boolean dir = true; // true: upwards, false: downwards. + int[] result = new int[rows * columns]; + int index = 0; // for result + // Start at 0,0 + int row = 0; + int column = 0; + for (int i = 0; i < rows * columns; i ++) { // traverse through entire matrix + // Add value at current row and column to the result and increment index. + result[index ++] = mat[row][column]; + if (dir) { + // moving up + if (column == columns - 1) { + dir = false; // move down + row += 1; + } else if (row == 0) { + dir = false; // move down + column += 1; + } else { + // move to next diagonal index in upward direction + row --; + column ++; + } + } else { + // moving down + if (row == rows - 1) { + dir = true; // move up + column += 1; + } else if (column == 0) { + dir = true; // move up + row += 1; + } else { + // move to next diagonal index in downward direction + row ++; + column --; + } + } + } + return result; + } +} \ No newline at end of file diff --git a/ProductOfArrayExceptSelf.java b/ProductOfArrayExceptSelf.java new file mode 100644 index 00000000..942faee4 --- /dev/null +++ b/ProductOfArrayExceptSelf.java @@ -0,0 +1,22 @@ +// Approach: Running product (Two-pass solution) +// Time: N + N ~= O(N) +// Space: O(1) +class Solution { + public int[] productExceptSelf(int[] nums) { + int runningProduct = 1; + int[] result = new int[nums.length]; + result[0] = runningProduct; + // left + 1 to right + for (int i = 1; i < nums.length; i ++) { + runningProduct *= nums[i - 1]; + result[i] = runningProduct; // assign running product + } + runningProduct = 1; + // right - 1 to left + for (int i = nums.length - 2; i >= 0; i --) { + runningProduct *= nums[i + 1]; + result[i] *= runningProduct; // multiply with running product + } + return result; + } +} \ No newline at end of file diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..dcbb3378 --- /dev/null +++ b/SpiralMatrix.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.List; + +// Time: O(rows * columns) +// Space: O(1) +class Solution { + public List spiralOrder(int[][] matrix) { + int rows = matrix.length; + int columns = matrix[0].length; + List result = new ArrayList<>(); + // Initialize directions + int top = 0; + int bottom = rows - 1; + int left = 0; + int right = columns - 1; + while (top <= bottom && left <= right) { + if (top <= bottom && left <= right) { + // move right + for (int i = left; i <= right; i ++) { + result.add(matrix[top][i]); + } + top ++; + } + if (top <= bottom && left <= right) { + // move down + for (int i = top; i <= bottom; i ++) { + result.add(matrix[i][right]); + } + right --; + } + if (top <= bottom && left <= right) { + // move left + for (int i = right; i >= left; i --) { + result.add(matrix[bottom][i]); + } + bottom --; + } + if (top <= bottom && left <= right) { + // move up + for (int i = bottom; i >= top; i --) { + result.add(matrix[i][left]); + } + left ++; + } + } + return result; + } +} \ No newline at end of file