https://programmers.co.kr/learn/courses/30/lessons/64061?language=python3
코딩테스트 연습 - 크레인 인형뽑기 게임
[[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
최종 코드
GitHub - hwayeon351/Programmers-Algorithms: 프로그래머스 알고리즘 소스 코드 모음
프로그래머스 알고리즘 소스 코드 모음. Contribute to hwayeon351/Programmers-Algorithms development by creating an account on GitHub.
github.com
def solution(board, moves):
answer = 0
stack = []
for m in moves:
for row in range(len(board)):
if board[row][m-1] != 0:
if len(stack) > 0:
if stack[-1] == board[row][m-1]:
answer += 2
stack.pop()
else: stack.append(board[row][m-1])
else: stack.append(board[row][m-1])
board[row][m-1] = 0
break
return answer
풀이 과정
풀이 시간 20분
def solution(board, moves):
answer = 0
stack = []
for m in moves:
#m-1열의 인형이 있는 행 찾기
for row in range(len(board)):
#뽑을 인형을 찾은 경우
if board[row][m-1] != 0:
#stack이 비어있지 않은 경우
if len(stack) > 0:
#stack의 top에 있는 인형과 현재 뽑으려는 인형이 같은 경우
if stack[-1] == board[row][m-1]:
#stack의 top에 있는 인형과 현재 뽑으려는 인형이 사라진다
answer += 2
stack.pop()
#stack의 top에 있는 인형과 현재 뽑으려는 인형이 다른 경우, stack에 인형을 쌓는다
else: stack.append(board[row][m-1])
#stack이 비어있는 경우, stack에 인형을 쌓는다
else: stack.append(board[row][m-1])
#인형을 뽑았으므로, 빈 칸으로 표현한다
board[row][m-1] = 0
break
return answer
#시간복잡도 = O(len(moves)*len(board)) = O(n), 공간복잡도 = O(n)
참고
Level 2 튜플 <2019 카카오 인턴십> Python 3
https://programmers.co.kr/learn/courses/30/lessons/64065?language=python3 코딩테스트 연습 - 튜플 "{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{..
hwayomingdlog.tistory.com
- 3. 불량 사용자 -> https://hwayomingdlog.tistory.com/144
Level 3 불량 사용자 <2019 카카오 개발자 겨울 인턴십> Python 3
https://programmers.co.kr/learn/courses/30/lessons/64064?language=python# 코딩테스트 연습 - 불량 사용자 개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는 최근 진행된 카카오이모티콘 이벤트에 비정상..
hwayomingdlog.tistory.com
- 4. 호텔 방 배정 ->
- 5. 징검다리 건너기 -> https://hwayomingdlog.tistory.com/154
Level 3 징검다리 건너기 <2019 카카오 인턴십> Python3
https://programmers.co.kr/learn/courses/30/lessons/64062?language=python3 코딩테스트 연습 - 징검다리 건너기 [2, 4, 5, 3, 2, 1, 4, 2, 5, 1] 3 3 programmers.co.kr 최종 코드 GitHub -> https://github.co..
hwayomingdlog.tistory.com
'코테 노트 > 프로그래머스' 카테고리의 다른 글
Level 2 거리두기 확인하기 <2021 카카오 인턴십> Python 3 (0) | 2021.08.31 |
---|---|
Level 3 불량 사용자 <2019 카카오 인턴십> Python 3 (0) | 2021.08.31 |
Level 3 표 편집 <2021 카카오 인턴십> Python 3 (0) | 2021.08.27 |
Level 1 키패드 누르기 <2020 카카오 인턴십> Python 3 (0) | 2021.08.24 |
Level 3 보석 쇼핑 <2020 카카오 인턴십> Python 3 (0) | 2021.08.23 |