Skip to content

Commit ee7ddcf

Browse files
committed
leetcode.com 121. Best Time to Buy and Sell Stock
문제 링크: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
1 parent 252a6ae commit ee7ddcf

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
answer = 0
7+
buy = prices[0]
8+
9+
for price in prices[1:]:
10+
if buy >= price:
11+
buy = price
12+
else:
13+
answer = max(answer, price - buy)
14+
15+
return answer
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
5+
class TestSolution(TestCase):
6+
def test_max_profit1(self):
7+
sln = Solution()
8+
self.assertEqual(sln.maxProfit([7,1,5,3,6,4]), 5)
9+
10+
def test_max_profit2(self):
11+
sln = Solution()
12+
self.assertEqual(sln.maxProfit([7,6,4,3,1]), 0)

0 commit comments

Comments
 (0)