755. Pour Water - cocoder39/coco39_LC GitHub Wiki

755. Pour Water

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