题目:原题链接(中等)

标签:数组

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( V K ) O(VK) O(VK) O ( 1 ) O(1) O(1) 76ms (92.86%)
Ans 2 (Python)      
Ans 3 (Python)      

解法一(暴力):

class Solution:
    def pourWater(self, heights: List[int], v: int, k: int) -> List[int]:
        for _ in range(v):
            left = k
            for i in range(k - 1, -1, -1):
                if heights[i] < heights[i + 1]:
                    left = i
                elif heights[i] > heights[i + 1]:
                    break
            if left < k:
                heights[left] += 1
                continue

            right = k
            for i in range(k + 1, len(heights)):
                if heights[i] < heights[i - 1]:
                    right = i
                elif heights[i] > heights[i - 1]:
                    break
            if right > k:
                heights[right] += 1
                continue

            heights[k] += 1

        return heights