
-Code
class Solution {
public int maxArea(int[] height) {
int left = 0, right = height.length - 1;
int answer = 0;
// 왼쪽이 오른쪽보다 작을 경우에만 반복
while (left < right) {
// 둘중 낮은 높이 구하기
int minHeight = Math.min(height[left], height[right]);
// 가로 넓기 구하기
int with = right - left;
// 가로, 세로 곱과 현재 저장 중인 값 중 큰 값 저장
answer = Math.max(answer, with * minHeight);
// left가 높으면 right - 1 반대면 left - 1
if (height[left] > height[right]) {
right--;
} else {
left++;
}
}
return answer;
}
}'Beakjoon&프로그래머스 > Java' 카테고리의 다른 글
| [프로그래머스/Java] 타겟 넘버 (0) | 2026.01.01 |
|---|---|
| [LeetCode/Java] Max Number of K-Sum Pairs (0) | 2026.01.01 |
| [프로그래머스/Java] n^2 배열 자르기 (0) | 2026.01.01 |
| [LeetCode/Java] String Compression (0) | 2026.01.01 |
| [LeetCode/Java] Increasing Triplet Subsequence (0) | 2026.01.01 |