Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions merge.js
Original file line number Diff line number Diff line change
@@ -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--;
}
}
};
39 changes: 39 additions & 0 deletions searchMatrix.js
Original file line number Diff line number Diff line change
@@ -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;

};