Untitled
unknown
plain_text
2 years ago
1.0 kB
7
Indexable
import java.util.Scanner;
public class StockTrading {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt(); // Number of test cases
for (int t = 1; t <= T; t++) {
int N = scanner.nextInt(); // Number of days
int[] prices = new int[N];
for (int i = 0; i < N; i++) {
prices[i] = scanner.nextInt();
}
int maxProfit = maximizeEarnings(N, prices);
System.out.println("#" + t + " " + maxProfit);
}
scanner.close();
}
public static int maximizeEarnings(int N, int[] prices) {
int[] maxEarnings = new int[N];
int maxProfit = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
maxEarnings[i] = Math.max(maxEarnings[i], maxEarnings[j] + prices[i] - prices[j]);
}
maxProfit = Math.max(maxProfit, maxEarnings[i]);
}
return maxProfit;
}
}
Editor is loading...