0 votes
in Python by

Given a list of stock prices in ascending order by datetime, write a function that outputs the max profit by buying and selling at a specific interval.

1 Answer

0 votes
by

stock_prices = [10,5,20,32,25,12]

dts = [

    '2019-01-01', 

    '2019-01-02',

    '2019-01-03',

    '2019-01-04',

    '2019-01-05',

    '2019-01-06',

]

def max_profit(stock_prices,dts) -> 27

There are many ways you could go about solving this problem. A good first step is thinking about what our goal is: if we want the maximum profit, then ideally we want to buy at the lowest possible price and sell at the highest possible price. However, since we cannot go back in time, we have a constraint that our sell date must be after our buy date.

Related questions

+1 vote
asked Oct 28, 2022 in Python by SakshiSharma
+1 vote
asked Oct 28, 2022 in Python by SakshiSharma
...