We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 467eb00 commit 12a0147Copy full SHA for 12a0147
best-time-to-buy-and-sell-stock/gyeo-ri.py
@@ -0,0 +1,30 @@
1
+class Solution:
2
+ def maxProfit(self, prices: list[int]) -> int:
3
+ max_profit = 0
4
+ buy_at = None
5
+
6
+ for p in prices:
7
+ if buy_at is None:
8
+ buy_at = p
9
+ elif buy_at > p:
10
11
+ elif buy_at < p:
12
+ max_profit = max(max_profit, p - buy_at)
13
14
+ return max_profit
15
16
17
+if __name__ == "__main__":
18
+ test_cases = [
19
+ ([7, 1, 5, 3, 6, 4], 5),
20
+ ([7, 6, 4, 3, 1], 0),
21
+ ([2, 4, 1], 2),
22
+ ]
23
24
+ solution = Solution()
25
+ for idx, case_ in enumerate(test_cases):
26
+ prices, answer = case_
27
+ result = solution.maxProfit(prices)
28
+ assert (
29
+ answer == result
30
+ ), f"Test Case {idx} Failed: Expected {answer}, Got {result}"
0 commit comments