Skip to content

Commit

Permalink
2024-04-10 크레인 인형 뽑기
Browse files Browse the repository at this point in the history
  • Loading branch information
suhyun113 committed Apr 12, 2024
1 parent 4b6cb2d commit 034ac61
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion suhyun113/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
| 1차시 | 2024.03.25 | 그리디 | [체육복](https://school.programmers.co.kr/learn/courses/30/lessons/42862) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/2) |
| 2차시 | 2024.03.29 | 수학 | [소수 & 팰린드롬](https://www.acmicpc.net/problem/1747) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/9) |
| 3차시 | 2024.04.02 | 그리디 | [거스름돈](https://www.acmicpc.net/problem/14916) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/12) |
| 4차시 | 2024.04.06 | DP | [피보나치 수 5](https://www.acmicpc.net/problem/10870) | [#16](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/16) |
| 4차시 | 2024.04.06 | DP | [피보나치 수 5](https://www.acmicpc.net/problem/10870) | [#16](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/16) |
| 5차시 | 2024.04.10 | 스택 | [크레인 인형 뽑기 게임](https://school.programmers.co.kr/learn/courses/30/lessons/64061) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/19) |
24 changes: 24 additions & 0 deletions suhyun113/스택/5-suhyun113.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def solution(board, moves):
answer = 0
basket = [float("inf")] # board배열에서 뽑은 인형들을 넣는 바구니

N = len(board) # board 2차원 배열은 N*N이므로, board의 행 또는 열의 길이를 N으로 받음.

# moves 1차원 배열에 있는 요소에 따라 어떤 열의 인형을 뽑을 지 정함.
for move in moves: #[1 5 3 5 1 2 1 4]
move -= 1 # 1번째가 실제로 board배열에서는 0번째 이므로, 각 요소마다 1씩 빼줌.
doll = 0 # 인형이 비어있다고 가정 -> 0으로 초기화

for i in range(N):
if board[i][move] != 0: # 열은 move로 정해져 있고, 행이 i로 계속 변함.
doll = board[i][move] # 0이 아니라면, 인형이 있는 것이므로, 인형의 값을 변경
board[i][move] = 0
break

if doll != 0: # 인형이 비어있지 않아서 doll에 새로운 인형의 값이 들어갔을 때,
if basket[-1] == doll: # 바구니 가장 위에 담긴 인형과 새로 뽑은 인형이 같다면
basket.pop() # 바구니 가장 위의 인형 제거
answer += 2 # 같은 인형이 바구니에 담겼으므로, 2개의 인형이 터짐
else:
basket.append(doll) # 바구니의 가장 위의 인형과 같은 인형이 아니라면, doll을 새로 바구니에 추가하기
return answer

0 comments on commit 034ac61

Please sign in to comment.