Skip to content

Commit 4489662

Browse files
committed
adding best time to sell algo
1 parent 3ae68ae commit 4489662

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_13\
3+
.best_time_to_sell_stock import Solution
4+
5+
6+
class BestTimeToSellStockTestCase(unittest.TestCase):
7+
8+
def test_best_time_to_sell_stock(self):
9+
solution = Solution()
10+
output = solution.maxProfit(prices=[7,1,5,3,6,4])
11+
target = 5
12+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)