알고리즘/Greedy

백준 2847번: 게임을 만든 동준이 - 파이썬(greedy)

Aytekin 2022. 4. 19. 22:43
728x90

가장 높은 레벨이 가장 큰 점수를 얻어야 하는 문제인데 최소한의 횟수를 사용해야 하므로

점수 조정시 다음 레벨과의 점수 차이를 1로 만들어 주면 된다.

 

문제를 풀 때 주의해야 할 점은

높은 레벨에서부터 비교하면서 점수를 낮춰주어야 한다.

낮은 레벨부터 점수를 낮추게 되면

[5,5,5] > [4,5,5] > [4,4,5] 

이런 식으로 점수가 오름차순이 되지 않을 수 있다.

실제로 처음에 이렇게 풀었다가 틀렸었다...

 

# 2847번 : 게임을 만든 동준이

n = int(input())

levels = []
for _ in range(n):
    levels.append(int(input()))

levels.reverse()
cnt = 0
for i in range(n-1):
    while levels[i] <= levels[i+1]:
        cnt += 1
        levels[i+1] -= 1

print(cnt)

https://github.com/aytekin827/algorithm_greedy

 

GitHub - aytekin827/algorithm_greedy: basic concept of greedy algorithm and some problems from Beakjoon online judge

basic concept of greedy algorithm and some problems from Beakjoon online judge - GitHub - aytekin827/algorithm_greedy: basic concept of greedy algorithm and some problems from Beakjoon online judge

github.com

이외의 알고리즘 관련 정리내용과 백준 문제풀이가 위의 깃에 정리되어 있습니다.

 

감사합니다.

728x90