반응형
https://level.goorm.io/exam/43114/%EA%B3%B5%EC%97%B0-%EC%A2%8C%EC%84%9D/quiz/1
구름LEVEL
난이도별 다양한 문제를 해결함으로써 SW 역량을 향상시킬 수 있습니다.
level.goorm.io
DP문제.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# UTF-8 encoding when using korean | |
import sys | |
N = int(sys.stdin.readline()) | |
M = int(sys.stdin.readline()) | |
arr = [0 for _ in range(N+1)] | |
for _ in range(M): | |
idx = int(sys.stdin.readline()) | |
arr[idx] = 1 | |
# table[1][0], table[1][1] = 0은 해당 위치 사람이 자기자리, 1은 자기자리 아닌 경우 | |
# table[2][0] = [1,2] (table[1][0] 값에서 변화 없음), table[2][1] = table[1][1] | |
table = [[0,0] for _ in range(N+1)] | |
# 첫번째 사람 = 자기 자리에만 앉을 수 있다. | |
table[1][0], table[1][1] = 1, 0 | |
for person in range(2, N+1): | |
# 1. 해당 사람이 자기자리에 앉는 경우 = 이전 사람이 어떻게 앉든 상관없다. | |
table[person][0] = table[person-1][0] + table[person-1][1] | |
# print(table[person]) | |
# 2. 해당 사람이 자기자리를 바꾸려 할 경우 | |
# 2-1. 이전 사람이 vip면 자리를 벗어날 수 없음 | |
# 2-2. 그렇지 않다면, 이전 사람이 자기자리를 지키고 있어야 자리를 바꿀 수 있다 | |
table[person][1] = 0 if arr[person-1] == 1 else table[person-1][0] | |
# 해당 사람이 vip인 경우 = 자기 자리에만 앉아야 한다. | |
if arr[person] == 1: | |
table[person][1] = 0 | |
# 마지막 사람까지의 경우의 수 | |
print(sum(table[-1])) | |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] 프로그래머스. 2018 카카오 recruit - 캐시 (Level 2) (0) | 2020.03.24 |
---|---|
[Python] 백준 16236. 아기 상어 (0) | 2020.03.23 |
[Python] 구름. 달걀 부화기 (0) | 2020.03.18 |
[Python] 구름. 외주 (0) | 2020.03.17 |
[Python] 백준 1005. ACM Craft (0) | 2020.03.14 |