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;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ