Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Docstring typo: profit update formula is inverted

The thought process says best_profit = min_price - price, but the correct formula is price - min_price (as correctly implemented at Line 74).

📝 Fix
-        if price - min_price > best_profit best_profit = min_price - price
+        if price - min_price > best_profit: best_profit = price - min_price
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@LeetCode/dump/best_time_buy_sell_stock_121.py` at line 32, The profit update
assigns the inverted expression; in the loop that compares price, min_price, and
best_profit (variables price, min_price, best_profit inside the solution
function, e.g., maxProfit), change the assignment so best_profit is set to price
- min_price (not min_price - price) when if price - min_price > best_profit;
ensure the conditional and assignment are properly separated/terminated so the
check updates best_profit correctly.


update
Day 0: price is 7 and min_price is 7 best_profit is 0
update
Expand All @@ -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}")
print(f"Best profit: {sol}")
Comment on lines +79 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Module-level side effects execute on import, polluting test output

Solution().maxProfit(price_3) and print(...) run unconditionally at module scope. Now that test_leetcode_easy.py (Line 88) imports from LeetCode.dump.best_time_buy_sell_stock_121 import Solution, every test run will execute this computation and emit stdout, dirtying pytest output.

Guard behind __main__:

-price_3 = [7, 6, 4, 3, 1]
-sol = Solution().maxProfit(price_3)
-print(f"Best profit: {sol}")
+if __name__ == "__main__":
+    price_3 = [7, 6, 4, 3, 1]
+    sol = Solution().maxProfit(price_3)
+    print(f"Best profit: {sol}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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}")
if __name__ == "__main__":
price_3 = [7, 6, 4, 3, 1]
sol = Solution().maxProfit(price_3)
print(f"Best profit: {sol}")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@LeetCode/dump/best_time_buy_sell_stock_121.py` around lines 79 - 81, The
module currently runs Solution().maxProfit(price_3) and prints the result at
import time, causing side effects when tests import Solution; move those example
invocation lines (the price_3 assignment, the call to Solution().maxProfit, and
the print) into a guard block so they only run when executed as a script: wrap
them in if __name__ == "__main__": or remove them entirely so importing the
Solution class and its maxProfit method has no stdout or computation side
effects.

12 changes: 12 additions & 0 deletions LeetCode/easy/valid_parentheses_20.py
Original file line number Diff line number Diff line change
@@ -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
23 changes: 0 additions & 23 deletions tests/test_leetcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
30 changes: 29 additions & 1 deletion tests/test_leetcode_easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading