내 학점 (C++)
[C++] Container With Most Water
z_zen
2025. 12. 9. 18:26
728x90
문제 - LeetCode 11. Container With Most Water
접근 흐름
- i = 0, j = n - 1 로 시작
- 현재 너비 (j - i)와 높이 min(height[i], height[j])로 면적 계산
- 최대값 갱신
- 더 낮은 쪽 포인터를 한 칸 이동
- i < j 동안 반복
코드
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0;
int j = height.size() - 1;
int maxArea = 0;
while (i < j) {
int h = min(height[i], height[j]);
int width = j - i;
maxArea = max(maxArea, h * width);
// 더 낮은 쪽을 이동
if (height[i] < height[j])
i++;
else
j--;
}
return maxArea;
}
};728x90