Skip to content

Commit

Permalink
feat(Array): Container with most water
Browse files Browse the repository at this point in the history
  • Loading branch information
xlreon committed Nov 12, 2023
1 parent 0831103 commit e57c247
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ var twoSum = function (nums, target) {
};
```

### Container With Most Water - Medium

https://leetcode.com/problems/container-with-most-water/

> take left and right pointer, check min height between both and multiply by distance between left and right and store it to max area, check which height is smaller and accordingly update left + 1 and right - 1
```javascript
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let left = 0;
let right = height.length - 1;
let maxima = 0;
while (left < right) {
if (height[left] <= height[right]) {
let area = height[left] * (right - left);
maxima = Math.max(maxima, area);
left++;
} else {
let area = height[right] * (right - left);
maxima = Math.max(maxima, area);
right--;
}
}
return maxima;
};
```

### Min in Rotated Sorted Array - Medium

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
Expand Down
21 changes: 21 additions & 0 deletions array/container-with-most-water.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let left = 0;
let right = height.length - 1;
let maxima = 0;
while (left < right) {
if (height[left] <= height[right]) {
let area = height[left] * (right - left);
maxima = Math.max(maxima, area);
left++;
} else {
let area = height[right] * (right - left);
maxima = Math.max(maxima, area);
right--;
}
}
return maxima;
};

0 comments on commit e57c247

Please sign in to comment.