利用双指针优化了动态规划问题,优化了空间复杂度o(n)->o(1)

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# max water
# @param arr int整型一维数组 the array
# @return long长整型
#
class Solution:
    def maxWater(self , arr: List[int]) -> int:
        # write code here
        if not arr:
            return 0
        n=len(arr)
        left,right=0,n-1
        leftmax=rightmax=0
        res=0
        while left<right:
            leftmax=max(arr[left],leftmax)
            rightmax=max(arr[right],rightmax)
            if arr[left]<arr[right]:
                res+=leftmax-arr[left]
                left+=1
            else:
                res+=rightmax-arr[right]
                right-=1
        return res