diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_07_best_time_to_sell_stocks_i.py b/src/my_project/interviews/top_150_questions_round_23/ex_07_best_time_to_sell_stocks_i.py new file mode 100644 index 00000000..c863b07d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/ex_07_best_time_to_sell_stocks_i.py @@ -0,0 +1,19 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC +import math + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + min_price = math.inf + max_profit = 0 + len_prices = len(prices) + + for i in range(len_prices): + + if prices[i] < min_price: + min_price = prices[i] + elif prices[i] - min_price > max_profit: + max_profit = prices[i] - min_price + + return max_profit \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_23/ex_07_best_time_to_sell_stocks_i.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_07_best_time_to_sell_stocks_i.ts new file mode 100644 index 00000000..b031d140 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_07_best_time_to_sell_stocks_i.ts @@ -0,0 +1,17 @@ +function maxProfit(prices: number[]): number { + + let minValuePrice: number = 10**6 + 1 + let maxProfit: number = 0 + + for (let i = 0; i < prices.length; i++){ + if (prices[i] < minValuePrice){ + minValuePrice = prices[i] + } + else if (prices[i] - minValuePrice > maxProfit){ + maxProfit = prices[i] - minValuePrice + } + } + + return maxProfit + +} \ No newline at end of file diff --git a/tests/test_150_questions_round_23/test_07_best_time_to_sell_stocks_i_round_23.py b/tests/test_150_questions_round_23/test_07_best_time_to_sell_stocks_i_round_23.py new file mode 100644 index 00000000..5e12a06c --- /dev/null +++ b/tests/test_150_questions_round_23/test_07_best_time_to_sell_stocks_i_round_23.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.ex_07_best_time_to_sell_stocks_i import Solution + +class BestTimeToSellStockTestCase(unittest.TestCase): + + def test_best_time_to_sell_stock(self): + solution = Solution() + output = solution.maxProfit(prices=[7,1,5,3,6,4]) + target = 5 + self.assertEqual(output, target) \ No newline at end of file