일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Level2
- BFS
- 파이썬
- CodingTest
- 조합
- 연습문제
- programmers
- counter
- coding
- join
- mysql
- 완전탐색
- python
- coding test
- lambda
- 데이터분석
- itertools
- 코딩테스트
- Stack
- 코딩
- time complexity
- import re
- 프로그래머스
- level4
- Queue
- sql
- lv4
- 코테
- collections
- 시간복잡도
- Today
- Total
ror_coding
[Programmers] 크레인 인형뽑기 게임 - 64061 본문
728x90
list의 행과 열을 바꾸기 위해 numpy 를 이용해서 transpose 해주고 stack을 이용해서 답 구하기 !
Question
게임 화면의 격자의 상태가 담긴 2차원 배열 board와 인형을 집기 위해 크레인을 작동시킨 위치가 담긴 배열 moves가 매개변수로 주어질 때, 크레인을 모두 작동시킨 후 터트려져 사라진 인형의 개수를 return 하도록 solution 함수를 완성해주세요.
Code
import numpy as np
def solution(board, moves):
answer = 0
stack = []
board = np.array(board).T
board = (board).tolist()
board = [[i for i in row if i!=0] for row in board] # 0(인형없음) 제거.
for m in moves:
if board[m-1] :
stack.append(board[m-1].pop(0)) # stack에 인형 넣고 board[m-1]에서 해당 인형 제거.
if len(stack)>=2 and stack[-1]==stack[-2] :
answer += 2
stack.pop()
stack.pop()
return answer
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/043.gif)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Python] Counter (collections) (0) | 2024.10.24 |
---|---|
[Programmers] [카카오 인턴] 키패드 누르기 - 67256 (1) | 2024.10.24 |
[Programmers] 귤 고르기 - 138476 (0) | 2024.10.24 |
[Programmers] 구명보트 - 42885 (1) | 2024.10.24 |
[Programmers] 햄버거 만들기 - 133502 (0) | 2024.10.23 |