Stock Maximize Discussions | Algorithms | HackerRank
  • + 1 comment

    fast and simple. find the max price, split the array before and after max price, buy all stocks before max price and then sell them at max price; repeat above procedure on the array after max element. Thats it!

        def iter_over_p(p):
            ind_max = p.index(max(p))
            inv = sum(p[:ind_max])
            pf = len(p[:ind_max])*p[ind_max] - inv
            if len(p[ind_max+1:]) > 0:
                pf += iter_over_p(p[ind_max+1:])
            return pf
        profit = iter_over_p(prices)