classSolution(object): deftrap(self, height): """ :type height: List[int] :rtype: int """ max_lefts = [] cur_max = 0 for n in height: cur_max = max(cur_max, n) max_lefts.append(cur_max) cur_max = 0 x = 0 for i in xrange(len(height) - 1, -1, -1): n = height[i] cur_max = max(cur_max, n) x += min(cur_max, max_lefts[i]) - n return x
classSolution(object): deftrap(self, height): """ :type height: List[int] :rtype: int """ low, high = 0, len(height) - 1 s = 0 while low < high: if height[low] < height[high]: k = low + 1 while height[k] <= height[low]: s += height[low] - height[k] k += 1 low = k else: k = high - 1 while height[k] < height[high]: s += height[high] - height[k] k -= 1 high = k return s