Skip to content

Instantly share code, notes, and snippets.

@saichenko
Created November 14, 2023 02:30
Show Gist options
  • Save saichenko/ab7a7232004fa807927a91765625300f to your computer and use it in GitHub Desktop.
Save saichenko/ab7a7232004fa807927a91765625300f to your computer and use it in GitHub Desktop.
from collections import namedtuple
Days = namedtuple("Days", ("buy", "sell"))
def find_profit_days(n: int, prices: list[int]) -> Days:
min_price = prices[0]
max_profit = 0
buy_day = 1
sell_day = 1
for day in range(1, n):
if prices[day] < min_price:
min_price = prices[day]
buy_day = day + 1
elif prices[day] - min_price > max_profit:
max_profit = prices[day] - min_price
sell_day = day + 1
if max_profit == 0:
return Days(buy=0, sell=0)
return Days(buy=buy_day, sell=sell_day)
if __name__ == '__main__':
n = int(input())
prices = list(map(int, input().split(",")))
result = find_profit_days(n=n, prices=prices)
print(result.buy, result.sell)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment