From 034ac610d9fa28ebcb4a06e4bd0551cb31c77a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Fri, 12 Apr 2024 22:20:47 +0900 Subject: [PATCH] =?UTF-8?q?2024-04-10=20=ED=81=AC=EB=A0=88=EC=9D=B8=20?= =?UTF-8?q?=EC=9D=B8=ED=98=95=20=EB=BD=91=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suhyun113/README.md | 3 ++- .../\354\212\244\355\203\235/5-suhyun113.py" | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 "suhyun113/\354\212\244\355\203\235/5-suhyun113.py" diff --git a/suhyun113/README.md b/suhyun113/README.md index f3aaba3..d79604f 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -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) | \ No newline at end of file +| 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) | \ No newline at end of file diff --git "a/suhyun113/\354\212\244\355\203\235/5-suhyun113.py" "b/suhyun113/\354\212\244\355\203\235/5-suhyun113.py" new file mode 100644 index 0000000..b07a0f8 --- /dev/null +++ "b/suhyun113/\354\212\244\355\203\235/5-suhyun113.py" @@ -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 \ No newline at end of file