티스토리 뷰
1859. 백만 장자 프로젝트
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. 테스트 케이스 정답이 이상하게 나온다면 범위를 생각해보기.
'코딩테스트 > SW Expert' 카테고리의 다른 글
[SWEA] 1954. 달팽이 숫자 (0) | 2021.01.30 |
---|---|
[SWEA] 2007. 패턴 마디의 길이 (0) | 2021.01.29 |
[SWEA] 1926. 간단한 369게임 (0) | 2021.01.29 |
[SWEA] 1204. [S/W 문제해결 기본] 1일차 - 최빈수 구하기 (0) | 2021.01.28 |
[SWEA] 1974. 스도쿠 검증 (0) | 2021.01.21 |
댓글