Skip to content

Commit 253cca3

Browse files
authored
Merge pull request #1439 from ivan1016017/october29
adding algo
2 parents b15e1a4 + fb1c23c commit 253cca3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import math
4+
5+
class Solution:
6+
def maxProfit(self, prices: List[int]) -> int:
7+
8+
min_price = math.inf
9+
max_profit = 0
10+
len_prices = len(prices)
11+
12+
for i in range(len_prices):
13+
14+
if prices[i] < min_price:
15+
min_price = prices[i]
16+
elif prices[i] - min_price > max_profit:
17+
max_profit = prices[i] - min_price
18+
19+
return max_profit
20+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.best_time_to_buy_stock import Solution
4+
5+
class BestTimeToSellStockTestCase(unittest.TestCase):
6+
7+
def test_best_time_to_sell_stock(self):
8+
solution = Solution()
9+
output = solution.maxProfit(prices=[7,1,5,3,6,4])
10+
target = 5
11+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)