11. Container With Most Water (JavaScript)
Source
https://leetcode.com/problems/container-with-most-water/
Container With Most Water - LeetCode
Can you solve this real interview question? Container With Most Water - You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that toget
leetcode.com
Code
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let maxWater = 0;
let left = 0;
let right = height.length - 1;
while (left <= right) {
maxWater = Math.max(
maxWater,
(right - left) * Math.min(height[left], height[right])
);
if (height[left] >= height[right]) {
right -= 1;
} else {
left += 1;
}
}
return maxWater;
};
How to solve?
투포인터를 설정해서 문제를 해결한다.
height[left]와 height[right]를 비교하여 값이 더 작은 쪽을 옮겨준다.
'Problem solve' 카테고리의 다른 글
[Leetcode] 26. Remove Duplicates from Sorted Array (0) | 2023.05.16 |
---|---|
[Leetcode] 48. Rotate Image (Python) (0) | 2023.05.15 |
[Leetcode] 28. Find the Index of the First Occurrence in a String (JavaScript) (0) | 2023.05.06 |
[LeetCode] 35. Search Insert Position (Javascript) (0) | 2023.04.24 |
[Leetcode] 14. Longest Common Prefix (0) | 2023.04.18 |