Skip to content

Commit fd4fc29

Browse files
committed
leetcode.com 122. Best Time to Buy and Sell Stock II
문제 링크: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
1 parent ee7ddcf commit fd4fc29

File tree

2 files changed

+31
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)