
[문제 링크]
https://programmers.co.kr/learn/courses/30/lessons/64061
[소스코드]
def solution(board, moves): stacklist = [] answer = 0 for i in moves: for j in range(len(board)): if board[j][i-1] != 0: stacklist.append(board[j][i-1]) board[j][i-1] = 0 if len(stacklist) > 1: if stacklist[-1] == stacklist[-2]: stacklist.pop(-1) stacklist.pop(-1) answer += 2 break return answer
[풀이]
- 순회할 때, move는 열좌표이며 1부터 시작하는 것에 유의하자.
- 순회하면서 board를 0으로 대체하고, 스택에 쌓아준다.
- 길이가 2이상이고 스택에서 제일 위 두 값이 같은 경우 answer에 2를 더하고 stack에서 두 개를 pop처리한다.
댓글을 사용할 수 없습니다.