-
Notifications
You must be signed in to change notification settings - Fork 1
prac: valid parentheses mapping solution reasoning #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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}") | ||||||||||||||||||||
| print(f"Best profit: {sol}") | ||||||||||||||||||||
|
Comment on lines
+79
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Module-level side effects execute on import, polluting test output
Guard behind -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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring typo: profit update formula is inverted
The thought process says
best_profit = min_price - price, but the correct formula isprice - min_price(as correctly implemented at Line 74).📝 Fix
🤖 Prompt for AI Agents