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
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();
}
}
}
|