-
[Python] 크레인 인형뽑기Algorithm/프로그래머스 2021. 4. 26. 23:59
1. 문제 📚
게임 화면의 격자의 상태가 담긴 2차원 배열 board와 인형을 집기 위해 크레인을 작동시킨 위치가 담긴 배열 moves가 매개변수로 주어질 때, 크레인을 모두 작동시킨 후 터트려져 사라진 인형의 개수를 return 하도록 solution 함수를 완성해주세요.
2. 입출력 예 📋
3. 알고리즘 ✅
- 하나 씩 비교하기 위해 moves와 board를 각각 for문으로 꺼내준다
- 0이 아닐 시 (인형이 있는 것) basket 배열에 넣어준다
- 배열이 마지막 숫자와 같을 경우 answer 에 2를 더해준다 → 총 2개가 사라지는 것이기 때문에
- 사라진 2개를 basket에서 없애준다
4. 소스코드 💻
def solution(board, moves): answer = 0 bucket = [] for move in moves: for i in board: if i[move - 1] != 0: bucket.append(i[move - 1]) i[move - 1] = 0 if len(bucket) > 1: if bucket[-1] == bucket[-2]: answer += 2 bucket = bucket[:-2] break return answer
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Python] 2016년 (0) 2021.05.01 [Python] 체육복 (0) 2021.05.01 [Python] 모의고사 (0) 2021.04.29 [Python] 완주하지 못한 선수 (0) 2021.04.27 [Java] 프로그래머스 Level2 전화번호 목록 (0) 2021.03.23