99클럽 코테 스터디 8일차 TIL + RGB거리

Saang Bum Kim·2024년 4월 30일
0

99클럽

목록 보기
21/59

문제

링크텍스트

난관

  • 모든 경우를 저장하려고 하니 시간제한에 계속 걸렸다.
  • jupyter로 문제를 풀고 있는데, 테스트에 좋은 code snipet를 찾았다.

링크텍스트

f = open("in.txt", "w")
f.write(i)
f.close()

import sys
sys.stdin = open('in.txt', 'r')
input = sys.stdin.readline

결과

N = int(input())
costs = [list(map(int, input().split())) for _ in range(N)]
# print(costs)

INF = int(1e9)
dp = [[INF] * N for _ in range(N)]
# print(dp)

for i in range(3):  # i: color id
    dp[0][i] = costs[0][i]  # accumulated cost for house #0 which is painted with color id i

for i in range(1,N):  # i: current house id
    for j in range(3):  # color id for the current house 
        for k in range(3):  # color id for the previous house
            if j == k: continue
            dp[i][j] = min(dp[i][j], dp[i-1][k]+costs[i][j])

print(min(dp[-1]))

profile
old engineer

0개의 댓글