๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SWEA

[SWEA] 1859. ๋ฐฑ๋งŒ ์žฅ์ž ํ”„๋กœ์ ํŠธ

by JulesJ 2022. 5. 28.
728x90

1859. ๋ฐฑ๋งŒ ์žฅ์ž ํ”„๋กœ์ ํŠธ - D2

 

์ด์ œ D2 ๋ ˆ๋ฒจ ๋ฌธ์ œ์ด๋‹ค.

 

๋งจ ์ฒซ๋ฒˆ์งธ ์ˆ˜ T๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ˆ˜์ด๋‹ค. 

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์ค„์—๋Š” N์ผ์„ ์ž…๋ ฅ ๋ฐ›๊ณ ,

๊ทธ ๋ฐ‘์ค„์—๋Š” ๊ฐ ๋‚ ์˜ ๋งค๋งค๊ฐ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” N๊ฐœ์˜ ์ž์—ฐ์ˆ˜(int [] num)๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.

 

profit์€ ์ถœ๋ ฅํ•  ๋ˆ„์  ์ด์ต์„ ์ €์žฅํ•œ๋‹ค.

(N์˜ ๋ฒ”์œ„๊ฐ€ 2 N 1,000,000 ์ด๊ธฐ ๋•Œ๋ฌธ์— longํƒ€์ž…์œผ๋กœ ์„ ์–ธํ–ˆ๋‹ค.)

max๋Š” ๋งค๋งค๊ฐ€์˜ ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

 

for๋ฌธ์€ ๋’ค์—์„œ ๋ถ€ํ„ฐ ๊ฐ์†Œ์‹œํ‚ค๋ฉฐ ๋ฐ˜๋ณตํ•˜๋Š”๋ฐ,

์ตœ๋Œ€๊ฐ’์ด num[i]๋ณด๋‹ค ์ ์œผ๋ฉด ๊ตฌ๋งค๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค ์ƒ๊ฐํ•˜๊ณ  max = num[i]๋กœ ํ•œ๋‹ค.

์ตœ๋Œ€๊ฐ’์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ์ด์ต์— ํ˜„์žฌ์˜ ๋งˆ์ง„(max-num[i])์„ ๊ตฌํ•œ๋‹ค.

 

 

import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
	public static void main(String args[]) throws Exception
	{
		Scanner sc = new Scanner(System.in);
		int T=sc.nextInt();
		for(int test_case = 1; test_case <= T; test_case++)
		{
			int day = sc.nextInt();
			int[] num = new int[day];
			for(int i = 0; i < day; i++){
				num[i] = sc.nextInt();
			}
			int max = num[day-1];
			long profit = 0;

			for(int i = day-1; i >= 0; --i){
				if(num[i] > max){
					max = num[i];
				}else{
					profit += max - num[i];
				}
			}
			System.out.println("#"+test_case + " " + profit);
		}
	}
}

 

 

 

๋ ๐Ÿง

๋Œ“๊ธ€