Search This Blog

Thursday, September 4, 2014

Algorithms - Dynamic Programming - Stock Maximize

Stock Maximize.

The following is the solution to Hacker Rank problem Stock Maximize using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score: 36/36

import java.util.Scanner;

public class StockMaximize {

       public static void main(String[] args) {
              /*
               * Enter your code here. Read input from STDIN. Print output to STDOUT.
               * Your class should be named Solution.
               */
              Scanner sc = new Scanner(System.in);
              int numOfTestCase = sc.nextInt();
              for (int i = 0; i < numOfTestCase; i++) {
                     int n = sc.nextInt();
                     long profit = 0;
                     int[] stockPrice = new int[n];

                     for (int j = 0; j < n; j++) {
                           stockPrice[j] = sc.nextInt();
                     }
                    
                     int currMax = Integer.MIN_VALUE;
                    
                     for (int j = n - 1; j >= 0; j--) {
                           if (currMax < stockPrice[j]) {
                                  currMax = stockPrice[j];
                           }
                           profit += (currMax - stockPrice[j]);
                     }
                     System.out.println(profit);
                    
                     sc.close();
              }
       }
}

1 comment:

  1. Harrah's Cherokee Casino & Hotel - MapYRO
    Find your herzamanindir.com/ way around the casino, find febcasino where everything is kadangpintar located with the most up-to-date information 메이피로출장마사지 about Harrah's Cherokee Casino & Hotel in gri-go.com Cherokee, NC.

    ReplyDelete

Labels