From e61cdcdba85da1613c5ef6d80f0dd42978badb51 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Fri, 16 Jan 2026 20:11:43 -0600 Subject: [PATCH] two pointer 2 --- merge.js | 36 ++++++++++++++++++++++++++++++++++++ searchMatrix.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 merge.js create mode 100644 searchMatrix.js diff --git a/merge.js b/merge.js new file mode 100644 index 00000000..00b16537 --- /dev/null +++ b/merge.js @@ -0,0 +1,36 @@ +/** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. +https://leetcode.com/problems/merge-sorted-array/ +**Intuition:** +The goal is to merge two sorted arrays into `nums1` without using extra space, so we take advantage of the empty slots at the end of `nums1`. +By starting from the last valid elements of both arrays and comparing them, we always place the larger value at the end of `nums1`, which prevents overwriting elements that haven’t been processed yet. + +We continue this process until one array is exhausted; if `nums2` still has elements left, we copy them over, while any remaining elements in `nums1` are already in the correct position. + +T.C: O(n), S.C: O(1) + */ +var merge = function(nums1, m, nums2, n) { + let pointer1 = m-1, pointer2 = n-1, movePointer = m+n-1; + while(pointer1 >= 0 && pointer2 >= 0){ + if(nums1[pointer1] > nums2[pointer2]){ + nums1[movePointer] = nums1[pointer1]; + pointer1--; + movePointer--; + }else{ + nums1[movePointer] = nums2[pointer2]; + pointer2--; + movePointer--; + } + } + if(pointer1 < 0){ + while(pointer2 >= 0){ + nums1[movePointer] = nums2[pointer2]; + pointer2--; + movePointer--; + } + } +}; \ No newline at end of file diff --git a/searchMatrix.js b/searchMatrix.js new file mode 100644 index 00000000..4ce1a4ee --- /dev/null +++ b/searchMatrix.js @@ -0,0 +1,39 @@ +/** + * @param {number[][]} matrix + * @param {number} target + * @return {boolean} + +**Intuition:** + +The matrix is sorted in increasing order both row-wise and column-wise, so starting from the top-right corner gives a useful comparison point. +At this position, if the current value is equal to the target, we can return true. + +If the target is larger, we can safely move down because all values to the left are smaller; if the target is smaller, we move left because all values below are larger. By eliminating one row or one column at each step, we efficiently narrow the search space until the target is found or the indices go out of bounds. + +If in this process our pointer are out of the matrix perimeter then we return false; + +T.C: O(m+n) +S.C: O(1) + + + */ +var searchMatrix = function(matrix, target) { + const rowLength = matrix.length, colLength = matrix[0].length; + let currRow = 0, currCol = colLength - 1; + // currRow >= 0 && currCol >= 0 && currRow < rowLength && currCol < colLength + // above consition can be simplified into the below while condition + while(currCol >= 0 && currRow < rowLength){ + const currVal = matrix[currRow][currCol]; + if(currVal === target){ + return true; + } + if(target > currVal){ + currRow = currRow + 1; + }else{ + currCol = currCol - 1; + } + } + + return false; + +}; \ No newline at end of file