From 1770e8808057448cc75da3e01fad9ffcfb5604de Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 8 Oct 2023 19:47:49 +0530 Subject: [PATCH 1/3] to add best_time_stock program --- .../best_time_to_buy_and_sell_stock.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 greedy_methods/best_time_to_buy_and_sell_stock.py diff --git a/greedy_methods/best_time_to_buy_and_sell_stock.py b/greedy_methods/best_time_to_buy_and_sell_stock.py new file mode 100644 index 000000000000..b93ae5d7fbcd --- /dev/null +++ b/greedy_methods/best_time_to_buy_and_sell_stock.py @@ -0,0 +1,45 @@ +""" +Given a list of stock prices, +This program calculates the maximum profit that can be made from a single buy and sell. +We can only complete one transaction (buy one and sell one share of the stock). +We must buy before we can sell. + +Example : prices = [7, 1, 5, 3, 6, 4] +max_profit will return 5 - which is by buying at price 1 and selling at price 6. + +This problem can be solved using the concept of "GREEDY ALGORITHM". + +We iterate over the price array once, keeping track of the lowest price point +(buy) and the maximum profit we can get at each point. +The greedy choice at each point is to either buy at the current price if +it's less than our current buying price, or sell at the current price if the profit +is more than our current maximum profit. +""" + +def max_profit(prices : list[int]) -> int: + """ + >>> max_profit([7, 1, 5, 3, 6, 4]) + 5 + >>> max_profit([7, 6, 4, 3, 1]) + 0 + """ + if not prices: + return 0 + + min_price = prices[0] + max_profit : int = 0 + + for price in prices: + if price < min_price: + min_price = price + elif price - min_price > max_profit: + max_profit = price - min_price + + return max_profit + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print(max_profit([7, 1, 5, 3, 6, 4])) + From b44851de13eecb8a2000730d46513a48dafc4c6b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Oct 2023 14:21:01 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../best_time_to_buy_and_sell_stock.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/greedy_methods/best_time_to_buy_and_sell_stock.py b/greedy_methods/best_time_to_buy_and_sell_stock.py index b93ae5d7fbcd..37b050b5d088 100644 --- a/greedy_methods/best_time_to_buy_and_sell_stock.py +++ b/greedy_methods/best_time_to_buy_and_sell_stock.py @@ -4,19 +4,20 @@ We can only complete one transaction (buy one and sell one share of the stock). We must buy before we can sell. -Example : prices = [7, 1, 5, 3, 6, 4] +Example : prices = [7, 1, 5, 3, 6, 4] max_profit will return 5 - which is by buying at price 1 and selling at price 6. This problem can be solved using the concept of "GREEDY ALGORITHM". -We iterate over the price array once, keeping track of the lowest price point +We iterate over the price array once, keeping track of the lowest price point (buy) and the maximum profit we can get at each point. -The greedy choice at each point is to either buy at the current price if -it's less than our current buying price, or sell at the current price if the profit +The greedy choice at each point is to either buy at the current price if +it's less than our current buying price, or sell at the current price if the profit is more than our current maximum profit. """ -def max_profit(prices : list[int]) -> int: + +def max_profit(prices: list[int]) -> int: """ >>> max_profit([7, 1, 5, 3, 6, 4]) 5 @@ -27,7 +28,7 @@ def max_profit(prices : list[int]) -> int: return 0 min_price = prices[0] - max_profit : int = 0 + max_profit: int = 0 for price in prices: if price < min_price: @@ -37,9 +38,10 @@ def max_profit(prices : list[int]) -> int: return max_profit + if __name__ == "__main__": import doctest + doctest.testmod() - - print(max_profit([7, 1, 5, 3, 6, 4])) + print(max_profit([7, 1, 5, 3, 6, 4])) From a685156731d1215de89cbd28e2587f9810ca7ed2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 8 Oct 2023 18:59:23 +0200 Subject: [PATCH 3/3] Update best_time_to_buy_and_sell_stock.py --- .../best_time_to_buy_and_sell_stock.py | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/greedy_methods/best_time_to_buy_and_sell_stock.py b/greedy_methods/best_time_to_buy_and_sell_stock.py index 37b050b5d088..4aea19172ece 100644 --- a/greedy_methods/best_time_to_buy_and_sell_stock.py +++ b/greedy_methods/best_time_to_buy_and_sell_stock.py @@ -1,8 +1,7 @@ """ -Given a list of stock prices, -This program calculates the maximum profit that can be made from a single buy and sell. -We can only complete one transaction (buy one and sell one share of the stock). -We must buy before we can sell. +Given a list of stock prices calculate the maximum profit that can be made from a +single buy and sell of one share of stock. We only allowed to complete one buy +transaction and one sell transaction but must buy before we sell. Example : prices = [7, 1, 5, 3, 6, 4] max_profit will return 5 - which is by buying at price 1 and selling at price 6. @@ -10,10 +9,9 @@ This problem can be solved using the concept of "GREEDY ALGORITHM". We iterate over the price array once, keeping track of the lowest price point -(buy) and the maximum profit we can get at each point. -The greedy choice at each point is to either buy at the current price if -it's less than our current buying price, or sell at the current price if the profit -is more than our current maximum profit. +(buy) and the maximum profit we can get at each point. The greedy choice at each point +is to either buy at the current price if it's less than our current buying price, or +sell at the current price if the profit is more than our current maximum profit. """ @@ -31,10 +29,8 @@ def max_profit(prices: list[int]) -> int: max_profit: int = 0 for price in prices: - if price < min_price: - min_price = price - elif price - min_price > max_profit: - max_profit = price - min_price + min_price = min(price, min_price) + max_profit = max(price - min_price, max_profit) return max_profit @@ -43,5 +39,4 @@ def max_profit(prices: list[int]) -> int: import doctest doctest.testmod() - print(max_profit([7, 1, 5, 3, 6, 4]))