📖 문제
https://programmers.co.kr/learn/courses/30/lessons/64061
💻 코드
def solution(board, moves):
# stack으로 인형 쌓기
sl = [list() for _ in range(len(board))]
for c in range(len(board)):
for r in range(len(board)-1,-1,-1):
if board[r][c] == 0:
break
sl[c].append(board[r][c])
bowl = list()
count = 0
for m in moves:
# 인형이 집어지지 않는 경우
if not sl[m - 1]:
continue
pick = sl[m - 1].pop()
# (바구니에 인형이 있고) 같은 모양 인형 두 개 연속한 경우
if bowl and pick == bowl[-1]:
bowl.pop()
count += 2
# 연속하지 않은 경우
else:
bowl.append(pick)
return count
🙌 한마디
더 직관적인 문제 풀이를 위해 먼저 인형들을 stack에 쌓고 인형 뽑기 코드를 짰다.
'Problem Solving > 📕Programmers' 카테고리의 다른 글
[Programmers] 불량 사용자 (Python) (0) | 2021.10.11 |
---|---|
[Programmers] 비밀지도 (Python) (0) | 2021.10.10 |
[Programmers] 튜플 (Python) (0) | 2021.10.09 |
[Programmers] n진수 게임 (Python) (0) | 2021.10.08 |
[Programmers] 캐시 (Python) (0) | 2021.10.07 |