본문 바로가기

Algorithm/SWEA

[SWEA] 1859. 백만장자 프로젝트 D2 JAVA

반응형


문제 이해

 

아이디어

더보기

어려웠었다..

괜히 어렵게 생각해서..복잡하게 풀었더니

테케는 통과되는데 Runtime Error발생.

싹 갈아엎었다.

뒤에서부터...해보라는 의견을 인용하였더니

진짜..간단하게 풀렸다...

 

코드

import java.io.*;
import java.util.*;

public class Solution {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		int T = Int(br.readLine());
		for(int tc=1; tc<=T; ++tc) {
			int N = Int(br.readLine());
			int[] arr= new int[N];
			st = new StringTokenizer(br.readLine()," ");
			for(int i=0; i<N;++i) {
				arr[i] = Int(st.nextToken());
			}
			long sum=0;
			int max = arr[N-1];
			for(int i=N-2;i>=0;--i) {
				if(max > arr[i]) {
					sum += (max - arr[i]);
				}else {
					max = Math.max(max, arr[i]);
				}
			}
			System.out.println("#"+tc+" "+sum);
		}
	}

	static int Int(String s) {
		return Integer.parseInt(s);
	}
}

 

 


 

 

집사는개발자가되고파

 

choppadontbiteme.tistory.com

 

반응형