‡ CODING TEST STUDY ‡/º 백준

[백준 | Java Silver I] (#1743) 게임을 만든 동준이

Trudy | 송연 2024. 7. 16. 16:16

문제

https://www.acmicpc.net/problem/2847


접근

그리디 알고리즘을 통해 하나씩 줄여나가면 됐던 문제이다. 

 

최종 코드

package week11.baek.july16;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class S2847 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        int[] level = new int[N];
        for (int i = 0; i < N; i++) {
            level[i] = Integer.parseInt(br.readLine());
        }

        int count = 0;
        for (int i = N-1; i > 0; i--) {
            while(level[i] <= level[i-1]) {
                level[i-1]--;
                count++;
            }

        }

        System.out.println(count);
    }
}