728x90
leetcode.com/problems/container-with-most-water/
아주아주 오랜만에 알고리즘 문제를 풀어 봤는데 재밌다...알타쿠의 길...
오랜만에 푸는 만큼 간단한 문제로 손풀기!
문제는 저작권 때문에 링크에서 참고 확인해주시기 바랍니다 ㅠ
간락하게 문제를 설명하자면 아래와 같습니다.
- 음이아닌 정수가 배열의 형태로 주어지고 이것은 각각 좌표(i, ai)의 점을 나타냅니다.
두 개의 수직선이 (a, ai) 와 (i, 0)에 그려집니다. 이 선들은 x 축과 함께 컨테이너를 이루고 있으며 물을 가장 많이 담을수 있는 두 개의 선을 찾으세요.
(단, 선을 기울일수 없습니다.)
n == height.length
2 <= n <= 3 *10^4
0 <= height[i] <= 3*10^4
풀이:
간단합니다. 그냥 다 그려보면 됩니다....
class Solution {
public int cmp(int a, int b){
if( a < b ) return a;
else return b;
}
public int maxArea(int[] height) {
int ans = 0;
for(int i = 0; i < height.length; i++){
for(int j = i+1; j < height.length; j++){
int maxSize = cmp(height[i], height[j]) * (j-i);
if(ans < maxSize) ans = maxSize;
}
}
return ans;
}
}
public class practice {
public static void main(String[] args){
int height[] = {1,3,2,5,25,24,5};
Solution solution = new Solution();
int num = solution.maxArea(height);
System.out.println(num);
}
}
728x90
'Algorithm Study > SW역량테스트' 카테고리의 다른 글
LRU 캐시 알고리즘 구현하기 (0) | 2023.02.22 |
---|---|
퀵 정렬(Quick Sort) 구현하기 (0) | 2020.08.17 |
큰 자릿수 뺄셈 (고급) (0) | 2020.07.03 |
압축된 문자열 풀기! (0) | 2020.07.02 |
합병 정렬(merge sort) (0) | 2020.06.28 |
댓글