leetcode 11. 盛最多水的容器

思路:

1. 单调栈:维护一个单调上升的栈,因为最大值只可能是与左边较大的木板围成。不过当输入为一个单调上升的数据时,此时算法变成了n^2.但似乎leetcode数据量不大,依然可以通过。

class Solution {
public:
    int maxArea(vector& height) {
        int n=height.size();
        int ans=0;
        int a[n+3],bit[n+3];
        int k=0;
        a[0]=height[0];
        bit[0]=1;
        for(int i=1;i

2. 双指针:设两个指针,分别指向最左和最右边,依次移动较小的木板即可。

class Solution {
public:
    int maxArea(vector& height) {
        int n=height.size();
        int l=0,r=n-1;
        int ans=0;
        int len=n-1;
        while(l

 

你可能感兴趣的:(LeetCode)