Skip to content

Commit 2fb7ee5

Browse files
python: add problem 121 and unittest
1 parent 4ee9fb3 commit 2fb7ee5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
from typing import List
3+
4+
5+
class Solution_121_130(object):
6+
def maxProfit(self, prices: List[int]) -> int:
7+
"""
8+
121
9+
:param prices:
10+
:return:
11+
"""
12+
if len(prices) <= 1:
13+
return 0
14+
max_profit = prices[1] - prices[0]
15+
lowest_profit = prices[0]
16+
for price in prices[1:]:
17+
if price < lowest_profit:
18+
lowest_profit = price
19+
if price - lowest_profit > max_profit:
20+
max_profit = price-lowest_profit
21+
return max_profit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
from unittest import TestCase
3+
from sln_101_200.solution_121_130 import Solution_121_130
4+
5+
class Test_Solution_121_130(TestCase):
6+
def setUp(self) -> None:
7+
self.sln = Solution_121_130()
8+
9+
def test_maxProfit(self):
10+
a = [7,1,5,3,6,4]
11+
max_val = self.sln.maxProfit(a)
12+
self.assertEqual(max_val, 5)

0 commit comments

Comments
 (0)