https://leetcode.com/problems/container-with-most-water/
class Solution {
public int maxArea(int[] height) {
int ww=0;
int templeft=0, tempright=height.length-1;
while(templeft<tempright){
int x=(tempright-templeft)*Math.min(height[templeft], height[tempright]);
if (x>ww){ ww=x;}
if (height[tempright]<height[templeft]) tempright--; else templeft++;
}
return ww;
}
}
왼쪽과 오른쪽부터 각각 인덱스 시작, 둘 중 height가 더 작은 쪽을 이동시키면서 그 전까지 가장 큰 값을 저장해뒀던 ww와 넓이 비교, 더 크면 갱신. 두 인덱스가 교차될 때까지 수행한다.
'Algorithm > Java' 카테고리의 다른 글
프로그래머스 - 주차 요금 계산 (0) | 2022.09.22 |
---|---|
프로그래머스 - 숫자의 표현 (0) | 2022.09.22 |
leetcode - Reverse Integer (1) | 2022.09.21 |
leetcode - Zigzag Conversion (1) | 2022.09.21 |
leetcode - add two numbers (0) | 2022.09.21 |