From 96fe8d5b6f850aa0b759238cca7247e2c7eb43fd Mon Sep 17 00:00:00 2001 From: BHAWANA SHARMA Date: Sat, 4 Oct 2025 21:41:54 +0530 Subject: [PATCH 1/3] Added Stock Buy and Sell (Max One Transaction) solution in Python --- .../arrays/max_profit_one_transaction.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 data_structures/arrays/max_profit_one_transaction.py diff --git a/data_structures/arrays/max_profit_one_transaction.py b/data_structures/arrays/max_profit_one_transaction.py new file mode 100644 index 000000000000..feda69ea5ce3 --- /dev/null +++ b/data_structures/arrays/max_profit_one_transaction.py @@ -0,0 +1,68 @@ +# --------------------------------------------------------------- +# Title: Stock Buy and Sell - Max One Transaction Allowed +# +# Problem: +# Given an array prices[] representing the price of a stock on each day, +# find the maximum profit achievable by performing at most one transaction. +# (You must buy before you sell.) +# +# Example: +# Input: prices = [7, 10, 1, 3, 6, 9, 2] +# Output: 8 (Buy at 1, Sell at 9) +# +# Approach: +# We traverse the list once, keeping track of: +# - min_price_so_far: The lowest price encountered so far. +# - max_profit: The maximum profit seen so far. +# +# For each price: +# - Compute potential profit = current_price - min_price_so_far. +# - Update max_profit if this profit is greater than the previous. +# - Update min_price_so_far if a smaller price is found. +# +# Time Complexity: O(n) +# Space Complexity: O(1) +# --------------------------------------------------------------- + +def max_profit(prices): + """ + Calculate maximum profit from at most one buy-sell transaction. + + Parameters: + prices (list[int]): List of stock prices per day. + + Returns: + int: Maximum profit possible. Returns 0 if no profit is possible. + """ + + # Edge case: no transaction possible if list is too short + if not prices or len(prices) < 2: + return 0 + + # Initialize minimum price as the first day's price + min_price_so_far = prices[0] + + # Initialize maximum profit + max_profit = 0 + + # Traverse price list starting from the second day + for price in prices[1:]: + # Update the minimum price seen so far + min_price_so_far = min(min_price_so_far, price) + + # Calculate today's potential profit + profit_today = price - min_price_so_far + + # Update max profit if today's profit is higher + max_profit = max(max_profit, profit_today) + + return max_profit + + +# --------------------------------------------------------------- +# Example Usage (For quick testing) +# --------------------------------------------------------------- +if __name__ == "__main__": + prices = [7, 10, 1, 3, 6, 9, 2] + print("Stock Prices:", prices) + print("Maximum Profit:", max_profit(prices)) # Expected Output: 8 From 4971c21d8c89a0f5deda0919444b7dad7cde5788 Mon Sep 17 00:00:00 2001 From: BHAWANA SHARMA Date: Sat, 4 Oct 2025 21:55:54 +0530 Subject: [PATCH 2/3] Add type hints and doctests for max_profit function --- .../arrays/max_profit_one_transaction.py | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/data_structures/arrays/max_profit_one_transaction.py b/data_structures/arrays/max_profit_one_transaction.py index feda69ea5ce3..097a140769fc 100644 --- a/data_structures/arrays/max_profit_one_transaction.py +++ b/data_structures/arrays/max_profit_one_transaction.py @@ -24,39 +24,38 @@ # Space Complexity: O(1) # --------------------------------------------------------------- -def max_profit(prices): +from typing import List + +def max_profit(prices: List[int]) -> int: """ - Calculate maximum profit from at most one buy-sell transaction. + Calculate the maximum profit from at most one buy-sell transaction. Parameters: - prices (list[int]): List of stock prices per day. + prices (List[int]): List of stock prices per day. Returns: int: Maximum profit possible. Returns 0 if no profit is possible. - """ - # Edge case: no transaction possible if list is too short + Doctests: + >>> max_profit([7, 10, 1, 3, 6, 9, 2]) + 8 + >>> max_profit([7, 6, 4, 3, 1]) + 0 + >>> max_profit([1, 3, 6, 9, 11]) + 10 + """ if not prices or len(prices) < 2: return 0 - # Initialize minimum price as the first day's price min_price_so_far = prices[0] + max_profit_val = 0 - # Initialize maximum profit - max_profit = 0 - - # Traverse price list starting from the second day for price in prices[1:]: - # Update the minimum price seen so far min_price_so_far = min(min_price_so_far, price) + max_profit_val = max(max_profit_val, price - min_price_so_far) - # Calculate today's potential profit - profit_today = price - min_price_so_far - - # Update max profit if today's profit is higher - max_profit = max(max_profit, profit_today) + return max_profit_val - return max_profit # --------------------------------------------------------------- From d0e73eb9cc4f4b24d2bb49de1a2cc279dd8d72a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 4 Oct 2025 16:35:12 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/max_profit_one_transaction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/max_profit_one_transaction.py b/data_structures/arrays/max_profit_one_transaction.py index 097a140769fc..19eb7478af52 100644 --- a/data_structures/arrays/max_profit_one_transaction.py +++ b/data_structures/arrays/max_profit_one_transaction.py @@ -26,6 +26,7 @@ from typing import List + def max_profit(prices: List[int]) -> int: """ Calculate the maximum profit from at most one buy-sell transaction. @@ -57,7 +58,6 @@ def max_profit(prices: List[int]) -> int: return max_profit_val - # --------------------------------------------------------------- # Example Usage (For quick testing) # ---------------------------------------------------------------