From fabb21c6ee1d2e28c2108160610a668d7aea7836 Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Thu, 4 Dec 2025 08:22:38 +0600 Subject: [PATCH] LeetCode 11: Container with most water --- .../medium/container_with_most_water_11.py | 32 +++++++++++++++ tests/test_leetcode.py | 40 +++++++------------ 2 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 LeetCode/medium/container_with_most_water_11.py diff --git a/LeetCode/medium/container_with_most_water_11.py b/LeetCode/medium/container_with_most_water_11.py new file mode 100644 index 0000000..9cd2fb2 --- /dev/null +++ b/LeetCode/medium/container_with_most_water_11.py @@ -0,0 +1,32 @@ +# LeetCode 11. Container With Most Water +from typing import List + + +class Solution: + def maxArea(self, height: List[int]) -> int: + """ + Pseudocode: + The problem is we dont know the max area where we we can store most of the water + So we can store from the start to end of the container but if the starting height of container is 1 then + max we can store height of 1 as more then that will spile the water regardless the end height + so if len is 9 and height is 1 then container can contain Area = length × width + + So, what we can do it use two pointer solution to find the max area + - initialize two pointer left and right, left with value of index 0 and + right index len(arr)-1 and res which contain most largest value + - we will continue until our left and right point cross their path + - we will be moving our smallest pointer as we know smallest length contain less water + - each time we will check which one is largest previous area which stored in res + - at the end we return the res as res will be containing the largest area value we got + """ + res = 0 + left, right = 0, len(height) - 1 + while left < right: + area = (right - left) * min(height[left], height[right]) + res = max(res, area) + + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return res diff --git a/tests/test_leetcode.py b/tests/test_leetcode.py index 66698d2..8ec6cb1 100644 --- a/tests/test_leetcode.py +++ b/tests/test_leetcode.py @@ -469,28 +469,18 @@ def test_min_number_operations(target, expected): assert minOperations(target) == expected -# @pytest.mark.performance -# def test_bank_performance(): -# from LeetCode.medium.simple_bank_system_2043 import Bank - -# n = 100_000 -# bank = Bank([1000] * n) # 100k accounts, each with balance 1000 -# operations = 200_000 - -# start = time.perf_counter() - -# for _ in range(operations): -# op_type = random.choice(["deposit", "withdraw", "transfer"]) -# a1 = random.randint(1, n) -# money = random.randint(1, 100) - -# if op_type == "deposit": -# bank.deposit(a1, money) -# elif op_type == "withdraw": -# bank.withdraw(a1, money) -# else: # transfer -# a2 = random.randint(1, n) -# bank.transfer(a1, a2, money) - -# end = time.perf_counter() -# print(f"\nTotal time for {operations} ops on {n} accounts: {end - start:.3f}s") +# LeetCode 11: Container with most water +@pytest.mark.parametrize( + "height, expected", + [ + ([1, 8, 6, 2, 5, 4, 8, 3, 7], 49), + ([1, 1], 1), + ([4, 3, 2, 1, 4], 16), + ([1, 2, 1], 2), + ], +) +def test_max_area(height, expected): + from LeetCode.medium.container_with_most_water_11 import Solution + + solution = Solution() + assert solution.maxArea(height) == expected