728x90
반응형
풀이 :
뒤에서 부터 돌면서 풀기 -> 가장 큰 이익을 얻기 위해선 가장 높은 매매가일 때 판매해야 하기 때문
Code
public class Solution1859 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
int n = sc.nextInt();
long diff = 0; // max - current = diff
// int로 하면 데이터 범위가 초과되어서 long
int max_value = 0;
int[] arr = new int[n];
for (int j = 0; j < n; j++) {
arr[j] = sc.nextInt();
}
for (int j = n - 1; j >= 0; j--) {
if(arr[j] > max_value) {
max_value = arr[j]; // 현재 값이 max보다 크면 max 갱신
}
diff += max_value - arr[j]; // 작으면 그 차이가 이익이므로 더함
}
System.out.printf("#%d %d\n", tc, diff);
}
sc.close();
}
}
728x90
반응형
'SWEA' 카테고리의 다른 글
swea 1928 [D2] Base64 Decoder JAVA (0) | 2023.11.20 |
---|---|
swea 1926 [D2] 간단한 369게임 JAVA (0) | 2023.11.20 |
swea 1288 [D2] 새로운 불면증 치료법 JAVA (0) | 2023.11.20 |
swea 1285 [D2] 아름이의 돌 던지기 JAVA (0) | 2023.11.20 |
swea 1284 [D2] 수도 요금 경쟁 JAVA (2) | 2023.11.20 |