๐ป Coding Problems Solving/Two Pointers | Binary Search| LinkedList
[LeetCode] Container With Most Water
Kim_dev
2023. 3. 28. 23:07
1. ๋ฌธ์ : https://leetcode.com/problems/container-with-most-water/
๋ง๋ ๋์ด๋ฅผ input์ผ๋ก ๋ฐ์์๋ ์ต๋๋ก ๋ด์์ ์๋ ๋ฌผ์ ์์ ๊ตฌํ๋ ๋ฌธ์
2. ํ์ด
ํฌํฌ์ธํฐ๋ก while๋ฌธ ๋๋ฉด์ ๋ง๋ ์ฎ๊ฒจ๊ฐ๋ฉฐ ์ต๋๊ฐ updateํ๋ฉฐ ํด๊ฒฐ
3. ์ฝ๋
class Solution {
public int maxArea(int[] height) {
int a=0;
int b=height.length-1;
int maxVal = Integer.MIN_VALUE;
while(b != a){
if(height[a]>height[b]){
maxVal = Math.max(maxVal,(b-a)*height[b]);
b--;
}else{
maxVal = Math.max(maxVal,(b-a)*height[a]);
a++;
}
}
return maxVal;
}
}