https://leetcode.com/problems/container-with-most-water/
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
var p1 = 0, p2 = height.length - 1;
var max = {area: 0, height: 0};
while(p1 !== p2) {
var minHeight = Math.min(height[p1], height[p2]);
var width = p2 - p1;
if (minHeight > max.height) {
var area = minHeight * width;
if (max.area < area) {
max.area = area;
max.height = minHeight;
}
}
if (minHeight === height[p1]) {
p1++;
} else {
p2--;
}
}
return max.area;
};
'알고리즘 > LeetCode' 카테고리의 다른 글
[Leetcode][Javascript][Easy] 144. Binary Tree Preorder Traversal (0) | 2021.07.05 |
---|---|
[Leetcode][Javascript][Easy] 14. Longest Common Prefix (0) | 2021.07.05 |
[Leetcode][Javascript][Easy] 9. Palindrome Number (0) | 2021.07.04 |
[Leetcode][Javascript][Medium] 7. Reverse Integer (0) | 2021.07.04 |
[Leetcode][Javascript][Medium] 2. Add Two Numbers (0) | 2021.07.02 |
댓글