在leetcode第11题看到一个题解,添加到代码前面,大幅缩减runtime

代码为:
static int speedup=[](){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
leetcode11题解
static int speedup=[](){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution
{
public:
int maxArea(vector<int> &height)
{
int i = 0, j = height.size() - 1, water = 0;
while (i < j)
{
int h = min(height[i], height[j]);
water = max(water, (j - i) * h);
while (height[i] <= h && i < j)
i++;
while (height[j] <= h && i < j)
j--;
}
return water;
}
};
本文介绍了一个在LeetCode第11题中使用的高效解法,通过优化代码,显著减少了运行时间。该解法利用双指针技巧,避免了不必要的重复计算,实现了快速求解最大面积的问题。
1793

被折叠的 条评论
为什么被折叠?



