Algorithm ๐ง๐ป๐ป/Leetcode
[Leetcode,c++] Container With Most Water
dkswnkk
2021. 11. 14. 18:08
๋ฌธ์
Container With Most Water - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
์ฝ๋
class Solution {
public:
int maxArea(vector<int>& height) {
int start = 0;
int end = height.size()-1;
int ans = -1;
while(start<=end){
int x = end-start;
int y = min(height[start],height[end]);
ans = max(ans,x*y);
if(height[start]<height[end]) start++;
else end--;
}
return ans;
}
};
ํ์ด
ํฌ ํฌ์ธํฐ๋ฅผ ํ์ฉํ์ฌ ์๊ฐ ๋ณต์ก๋ O(N)๋ก ํด๊ฒฐํ ํ์ด์ ๋๋ค.
๋ฐฐ์ด์ ์ฒ์๊ณผ ๋์ ๋น๊ตํ๋๋ฐ ๋์ค ์์ ์ชฝ์ ๋ค์ ์ธ๋ฑ์ค๋ก ๋๊ธฐ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ต๋๋ค.