SWEA

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

녁이 2023. 11. 20. 20:01
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
반응형