Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 821 Bytes

11.container-with-most-water.md

File metadata and controls

28 lines (23 loc) · 821 Bytes

题目地址

https://leetcode-cn.com/problems/container-with-most-water/

思路

遍历

  1. 简单粗暴,记录最大值,直接遍历2词数组即可,然后就超出时间,gg。

双指针

  1. 左右各一个指针,双指针为边界,双指针之间的距离*最小的指针 = 最小容积
  2. 将较小的指针向着对方的方向移动一位。

复杂度

  1. 时间复杂度:O(N) 遍历一遍数组
  2. 空间复杂度:O(1) 额外维护一个最大值
from typing import List


class Solution:
    def canJump(self, nums: List[int]) -> bool:
        n, rightmost = len(nums), 0
        for i in range(n):
            if rightmost >= i:
                rightmost = max(nums[i]+i, rightmost)
                if rightmost >= n-1:
                    return True
        return False