From d060d96221c7b429538d124ff70307ec2ee578f3 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 6 May 2025 04:20:16 -0600 Subject: [PATCH] adding solution --- .../best_time_to_sell_stock.py | 19 +++++++++++++++++++ .../test_best_time_to_sell_stock_round_16.py | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_16/best_time_to_sell_stock.py create mode 100644 tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py diff --git a/src/my_project/interviews/top_150_questions_round_16/best_time_to_sell_stock.py b/src/my_project/interviews/top_150_questions_round_16/best_time_to_sell_stock.py new file mode 100644 index 00000000..0adbeb98 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_16/best_time_to_sell_stock.py @@ -0,0 +1,19 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +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 diff --git a/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py b/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py new file mode 100644 index 00000000..ddb38189 --- /dev/null +++ b/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py @@ -0,0 +1,3 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_16\ +.best_time_to_sell_stock import Solution \ No newline at end of file