[SW Expert Academy] D2 1859번 백만 장자 프로젝트(python)

good_da22·2022년 5월 3일
0

SW Expert Academy

목록 보기
1/20
post-thumbnail

SW Expert Academy

D2 1859번 백만 장자 프로젝트 / Python

문제

풀이과정

이득을 구하기 위해서 최대 값을 찾고 갱신하는 과정의 풀이를 하려 했으나 실패

리스트를 뒤에서부터 탐색, 가장 뒤에 있는 값을 최대 값으로 설정
이득을 계산하고 새로운 최대 값을 만나면 갱신

소스코드

t = int(input())

answer = []

for i in range(t):
  n = int(input())
  price = list(map(int, input().split()))

  best_price = price[-1]
  result = 0
  for j in range(2, n+1):

    if best_price > price[-j]:
      result += (best_price - price[-j])
    elif best_price < price[-j]:
      best_price = price[-j]
  
  answer.append(result)

for i in range(t):
  print("#%d %d" %(i+1, answer[i]))
profile
dev blog

0개의 댓글