๋ฌธ์
์ฝ๋
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)๋ก ํด๊ฒฐํ ํ์ด์ ๋๋ค.
๋ฐฐ์ด์ ์ฒ์๊ณผ ๋์ ๋น๊ตํ๋๋ฐ ๋์ค ์์ ์ชฝ์ ๋ค์ ์ธ๋ฑ์ค๋ก ๋๊ธฐ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode,c++] Group Anagrams (0) | 2021.11.14 |
---|---|
[Leetcode,c++] Next Permutation (0) | 2021.11.14 |
[Leetcode,c++] Length of Last Word (0) | 2021.11.14 |
[Leetcode,c++] Remove Duplicates from Sorted Array (0) | 2021.11.14 |
[Leetcode,c++] Longest Common Prefix (0) | 2021.11.11 |
[Leetcode,c++] Permutations (0) | 2021.11.10 |
๋๊ธ