<p>Given <code>n</code> non-negative integers representing an elevation map where the width of each bar is <code>1</code>, compute how much water it can trap after raining.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png" style="width: 412px; height: 161px;" />
<pre>
<strong>Input:</strong> height = [0,1,0,2,1,0,1,3,2,1,2,1]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> height = [4,2,0,3,2,5]
<strong>Output:</strong> 9
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == height.length</code></li>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= height[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def findHighest(self, s: int, e:int):         
        mh = 0
        lh = 0
        rh = 0
        mhidx = -1
        lhidx = -1
        rhidx = -1
        # rlow = 0
        # rlowidx = -1
        risDown=True
        for i in range(s, e):
            if mh < self.height[i]:
                if mhidx != -1:
                    lhidx = mhidx
                    lh = mh
                mh = self.height[i]
                mhidx = i 
                rhidx = -1
                rh = 0
                risDown = True
            elif mhidx != -1:
                if rh <= self.height[i]:
                    rh = self.height[i]
                    rhidx = i
                    lastv = self.height[i]
                    risDown=True
                else:
                    if lastv > self.height[i]:
                        lastv = self.height[i]
                    elif lastv < self.height[i]:
                        risDown=False


                
        
        kv = {'mh': mh,
        'mhidx': mhidx,
        'lh': lh,
        'lhidx' : lhidx,
        'rh': rh,
        'rhidx' : rhidx,
        'risDown':risDown}
       # print(kv)
        return kv
            



    def calckv(self, kv) -> int:
        if kv['mhidx'] == -1:
            return 0
        v = 0
        if kv['lhidx'] != -1:
            ch = min(kv['mh'], kv['lh'])
            for i in range(kv['lhidx']+1, kv['mhidx']):
                v += (ch-self.height[i])
        if kv['rhidx'] != -1:
            ch = min(kv['mh'], kv['rh'])
            for i in range(kv['mhidx']+1, kv['rhidx']):
                v += (ch-self.height[i])
        return v

    def gotrap(self, s:int, e:int) -> int:
        kv = self.findHighest(s, e)
        print(kv)
        v = self.calckv(kv)
        if kv['lhidx'] != -1:
            ns = s
            ne = kv['lhidx']+1
            # llst = lst[:kv['lhidx']+1]
            #print(llst)
            if ns < ne:
                v += self.gotrap(ns, ne)
        if kv['rhidx'] != -1 and not kv['risDown']:
            ns = kv['rhidx']
            ne = e
            # rlst = lst[kv['rhidx']:]
            #print(rlst)
            if ns< ne :
                v += self.gotrap(ns, ne)
        return v

    def fill(self, rside: int, lside: int) -> int:
        v = 0
        mh = min(self.height[rside], self.height[lside])
        for i in range(rside+1, lside):
            if mh > self.height[i]:
                v+= (mh - self.height[i])
            # v += mh - self.height[i]
                self.height[i] = mh
        return v
    
    def rain(self) -> int:
        idx = self.llimit
        ridx = 0   
        obv = self.height[0]
        w = 0     
        while idx < self.rlimit:
            
            bv = self.height[idx]
            if obv == bv:
                idx += 1
                continue
            obv = bv
            
            rside = -1
            for i in range(idx+1, self.ilen):
                nv = self.height[i]
                if bv < nv:
                    rside = i
                    bv = nv
                elif bv > nv:
                    break
            if rside == -1:
                idx += 1
                continue
            bv = self.height[idx]
            lside = -1
            for i in range(idx-1, ridx-1, -1):
                nv = self.height[i]
                if bv < nv:
                    lside = i
                    bv = nv
                elif bv > nv:
                    break
            if lside == -1:
                idx += 1
                continue
                
            w += self.fill(lside, rside)
                # print ('w', w)
            ridx = rside
            idx = rside + 1
                
        return w




        

    def trap(self, height: List[int]) -> int:
        self.height  = height
        self.ilen = len(height)
        if self.ilen <= 2: 
            return 0
        self.llimit = 1
        self.rlimit = self.ilen -1
        w = 0 
        # return self.rain()
        while True:
            nw = self.rain()
            if nw == 0:
                break
            else:
                w += nw
        return w

        # return self.gotrap(0, self.ilen)
        


                
18000