반응형
문제 이해
아이디어
더보기
어려웠었다..
괜히 어렵게 생각해서..복잡하게 풀었더니
테케는 통과되는데 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
반응형
'Algorithm > SWEA' 카테고리의 다른 글
[SWEA] 1225. 암호생성기 D3 JAVA (0) | 2021.03.30 |
---|---|
[SWEA] 1873. 상호의 배틀 필드 D3 JAVA (0) | 2021.03.30 |
[SWEA] 1926. 간단한 369 게임 D2 JAVA (0) | 2021.03.26 |
[SWEA] 2007. 패턴 마디의 길이 D2 JAVA (0) | 2021.03.26 |
[SWEA] 2005. 파스칼의 삼각형 D2 JAVA (0) | 2021.03.26 |