Trapping Rain Water O(n^2) time limit exceeded
168

According to: https://leetcode.com/articles/trapping-rain-water/ ... I wrote the following solution but the link says that this solution is accepted but it gives me time lmit exceeded

class Solution:
    def trap(self, height):
	"""
	:type height: List[int]
	:rtype: int
	"""
	ans = 0
	for i in range(1, len(height)):

		max_left = 0
		max_right = 0

		for j in range(i):
				max_left = max(max_left, height[j])

		for j in range(i+1, len(height)):
				max_right = max(max_right, height[j])

		ans = ans + max(min(max_right, max_left)-height[i], 0)

	return ans
Comments (0)