Stock Maximize Discussions | | HackerRank

Stock Maximize

  • + 0 comments

    Java O(n)

      public static long stockmax(List<Integer> prices) {
            long maxProfit = 0;
            int maxFuturePrice = 0;
    
            for (int i = prices.size() - 1; i >= 0; i--) {
                if (prices.get(i) > maxFuturePrice) {
                    maxFuturePrice = prices.get(i);
                }
                maxProfit += maxFuturePrice - prices.get(i);
            }
    
            return maxProfit;
        }