Stock Maximize Discussions | Algorithms | HackerRank

Sort by

recency

|

365 Discussions

|

  • + 0 comments

    lol I overcomplicated this problem for some reason

  • + 0 comments

    Just go from end to beginning

    long stockmax(vector<int> p) {
        long ans = 0; int max_num = 0;
        for(int i = p.size() - 1; i >= 0; i--) 
        {
            max_num = max(max_num, p[i]);
            ans += max(0, max_num - p[i]);
        }
        return ans;
    }
    
  • + 2 comments

    Not a DP problem. Solution is just to iterate backwards. If our current price is greater than the max price, update the max price. If it is smaller, then there is profit to be made - and that profit is the difference between the current price and max price (as we would offload all shares on the max price day).

    def stockmax(prices):
        profit = 0
        maxPrice = 0
        for price in prices[::-1]:
            if price > maxPrice:
                maxPrice = price
            else:
                profit += maxPrice - price
        return profit
    
  • + 0 comments

    Here is my solution in java, javascript, python, C , C++, Csharp HackerRank Stock Maximize Problem Solution

  • + 0 comments
    def stockmax(prices):
        res = 0
        while prices:
            max_price = max(prices)
            idx_max = prices.index(max_price)
            cur_pft = sum([max_price - p for p in prices[:idx_max]])
            res += cur_pft
            prices = prices[idx_max+1:]
        return res