Skip to content

Commit 1c39def

Browse files
Added Trapping Rain Water Solution (#35)
1 parent e601f6f commit 1c39def

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Python/trapping_rain.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def trap(self, height: List[int]) -> int:
3+
water=0
4+
left=-1
5+
right=len(height)
6+
current=0
7+
arr1=[]
8+
arr2=[]
9+
left_max=0
10+
right_max=0
11+
for current in range(len(height)):
12+
while(left!=current):
13+
left+=1
14+
if(left_max<=height[left]):
15+
left_max=height[left]
16+
arr1.append(left_max)
17+
while(right!=current):
18+
right-=1
19+
20+
if(right_max<=height[right]):
21+
right_max=height[right]
22+
arr2.append(right_max)
23+
right_max=0
24+
25+
right=len(height)
26+
left=-1
27+
28+
29+
for i in range(len(arr1)):
30+
s=min(arr1[i],arr2[i])-height[i];
31+
if(s>0):
32+
water+=s
33+
34+
return water

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
9292
| 122 | [Best Time to buy and sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [Python](./Python/best-time-to-buy-and-sell-stock-ii.py) | _O(N)_ | _O(1)_ | Medium | Stocks | |
9393
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [Python](./Python/pascals-triangle-ii.py) | _O(N^2)_ | _O(K)_ | Easy | | |
9494
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum | |
95+
| 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./Python/trapping_rain.py) | _O(N^2)_ | _O(N)_ | Hard | Array | |
9596

9697

9798
<br/>
@@ -326,7 +327,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
326327
| [Rudra Mishra](https://github.com/Rudra407) <br> <img src="https://github.com/Rudra407.png" width="100" height="100"> | India | C++ | [Twitter](https://twitter.com/ruDra_Mishra407)<br>[Leetcode](https://leetcode.com/rudramishra/) |
327328
| [Sachin Singh Negi](https://github.com/sachinnegi) <br> <img src="https://github.com/sachinnegi.png" width="100" height="100"> | India | Python | [Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688) |
328329
| [Girish Thatte](https://github.com/girishgr8/) <br> <img src="https://github.com/girishgr8.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/girish13/) <br> [Hackerrank](https://www.hackerrank.com/procoder_13) <br> [Codechef](https://www.codechef.com/procoder_13) |
329-
330+
| [Kevin Chittilapilly](https://github.com/KevinChittilapilly) <br> <img src="https://github.com/KevinChittilapilly.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/being_kevin/) <br> [Hackerrank](https://www.hackerrank.com/ckevinvarghese11) <br> [Kaggle](https://www.kaggle.com/kevinchittilapilly) |
330331
<br/>
331332
<div align="right">
332333
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)