盛最多水的容器
# 📃 题目描述
题目链接:11. 盛最多水的容器 (opens new window)
# 🔔 解题思路
总存水量 = 两个板子之间的最小值 * 两个板子之间的距离
那么我们是不是可以,用两个指针,一个指向开头,一个指向结尾,然后我们每次移动一个指针,这时候我们要考虑一个问题,我们要移动哪一个指针
我们每次移动指针,两个板子之间的距离都是会变小,那么我们要是尽可能的想让我们总存水量变大,那么我们就要让我们两个板子之间的最小值变大,我们移动大的那个指针只会让我们的答案不变或者变小,所以我们只有移动小的那个指针才可以达到变化的一个作用
class Solution {
public int maxArea(int[] height) {
// write code here
if (height == null || height.length == 0) {
return 0;
}
int maxArea = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
int h = Math.min(height[left], height[right]);
int w = right - left;
maxArea = Math.max(w * h, maxArea);
// 较小的柱子移动
if (height[left] >= height[right]) {
right --;
} else {
left ++;
}
}
return maxArea;
}
}
# 💥 复杂度分析
- 空间复杂度:
- 时间复杂度:
🎁 公众号

各位小伙伴大家好呀,叫我小牛肉就行,目前在读东南大学硕士,上方扫码关注公众号「飞天小牛肉」,与你分享我的成长历程与技术感悟~
帮助小牛肉改善此页面 (opens new window)
Last Updated: 2023/02/16, 11:27:10