From ce2f61c7719bf994327ecf0cc7325667731f14dc Mon Sep 17 00:00:00 2001 From: wazedkhan Date: Sun, 22 Feb 2026 23:39:14 +0600 Subject: [PATCH] prac: valid parentheses mapping solution reasoning --- .../best_time_buy_sell_stock_121.py | 22 +++++++------- LeetCode/easy/valid_parentheses_20.py | 12 ++++++++ tests/test_leetcode.py | 23 -------------- tests/test_leetcode_easy.py | 30 ++++++++++++++++++- 4 files changed, 51 insertions(+), 36 deletions(-) rename LeetCode/{easy => dump}/best_time_buy_sell_stock_121.py (94%) create mode 100644 LeetCode/easy/valid_parentheses_20.py diff --git a/LeetCode/easy/best_time_buy_sell_stock_121.py b/LeetCode/dump/best_time_buy_sell_stock_121.py similarity index 94% rename from LeetCode/easy/best_time_buy_sell_stock_121.py rename to LeetCode/dump/best_time_buy_sell_stock_121.py index 38a091d..69773a5 100644 --- a/LeetCode/easy/best_time_buy_sell_stock_121.py +++ b/LeetCode/dump/best_time_buy_sell_stock_121.py @@ -3,7 +3,7 @@ class Solution: """ - + price = [7,6,4,3,1] price = [7,1,5,3,6,4] price_3 = [2,7,1,5,4] @@ -30,7 +30,7 @@ class Solution: update() if min_price > price then min_price = price if price - min_price > best_profit best_profit = min_price - price - + update Day 0: price is 7 and min_price is 7 best_profit is 0 update @@ -51,33 +51,31 @@ class Solution: Boom! At the end, we return that """ + def bruteForceMaxProfit(self, prices: List[int]) -> int: best_profit = 0 for i in range(len(prices)): for j in range(i + 1, len(prices)): profit = prices[j] - prices[i] - + best_profit = max(best_profit, profit) - + return best_profit - + def maxProfit(self, prices: List[int]) -> int: - min_price = float('inf') + min_price = float("inf") best_profit = 0 for price in prices: if price < min_price: min_price = price - + if price - min_price > best_profit: best_profit = price - min_price return best_profit - - - -price_3 = [7,6,4,3,1] +price_3 = [7, 6, 4, 3, 1] sol = Solution().maxProfit(price_3) -print(f"Best profit: {sol}") \ No newline at end of file +print(f"Best profit: {sol}") diff --git a/LeetCode/easy/valid_parentheses_20.py b/LeetCode/easy/valid_parentheses_20.py new file mode 100644 index 0000000..281f8e6 --- /dev/null +++ b/LeetCode/easy/valid_parentheses_20.py @@ -0,0 +1,12 @@ +class Solution: + def isValidByMapping(self, s: str) -> bool: + stack = [] + mapping = {"}": "{", ")": "(", "]": "["} + + for p in s: + if stack and p in mapping and stack[-1] == mapping[p]: + stack.pop() + else: + stack.append(p) + + return len(stack) == 0 diff --git a/tests/test_leetcode.py b/tests/test_leetcode.py index 8ec6cb1..09ecf04 100644 --- a/tests/test_leetcode.py +++ b/tests/test_leetcode.py @@ -248,29 +248,6 @@ def test_reverse_integer(x: int, expected: int): assert result == expected -@pytest.mark.parametrize( - "s, expected", - [ - ("()", True), # Single pair - ("()[]{}", True), # Multiple valid pairs - ("(]", False), # Mismatched parentheses - ("([)]", False), # Nested but invalid - ("{[]}", True), # Properly nested - ("", True), # Empty string - ("(", False), # Single opening parenthesis - (")", False), # Single closing parenthesis - ("(((((((((())))))))))", True), # Deeply nested - ("(((((((((()))", False), # Deeply nested but incomplete - ], -) -def test_is_valid_parentheses(s: str, expected: bool): - from LeetCode.valid_parentheses_20 import Solution - - solution = Solution() - result = solution.isValid(s) - assert result == expected - - @pytest.mark.parametrize( "s, expected", [ diff --git a/tests/test_leetcode_easy.py b/tests/test_leetcode_easy.py index df9be14..697b45f 100644 --- a/tests/test_leetcode_easy.py +++ b/tests/test_leetcode_easy.py @@ -85,7 +85,7 @@ def test_generate(numRows, expected): ], ) def test_max_profit(prices, expected): - from LeetCode.easy.best_time_buy_sell_stock_121 import Solution + from LeetCode.dump.best_time_buy_sell_stock_121 import Solution solution = Solution() assert solution.maxProfit(prices) == expected @@ -182,3 +182,31 @@ def test_missing_number(nums, expected): solution = Solution() assert solution.missingNumber(nums) == expected + + +@pytest.mark.parametrize( + "s, expected", + [ + ("()", True), # Single pair + ("()[]{}", True), # Multiple valid pairs + ("(]", False), # Mismatched parentheses + ("([)]", False), # Nested but invalid + ("{[]}", True), # Properly nested + ("", True), # Empty string + ("(", False), # Single opening parenthesis + (")", False), # Single closing parenthesis + ("(((((((((())))))))))", True), # Deeply nested + ("(((((((((()))", False), # Deeply nested but incomplete + ], +) +def test_is_valid_parentheses(s: str, expected: bool): + from LeetCode.easy.valid_parentheses_20 import Solution as Sol + from LeetCode.valid_parentheses_20 import Solution + + solution = Solution() + result = solution.isValid(s) + assert result == expected + + sol = Sol() + res = sol.isValidByMapping(s) + assert res == expected