코딩 테스트(Python)/백준, 프로그래머스
프로그래머스) 크레인 인형뽑기 게임
5_hyun
2022. 3. 27. 19:32
반응형
https://programmers.co.kr/learn/courses/30/lessons/64061
코딩테스트 연습 - 크레인 인형뽑기 게임
[[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] [1,5,3,5,1,2,1,4] 4
programmers.co.kr
내 풀이(board을 어떻게 정렬하는지를 이해 못해서 구글링함, 이 이후 로직은 안보고 맞음)
def solution(board, moves):
answer = 0
rst = []#오른쪽 바구니
game = []
for i in range(len(board)):
game.append([])
for j in range(len(board)):
game[i].append(board[len(board)-1-j][i])
remove_set = {0}
for i in range(len(game)):
game[i] = [i for i in game[i] if i not in remove_set]
for i in moves:
if game[i-1]:
t = game[i-1].pop()
if rst and rst[-1] == t:
rst.pop()
answer += 2
else:
rst.append(t)
return answer
board를 문제에 맞게 정렬을 했다. 그리고 remove_set은 0을 제거하는 것이고 그 밑의 for문은 gmae[i] 마다 0이 있다면 0을 제거해주는 로직이다.(이것도 구글링해서 찾음) 그리고 스택을 이용하여 문제를 풀었다.
반응형