Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
theabdallahnjr authored Jan 12, 2025
1 parent 8e53312 commit 43bb752
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions solutions/testtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Created on 12.01.2025
@author: Mushtary Alam
"""


def trap(height):
"""
Calculates the amount of rainwater that can be trapped given an elevation map.
Parameters:
height (list[int]): A list of non-negative integers representing the elevation map, where the width of each bar is 1.
Returns:
int: The total amount of trapped rainwater.
Logic:
- Uses two pointers (left and right) to traverse the elevation map.
- Tracks the maximum height to the left and right of each bar.
- Accumulates water trapped based on the minimum of these maximum heights.
Examples:
>>> trap([0,1,0,2,1,0,1,3,2,1,2,1])
6
>>> trap([4,2,0,3,2,5])
9
>>> trap([1,0,2,1,0,1,3,2,1,2,1])
6
"""

# Defensive assertions for input validation
assert isinstance(height, list), "Input must be a list."
assert all(isinstance(h, int) and h >= 0 for h in height), (
"All elements must be non-negative integers."
)

# If there are fewer than 3 elements, no water can be trapped.
if not height or len(height) < 3:
return 0

# Initialize two pointers and variables to track the max heights
left, right = 0, len(height) - 1
left_max, right_max = height[left], height[right]
water = 0

# Traverse the list with two pointers to calculate trapped water
while left < right:
if height[left] < height[right]:
# Move the left pointer and update the left_max
left += 1
left_max = max(left_max, height[left])
# Calculate the trapped water at the current left position
water += max(0, left_max - height[left])
else:
# Move the right pointer and update the right_max
right -= 1
right_max = max(right_max, height[right])
# Calculate the trapped water at the current right position
water += max(0, right_max - height[right])

return water

0 comments on commit 43bb752

Please sign in to comment.