반응형
1. 문제
https://leetcode.com/problems/container-with-most-water/
2. 코드
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let n = height.length;
let left = 0, right = n - 1;
let max_area = 0;
while (left < right) {
let area = Math.min(height[left], height[right]) * (right - left);
max_area = Math.max(max_area, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return max_area;
}
투포인터, DP 얘네 둘 언제 잘 풀게 될까..
비슷한 문제)
https://leetcode.com/problems/trapping-rain-water/description/
반응형
'리트코드 > midium' 카테고리의 다른 글
[리트코드] 300. Longest Increasing Subsequence - js (DP) (0) | 2023.08.31 |
---|---|
[리트코드] 994. Rotting Oranges - js (BFS) (0) | 2023.08.30 |
[리트코드] 72. Edit Distance - js (DP) (1) | 2023.08.27 |
[리트코드] 1143. Longest Common Subsequence - js (DP) (0) | 2023.08.25 |
[리트코드] 200. Number of Islands - js (DFS) (0) | 2023.08.24 |