File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments