나의 풀이
def solution(board, moves):
stack=[]
cnt=0
#j는 열(column)을 의미한다.
#len(board)는 행의 갯수를 의미한다.
#move리스트에서 1은 0열(column)을 의미한다. 5는 4열(column)을 의미한다.
for j in range(len(moves)):
for i in range(len(board)):
# print(board[i][moves[j]-1]) 열단위로 접근한다.
if board[i][moves[j]-1] !=0:
#stack processing
stack.append(board[i][moves[j]-1])
board[i][moves[j]-1]=0
#스택에 동일한 값인지 아닌지 확인을 한다.
if len(stack)>=2 and stack[-2] == stack[-1]:
stack.pop()
stack.pop()
cnt+=2
break
return cnt
다른 사람의 풀이
def solution(boards, moves):
stack=[]
cnt=0
#moves의 요소를 index로 활용하기 위해 각 요소에서 1씩 빼준다.
moves = list(map(lambda mv: mv-1, moves))
for i in moves:
for board in boards:
#board는 boards라는 2차원 배열을 "행"단위(1차원리스트)로 접근한다.
#board[i]는 행단위로 접근시 i번째 요소를 출력한다.
#결과론적으로는 "열접근법"이다.
print(board)
if board[i] !=0:
#stack processing
stack.append(board[i])
board[i]=0
#스택에 동일한 값인지 아닌지 확인을 한다.
if len(stack)>=2 and stack[-2] == stack[-1]:
stack.pop()
stack.pop()
cnt+=2
break
return cnt
'프로그래머스(파이썬) > LV.1(파이썬)' 카테고리의 다른 글
명예의 전당→킹 받네!! 열 받네!! + del vs remove() (0) | 2022.12.21 |
---|---|
신규 아이디 추천→isalpha(), isdigit(), 정규식★★ (0) | 2022.12.21 |
폰켓몬★★ → 해설, 조합X (0) | 2022.12.21 |
키패드 누르기→매개변수로 값주기, 현재값, next값★ (0) | 2022.12.20 |
숫자 짝꿍 → 일치 + count() + min(a,b) + len(answer) == a.count('0')★★ + 모든 요소가 0인 경우 (0) | 2022.12.20 |