From 719f113544ec39a47d2bcf67f4179007643d42ec Mon Sep 17 00:00:00 2001 From: tanvipandhre <37935899+tanvipandhre@users.noreply.github.com> Date: Tue, 25 Nov 2025 22:54:41 -0500 Subject: [PATCH 1/2] Enhance problem-solving strategies in README Added approaches and considerations for solving problems in README. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index ae287b93..2e022128 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,10 @@ Note: Please solve it without division and in O(n). Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.) +* 1st approach - use 2 loops O(n^2) where if(i!=j) res[]*=nums[j] +* 2nd approach = var rp=1, take left product array product of element in left side of nums[i], right product array product of element in right side of nums[i]. +* Take product of left array & right array. + ## Problem 2 Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. @@ -33,6 +37,10 @@ Input: Output: [1,2,4,7,5,3,6,8,9] +* 2 directions in traversal given by boolean flag +* do the edge condition where direction is changing + + ## Problem 3 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. @@ -64,3 +72,7 @@ Input: ] Output: [1,2,3,4,8,12,11,10,9,5,6,7] + +* we need to consider the elements in Spiral. don't repeat the elements. +* 4 directions - 4 pointers - top, bottom, left, right. +* form loop around how is the movement. From 21e0457c34f4192622bd7d922c885628e9b90de1 Mon Sep 17 00:00:00 2001 From: tanvipandhre <37935899+tanvipandhre@users.noreply.github.com> Date: Tue, 25 Nov 2025 22:58:37 -0500 Subject: [PATCH 2/2] Day11 - Diagonal-traverse, Product-of-array-except-self, Spiral-matrix --- Diagonal-traverse.java | 39 +++++++++++++++++++++++ Product-of-array-except-self.java | 29 ++++++++++++++++++ Spiral-matrix.java | 51 +++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 Diagonal-traverse.java create mode 100644 Product-of-array-except-self.java create mode 100644 Spiral-matrix.java diff --git a/Diagonal-traverse.java b/Diagonal-traverse.java new file mode 100644 index 00000000..07569995 --- /dev/null +++ b/Diagonal-traverse.java @@ -0,0 +1,39 @@ +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[] res = new int[m*n]; + boolean flag=true; + int r=0; + int c=0; + for(int i=0;i=0;i--){ + right[i]=nums[i+1]*rp; + rp = right[i]; + } + + //result + for(int i=0;i spiralOrder(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int top=0, bottom=m-1, left=0, right=n-1; + List list = new ArrayList<>(); + while(top<=bottom && left<=right){ + //top + + for(int i=left;i<=right;i++){ + list.add(matrix[top][i]); + + } + top++; + + + + //right + if(top<=bottom && left<=right){ + for(int i=top;i<=bottom;i++){ + list.add(matrix[i][right]); + + } + right--; + } + + + //bottom + if(top<=bottom && left<=right){ + for(int i=right;i>=left;i--){ + list.add(matrix[bottom][i]); + + } + bottom--; + } + + + //left + if(top<=bottom && left<=right){ + for(int i=bottom;i>=top;i--){ + list.add(matrix[i][left]); + + } + left++; + } + + + } + return list; + } +} \ No newline at end of file