240518 주식

Jongleee·2024년 5월 18일
0

TIL

목록 보기
576/737
static int day;
static long profit;
static int[] stock;

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int testCase = Integer.parseInt(br.readLine());
	StringTokenizer st;

	for (int t = 1; t <= testCase; t++) {
		day = Integer.parseInt(br.readLine());
		stock = new int[day];
		profit = 0;

		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < day; i++) {
			stock[i] = Integer.parseInt(st.nextToken());
		}

		int max = stock[day - 1];
		calculateProfit(max);

		System.out.println(profit);
	}
}

static void calculateProfit(int max) {
	for (int i = day - 2; i >= 0; i--) {
		if (stock[i] < max) {
			profit += max - stock[i];
		} else {
			max = stock[i];
		}
	}
}

출처:https://www.acmicpc.net/problem/11501

0개의 댓글