Skip to content

Commit 12a0147

Browse files
committed
feat: if/else 문 사용
1 parent 467eb00 commit 12a0147

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
buy_at = p
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

Comments
 (0)