LC100-11. 盛最多水的容器

题目

题目要求

暴力(超时)


class Solution {
    public int maxArea(int[] height) {

        // 可以正常运行,但是提交会产生超时的
        int max = 0;
        int start = 0;
        int end = height.length;
        for(int i = 0; i < height.length; i ++) {
            for (int j = height.length -1; j > i; j--) {
                int dis = j - i;
                int temp = 0;
                temp = (height[i]> height[j] ? height[j]:height[i]) * dis;
                if (temp > max) {
                    max = temp;
                }
            }
        }
        return max;
    }
}

使用双指针(O(n))复杂度

class Solution {
    public int maxArea(int[] height) {

/*对O(n)的算法写一下自己的理解,一开始两个指针一个指向开头一个指向结尾,此时容器的底是最大的,接下来随着指针向内移动,会造成容器的底变小,在这种情况下想要让容器盛水变多,就只有在容器的高上下功夫。 那我们该如何决策哪个指针移动呢?我们能够发现不管是左指针向右移动一位,还是右指针向左移动一位,容器的底都是一样的,都比原来减少了 1。这种情况下我们想要让指针移动后的容器面积增大,就要使移动后的容器的高尽量大,所以我们选择指针所指的高较小的那个指针进行移动,这样我们就保留了容器较高的那条边,放弃了较小的那条边,以获得有更高的边的机会。
*/
       int size =height.length;
       int left = 0, right = size - 1;
       int result = 0;
       while (left < right) {
        int temp = (right - left) * (height[right] > height[left] ? height[left] : height[right]);
           if (temp > result) {
               result = temp;
           }
           if(height[left] > height[right]){
               right--;
           } else {
               left++;
           }
       }
       return result;
    }
}

### 使用双指针跳过方式,进一步提高效率
```java
class Solution {
    // 使用快速跳过的方法进行跳过
    public int maxArea(int[] height) {
         int max = 0;
        int l =0, r = height.length - 1;
        while (l < r) {
            int min = Math.min(height[l], height[r]);

            int temp = min * (r - l);

            max = Math.max(max, temp);

            // 快速跳过 
            // 跳过原理,无论left向左移动还是向右移动,容器的底部都会缩小,根据短板效应,如果移动后的“木板”小于之前
            // 的木板,这种情况就没有必要再进行一次计算,直接向一方移动即可
            while (height[l] <= min && l < r) {
                ++l;
            }

            while (height[r] <= min && l < r) {
                --r;
            }
        }
        return max;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure! Here's your code with comments added: ```matlab F = zeros(length(z), 1); % Initialize the F vector with zeros for i = 1:length(z) % Define the Phi function using anonymous function Phi = @(theta, R, r) (z(i) + lc - lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) + lc - lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) - lc + lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) - lc + lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) + lc + lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.*sin(theta)).sqrt(R.^2 + r.^2 + (z(i) + lc + lm).^2 - 2*R.*r.*sin(theta))) + ... (z(i) - lc - lm) .* r.R .(R - r.sin(theta)) ./ ... ((R.^2 + r.^2 - 2*R.*r.sin(theta)).sqrt(R.^2 + r.^2 + (z(i) - lc - lm).^2 - 2*R.*r.sin(theta))); % Calculate the value of F(i) using the integral3 function F(i) = BrNI / (4 * lc * (Rc - rc)) * integral3(Phi, 0, 2*pi, rc, Rc, rm, Rm); end ``` This code calculates the values of the vector `F` using a loop. The `Phi` function is defined as an anonymous function that takes `theta`, `R`, and `r` as input parameters. It performs a series of calculations and returns a value. The integral of `Phi` is then calculated using the `integral3` function. The result is stored in the corresponding element of the `F` vector. Please note that I have made some assumptions about the variables and functions used in your code since I don't have the complete context. Feel free to modify or clarify anything as needed.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值