티스토리 뷰

1859. 백만 장자 프로젝트

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LrsUaDxcDFAXc&categoryId=AV5LrsUaDxcDFAXc&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

import java.util.Scanner;

// 210128
// 1859. 백만 장자 프로젝트
public SWEA1859 Solution {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); 
		
		int T = sc.nextInt();
		for (int tc=0; tc<T; tc++) {
			int N = sc.nextInt();
			int[] arr = new int[N];
			
			for(int i=0; i<N; i++) {
				arr[i] = sc.nextInt();
			}
			
			int max = arr[N-1];
			long total = 0;
			for(int i=N-2; i>=0; i--) {
				if (max >= arr[i]) total += (max - arr[i]);
				else max = arr[i];
			}
			
			System.out.println("#" + (tc+1) + " " + total);
		} // for
		
	} // main
}

 

1. 생각의 전환!! 뒤에서부터 생각하기. 어차피 앞에 더 큰 수가 있어도 시간이 이미 지나서 팔 수가 없다.

2. 테스트 케이스 정답이 이상하게 나온다면 범위를 생각해보기.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함