티스토리 뷰

import numpy as np
def solution(board, moves):
    answer = 0
    stack = []
    board = np.asarray(board)
    moves = np.asarray(moves)

    board = np.transpose(board)
    moves = moves - 1

    for i in moves:
        for idx, j in enumerate(board[i]):
            if j != 0:
                stack.append(j)
                board[i][idx] = 0
                break

        if len(stack) >= 2 and stack[-1] == stack[-2]:
            stack.pop()
            stack.pop()
            answer = answer + 2

    return answer

나쁘지 않았다고 생각하지만, 나빴다고 생각하는 한가지 부분이 있다면 굳이 numpy array로 바꿔서 했어야했나 하는 의문!

효율성이 있었더라면 나는 빠꾸받았을 것...

 

그렇다면, best 풀이를 보자

<내가 생각하는 BEST 풀이>

로직은 같지만 numpy 없이 풀었음.

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

그치 그냥 이렇게 해도 되지.

나는 뻘짓.. ㅇㅅㅇ 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함