https://school.programmers.co.kr/learn/courses/30/lessons/64061
해설 코드
def solution(board, moves):
answer = 0
stk = []
for i in moves:
for j in range(len(board)):
# 세로 단위로 검색, 0이 아닐경우 스택에 넣습니다.
if board[j][i-1] != 0:
stk.append(board[j][i-1])
board[j][i-1] = 0
if len(stk) >= 2:
if stk[-1] == stk[-2]:
answer += 2
stk.pop()
stk.pop()
break
return answer
내 코드
def solution(board, moves):
answer = 0
stack = []
for m in moves :
for j in range(len(board)) :
if board[j][m-1] != 0 :
stack.append(board[j][m-1])
board[j][m-1] = 0 # 인형 뺌
if len(stack) > 1 : # 최소 stack에 2개가 들어있어야 비교 가능
d1 = stack.pop()
d2 = stack.pop()
if d1 == d2 :
answer += 2
else :
stack.append(d2)
stack.append(d1)
break # 세로 한 줄에 대해서 검사하면 다음 move로 이동
return answer
2회차 - 25.03.31
def solution(board, moves):
answer = 0
stack = []
for m in moves :
for i in range(len(board)) :
if board[i][m-1] != 0 :
stack.append(board[i][m-1])
board[i][m-1] = 0
if len(stack) > 1 :
d1 = stack.pop()
d2 = stack.pop()
print('d', d1, d2)
if d1 == d2 : # stack이 비어있지 않고, 위에 두개가 같으면
answer += 2
else :
stack.append(d2)
stack.append(d1)
break # 다음 m으로
return answer
다른 정답 코드
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
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[Python] 프로그래머스 lv.2 조이스틱 (0) | 2025.03.31 |
---|---|
[Python] 프로그래머스 lv. 0 분수의 덧셈 (0) | 2025.03.26 |
[Python] 프로그래머스 lv.1 달리기 경주 (0) | 2025.03.26 |
[프로그래머스] 방문 길이 (1) | 2025.03.04 |
[Python] 프로그래머스 lv.2 피로도 (0) | 2024.12.17 |