[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Lv1] ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ / Python

๐Ÿ“š Problem Solving/Programmers

https://programmers.co.kr/learn/courses/30/lessons/64061

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„

[[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] 4

programmers.co.kr

import sys

input = sys.stdin.readline
n = int(input())
board = [list(map(int, input().split())) for _ in range(n)]
moves = list(map(int, input().split()))


def solution(board, moves):
    basket = []
    answer = 0
    for i in range(len(moves)):
        for j in range(len(board)):
            if board[j][moves[i] - 1] == 0:
                continue
            basket.append(board[j][moves[i] - 1])
            board[j][moves[i] - 1] = 0
            if len(basket) != 1 and basket[len(basket) - 1] == basket[len(basket) - 2]:
                del basket[len(basket) - 1]
                del basket[len(basket) - 1]
                answer += 2
            break
    return answer


print(solution(board, moves))

 

ํ•ด์„ค

ํฌ๋ ˆ์ธ์˜ ์ธ๋ฑ์Šค์— ์ธํ˜•์ด ์œ„์น˜ํ•˜๋ฉด ํ•ด๋‹น ์ธํ˜• ์œ„์น˜์˜ ๊ฐ’์„ 0์œผ๋กœ ๋ฐ”๊พธ๊ณ  ๊ทธ ๊ฐ’์„ basket์— ๋„ฃ์–ด์ค€๋‹ค.

์ด๋•Œ basket์— ๋งˆ์ง€๋ง‰ ๊ฐ’๊ณผ ๋ฐ”๋กœ ์ด์ „ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์›Œ์ฃผ๊ณ  answer๋ฅผ 2์”ฉ ๋Š˜๋ ค์ค€๋‹ค.