diff --git a/findDiagonalOrder.js b/findDiagonalOrder.js new file mode 100644 index 00000000..468487df --- /dev/null +++ b/findDiagonalOrder.js @@ -0,0 +1,56 @@ +/** + * @param {number[][]} mat + * @return {number[]} + Intuition: + I am traversing the matrix diagonally, by tracking the boundaries and maintaining a flag to chech whether i am travelling upward or downward. + + I am updating the flag based on the boundaries i am touching, when we travel upwards, if i am hitting the 1st row but not last column i am moving col pointer to next and updating the dir to down , if i am hitting right boundary i am updating row to next and updating dir to down , otherwise row--, col++; + + When i traverse downwards, if i am hitting left boundary but not the bottom, i am setting my row to next row and updating the dir to up, or if i am hitting bottom then updating col to next column and changing the dir to up, otherwise row++, col--; + */ +var findDiagonalOrder = function (mat) { + const res = []; + const m = mat.length; + const n = mat[0].length; + let dir = 1; + let row = 0, col = 0; + for (let i = 0; i < m * n; i++) { + // direction is up + if (dir) { + // push the current val + res[i] = mat[row][col]; + // if the boundary is top but not right + if (row === 0 && col !== n - 1) { + col++; + // set direction to down + dir = 0; + } else if (col === n - 1) { + // case when we hit the right boundary + row++; + dir = 0; + } else { + // case when we are traversing up wards + row--; + col++; + } + } else { + // push the current val + res[i] = mat[row][col]; + // if the boundary is left but not bottom + if (col === 0 && row !== m - 1) { + row++; + // set direction to down + dir = 1; + } else if (row === m - 1) { + // case when we hit the bottom boundary + col++; + dir = 1; + } else { + // case when we are traversing downwards + row++; + col--; + } + } + } + return res; +}; \ No newline at end of file diff --git a/productExceptSelf.js b/productExceptSelf.js new file mode 100644 index 00000000..501d36af --- /dev/null +++ b/productExceptSelf.js @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @return {number[]} + Intuition: + + As we aren't allowed to use division operation, here i am using running product to solve this problem. + result is initialized with 1 and in the forward pass we are storing the left side product of each number. + In the backward pass, i am using a variable product to reduce another array, and it is initialized with 1, + At each location i am multiplying product with res[i-1] to get the final value and product is multiplied by + nums[j] to store the running product of right side values. + + */ +var productExceptSelf = function(nums) { + let res = [1]; + // forward pass + for(let i=1; i 0; j--){ + res[j] = product * res[j]; + product *= nums[j]; + } + res[0] = product; + return res; +}; \ No newline at end of file diff --git a/spiralOrder.js b/spiralOrder.js new file mode 100644 index 00000000..69c9e267 --- /dev/null +++ b/spiralOrder.js @@ -0,0 +1,47 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + + We treat the matrix like a set of shrinking boundaries and keep removing it layer by layer in a spiral order. + Four pointers — top, bottom, left, and right — represent the current rectangle we have not yet visited. + + In each iteration, we traverse the top row from left to right, then the right column from top to bottom, followed by the bottom row from right to left, and finally the left column from bottom to top. + + After completing one full spiral layer, we move the boundaries inward (top++, bottom--, left++, right--) to a avoid revisiting elements. The loop continues as long as the boundaries are valid, ensuring every cell is visited exactly once in spiral order. + + Time Complexity: O(m * n) + Space Complexity: O(1) + */ +var spiralOrder = function (mat) { + const res = []; + let m = mat.length; + let n = mat[0].length; + let left = 0, right = n - 1, top = 0, bottom = m - 1; + + while (left <= right && top <= bottom) { + // top row iteration + for (let i = left; i <= right; i++) { + res.push(mat[top][i]); + } + top++; + // right col iteration + for (let j = top; j <= bottom; j++) { + res.push(mat[j][right]); + } + right--; + // making sure the bottom conditions are still in place + if (top <= bottom && left <= right) { + // bottom row iteration + for (let k = right; k >= left; k--) { + res.push(mat[bottom][k]); + } + bottom--; + // left col iteration + for (let i = bottom; i >= top; i--) { + res.push(mat[i][left]); + } + left++; + } + } + return res; +}; \ No newline at end of file