-1
class Solution:
    def trap(self, height: List[int]) -> int:
        n = len(height)
        mxl = []
        mxr = []
        mxl.append(height[0])
        for i in range(1,n):
            mxl[i] = max(mxl[i-1],height[i])

Line 8 gives Index error. Is it wrong way to assign value in a list at particular index?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

2 Answers2

0

Fix:

class Solution:
    def trap(self, height: list[int]) -> int:
        n = len(height)
        mxl = []
        mxr = []
        for item in height:
            mxl.append(item)
        for i in range(1, n):
            mxl[i] = max(mxl[i - 1], height[i])
0

By the way, np.maximum.accumulate is the fastest (and reliable) way of doing what you are trying to achieve here

mxl = np.maximum.accumulate(height)
Learning is a mess
  • 7,479
  • 7
  • 35
  • 71