189. 크레인
1) 어떤 전략(알고리즘)으로 해결?
2) 코딩 설명
<내 풀이>
from collections import deque
def solution(board, moves):
board = deque(board)
boardlen = len(board)
basket = deque()
poppeddoll = 0
for i in range(len(moves)) :
if board[boardlen-1][moves[i]-1]==0 :
continue
else :
for j in range(boardlen) :
if board[j][moves[i]-1]!=0:
basket.append(board[j][moves[i]-1])
board[j][moves[i]-1]=0
if len(basket)>1 :
if basket[-1]==basket[-2] :
basket.pop()
basket.pop()
poppeddoll+=2
break
return poppeddoll
print(solution([[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]))
<내 틀렸던 풀이, 문제점>
from collections import deque
def solution(board, moves):
answer = 0
basket=deque()
board = deque(board)
for i in range(len(moves)) :
print(answer, list(basket), moves[i]-1)
if board[moves[i]-1] :
doll = board[moves[i]-1].pop()
if basket and basket[-1]==doll :
basket.pop()
answer+=2
else : basket.append(doll)
return answer
solution([[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])
from collections import deque
def solution(board, moves):
answer = 0
basket=deque()
board = deque(board)
boardlen = len(board[0])
for i in range(len(moves)) :
if board[len(board)-1][moves[i]-1] == 0 : continue
for j in range(boardlen) :
if board[j][moves[i]-1]==0 : continue
else :
if basket and basket[-1]==board[j][moves[i]-1] :
basket.pop()
answer+=2
else :
basket.append(board[j][moves[i]-1])
board[j][moves[i]-1]=0
break
return answer
solution([[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])
<반성 점 & 배운 점>
세게 배운 점 :
- 처음에 문제 이해하지 않고 생각없이 코딩부터 들이밀면 안된다.
- 설계, 문제이해 충분히 한 후 이를 구현하도록 하자
- 지금 코드가 이해안되고 답이 없으면 과감히 지워버리도록 하자