PS/SWEA
[SWEA] 1859.py : 백만 장자 프로젝트
bconfiden2
2020. 7. 10. 21:21
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
t = int(input())
for tc in range(t) :
n = int(input())
prices = list(map(int, input().split()))
ans = 0
max = 0
for i in range(n-1,-1,-1) :
if prices[i] > max :
max = prices[i]
ans += max - prices[i]
print("#" + str(tc+1), ans)
리스트의 앞에서부터가 아니라 뒤에서부터 탐색을 하면 간단하게 풀 수 있다.
뒤에서부터 앞으로 하나씩 가며 최댓값을 갱신해주고, 해당 최댓값과 현재값과의 차이를 누적하면 결과가 나온다.