diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..2c7abad4 --- /dev/null +++ b/problem1.java @@ -0,0 +1,33 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) (output array not counted) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just needed to do prefix and suffix product without using division. + + +// Your code here along with comments explaining your approach in three sentences only +// First I store prefix product in the output array so output[i] has product of all elements before i. +// Then I traverse from right side and keep a suffix product and multiply it into output[i]. +// This gives product except self in O(n) and constant extra space. + +class Solution { + public int[] productExceptSelf(int[] nums) { + + int n = nums.length; + int[] res = new int[n]; + + // prefix product + res[0] = 1; + for (int i = 1; i < n; i++) { + res[i] = res[i - 1] * nums[i - 1]; + } + + // suffix product and multiply + int suffix = 1; + for (int i = n - 1; i >= 0; i--) { + res[i] = res[i] * suffix; + suffix = suffix * nums[i]; + } + + return res; + } +} diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..a20ed1fa --- /dev/null +++ b/problem2.java @@ -0,0 +1,57 @@ +// Time Complexity : O(m*n) +// Space Complexity : O(1) (only output array used) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Only tricky part is direction change and boundary handling. + + +// Your code here along with comments explaining your approach in three sentences only +// I move diagonally up-right and down-left alternatively using direction flag. +// Whenever I hit a boundary, I shift to the next valid cell (right or down) and flip direction. +// I continue until I collect all m*n elements. + +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + + int m = mat.length; + int n = mat[0].length; + + int[] ans = new int[m * n]; + int r = 0, c = 0; + int dir = 1; // 1 means up-right, -1 means down-left + int idx = 0; + + while (idx < m * n) { + + ans[idx++] = mat[r][c]; + + // moving up-right + if (dir == 1) { + if (c == n - 1) { // hit right wall + r++; + dir = -1; + } else if (r == 0) { // hit top wall + c++; + dir = -1; + } else { + r--; + c++; + } + } + // moving down-left + else { + if (r == m - 1) { // hit bottom wall + c++; + dir = 1; + } else if (c == 0) { // hit left wall + r++; + dir = 1; + } else { + r++; + c--; + } + } + } + + return ans; + } +} \ No newline at end of file diff --git a/problem3.java b/problem3.java new file mode 100644 index 00000000..20e3eb58 --- /dev/null +++ b/problem3.java @@ -0,0 +1,58 @@ +// Time Complexity : O(m*n) +// Space Complexity : O(1) (excluding output list) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No, just need to update boundaries properly to avoid duplicates. + + +// Your code here along with comments explaining your approach in three sentences only +// I maintain four boundaries (top, bottom, left, right) and keep shrinking them after each traversal. +// I go in order: left to right, top to bottom, right to left, bottom to top while checking boundaries. +// This continues until all elements are added into the list. + +import java.util.*; + +class Solution { + public List spiralOrder(int[][] matrix) { + + List li = new ArrayList<>(); + + int m = matrix.length; + int n = matrix[0].length; + + int top = 0, bottom = m - 1; + int left = 0, right = n - 1; + + while (top <= bottom && left <= right) { + + // top row: left -> right + for (int i = left; i <= right; i++) { + li.add(matrix[top][i]); + } + top++; + + // right col: top -> bottom + for (int i = top; i <= bottom; i++) { + li.add(matrix[i][right]); + } + right--; + + // bottom row: right -> left + if (top <= bottom) { + for (int i = right; i >= left; i--) { + li.add(matrix[bottom][i]); + } + bottom--; + } + + // left col: bottom -> top + if (left <= right) { + for (int i = bottom; i >= top; i--) { + li.add(matrix[i][left]); + } + left++; + } + } + + return li; + } +}