From 7070f538d47753f51773d7a50a1ff566e372be42 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 14 May 2026 04:54:16 -0600 Subject: [PATCH] adding updates --- .../ex_16_trapping_rain_water.py | 45 +++++++++++++++++++ .../ex_16_trapping_rain_water.ts | 28 ++++++++++++ .../test_16_trapping_rain_water_round_23.py | 17 +++++++ 3 files changed, 90 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_23/ex_16_trapping_rain_water.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_23/ex_16_trapping_rain_water.ts create mode 100644 tests/test_150_questions_round_23/test_16_trapping_rain_water_round_23.py diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_16_trapping_rain_water.py b/src/my_project/interviews/top_150_questions_round_23/ex_16_trapping_rain_water.py new file mode 100644 index 00000000..03530048 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/ex_16_trapping_rain_water.py @@ -0,0 +1,45 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def trap(self, height: List[int]) -> int: + """ + Calculate how much water can be trapped after raining. + + Key insight: Water trapped at position i = min(max_left, max_right) - height[i] + (if positive) + + Strategy: Two-pointer approach + - Use two pointers from both ends + - Track max height seen from left and right + - Move pointer with smaller max height (water level determined by shorter side) + - Add water trapped at current position + + Time: O(n), Space: O(1) + """ + + if not height or len(height) < 3: + return 0 + + left, right = 0, len(height) - 1 + left_max, right_max = 0, 0 + water_trapped = 0 + + while left < right: + # Update max heights seen so far + left_max = max(left_max, height[left]) + right_max = max(right_max, height[right]) + + # Water level is determined by the shorter side + if left_max < right_max: + # Left side is shorter, so water trapped at left position + # is determined by left_max + water_trapped += left_max - height[left] + left += 1 + else: + # Right side is shorter or equal, so water trapped at right position + # is determined by right_max + water_trapped += right_max - height[right] + right -= 1 + + return water_trapped diff --git a/src/my_project/interviews_typescript/top_150_questions_round_23/ex_16_trapping_rain_water.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_16_trapping_rain_water.ts new file mode 100644 index 00000000..ef73ef75 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_16_trapping_rain_water.ts @@ -0,0 +1,28 @@ +function trap(height: number[]): number { + let solution: number = 0 + let l: number = 0 + let r: number = height.length + let i: number + + while (l < r){ + i = 1 + + if (height[l] < height[r]){ + while (height[l] > height[l+i]){ + solution += height[l]-height[l+i] + i += 1 + } + l += i + } + else{ + while (height[r] > height[r-i]){ + solution += height[r] - height[r-i] + i += 1 + } + r -= i + } + } + + return solution + +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_23/test_16_trapping_rain_water_round_23.py b/tests/test_150_questions_round_23/test_16_trapping_rain_water_round_23.py new file mode 100644 index 00000000..efc7c3c5 --- /dev/null +++ b/tests/test_150_questions_round_23/test_16_trapping_rain_water_round_23.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.ex_16_trapping_rain_water import Solution + +class TrappingWaterTestCase(unittest.TestCase): + + def test_trapping_water_first_case(self): + solution = Solution() + output = solution.trap(height = [0,1,0,2,1,0,1,3,2,1,2,1]) + target = 6 + self.assertEqual(output, target) + + def test_trapping_water_second_case(self): + solution = Solution() + output = solution.trap(height = [4,2,0,3,2,5]) + target = 9 + self.assertEqual(output, target) \ No newline at end of file