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
22 changes: 22 additions & 0 deletions maxArea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[]} height
* @return {number}
* This solution uses a two-pointer approach to find the maximum water container efficiently.
* One pointer starts at the beginning of the array (left) and the other at the end (right).
* The area formed by the two lines is calculated by multiplying the distance between the pointers with the minimum of their heights, since the shorter line limits the amount of water that can be held.
* After computing the area, we compare it with the current maximum and update the maximum if needed.
* To potentially find a larger area, we always move the pointer pointing to the smaller height, because moving the taller line cannot increase the area when the width decreases, while moving the shorter line may lead to a taller boundary.
* This process continues until the two pointers meet, ensuring all optimal pairs are considered in linear time.
*/
var maxArea = function (height) {
let left = 0, right = height.length - 1;
let max = 0;

while (left < right) {
const capacity = Math.min(height[left], height[right]) * (right - left);
if (max < capacity) max = capacity;
if (height[left] <= height[right]) left++;
else right--;
}
return max;
};
37 changes: 37 additions & 0 deletions sortColors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
* We use three pointers to sort the three colors:
* a `left` pointer to place `0`s at the beginning,
* a `mid` pointer to scan the array, and
* a `right` pointer to place `2`s at the end.
* Both `left` and `mid` start at index `0`, while `right` starts at `nums.length - 1`.
* As `mid` scans the array, if it encounters a `0`, we swap it with the value at `left` and increment both `left` and `mid` since that position is now correctly placed.
* If `mid` encounters a `1`, it is already in the correct middle region, so we simply increment `mid`.
* If `mid` encounters a `2`, we swap it with the value at `right` and decrement `right`, but we do not increment `mid` because the value swapped in from the right is still unprocessed.
* We repeat this process until `mid` moves past `right`, at which point all elements are sorted in-place.
*
* T.C: O(n), S.C: O(1)
*/
var sortColors = function(nums) {
// left to catch 0's, mid to catch 1's and right to catch 2's
let left =0; mid = 0, right = nums.length - 1;

while(mid <= right){
if(nums[mid]===2){
// swap mid and right pointer values.
[nums[mid], nums[right]] = [nums[right], nums[mid]];
// decerement the right pointer
right--;
}else if(nums[mid] === 0){
// swap mid and left pointer values.
[nums[mid], nums[left]] = [nums[left], nums[mid]];
// increment both left and right pointers
left++, mid++;
}else{
// if mid points to value 1
// just increment the mid pointer
mid++;
}
}
};
58 changes: 58 additions & 0 deletions threeSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @param {number[]} nums
* @return {number[][]}
*
* Intuition:
To find all unique triplets that sum to zero efficiently, I first sort the array.
Sorting helps in two important ways:
it allows the use of the two-pointer technique, and
it makes it easy to skip duplicate values to avoid repeated triplets.
I iterate through the array and fix one number at a time. For each fixed number nums[i], I convert the problem into finding two numbers in the remaining part of the array whose sum equals -nums[i].

Using two pointers:
I place one pointer at the element just after i (left)
Another pointer at the end of the array (right)
At each step:
If nums[left] + nums[right] equals the target, I record the triplet and move both pointers inward.
If the sum is smaller than the target, I move the left pointer to increase the sum.
If the sum is larger, I move the right pointer to decrease the sum.
To ensure unique triplets, I skip over duplicate values for:
The fixed element (i)
The left pointer
The right pointer
Since the array is sorted, once the fixed number becomes greater than zero, no valid triplet can exist, so I stop early.
This approach reduces the time complexity from O(n³) to O(n²) while using constant extra space (excluding the output).
*
*
*/
var threeSum = function (nums) {
// sort nums
nums.sort((a, b) => a - b);
const result = [];
for (let i = 0; i < nums.length - 2; i++) {
const currNum = nums[i];
// if nums[i] > 0 then as the array is sorted there is no way we find negatives after this number;
if (currNum > 0) break;
// outer loop duplicates reduction
if (i > 0 && currNum === nums[i - 1]) continue;
const target = 0 - currNum;
let left = i + 1; right = nums.length - 1;
while (left < right) {
const leftAndRightSum = nums[left] + nums[right];
if (leftAndRightSum === target) {
result.push([currNum, nums[left], nums[right]]);
left++;
right--;
// skip if the curr num is same as previous ones on both left and right
while (left < right && nums[left] === nums[left - 1]) left++;
while (left < right && nums[right] === nums[right + 1]) right--;
} else if (leftAndRightSum < target) {
left++;
} else {
right--;
}
}
}

return result;
};