728x90
반응형

Youtube 풀이
Problem
코딩테스트 연습 - 크레인 인형뽑기 게임
[[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
Solution
board를 각 index에 있는 숫자들로 구성해서 array 배열을 만들어준다.
array에서 0은 없애줍니다.
뽑은 숫자들을 담을 basket 배열을 만들어줍니다.
moves안에 있는 숫자들은 array index이므로 array의 index번째에서 첫번째 숫자를 제거해줍니다.
array의 맨 첫번째 숫자와 basket의 맨 마지막 숫자를 비교하여
만약 같다면 basket의 맨 마지막 숫자를 제거해주고 count를 +2 해줍니다.
Source Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func solution(_ board:[[Int]], _ moves:[Int]) -> Int { | |
var array = [[Int]]() | |
var basket = [Int]() | |
var count = 0 | |
for i in 0..<board.count { | |
array.append(board.map{$0[i]}.filter{$0 != 0}.compactMap{$0}) | |
} | |
for n in moves { | |
if !array[n-1].isEmpty{ | |
if !basket.isEmpty { | |
if array[n-1].first == basket.last { | |
array[n-1].removeFirst() | |
basket.removeLast() | |
count += 2 | |
continue | |
} | |
} | |
basket.append(array[n-1].removeFirst()) | |
} | |
} | |
return count | |
} |
728x90
반응형
'📖 Problem Solution > Programmers' 카테고리의 다른 글
[Swift] 프로그래머스 월간 코드 챌린지 시즌1 풍선 터뜨리기 (0) | 2021.04.04 |
---|---|
[Swift] 2021 KAKO BLIND RECUITMENT 합승 택시 요금 (0) | 2021.03.27 |
[Swift] 2021 KAKAO BLIND RECRUITMENT 순위 검색 (0) | 2021.03.18 |
[Swift] 2021 KAKAO BLIND RECRUITMENT 메뉴 리뉴얼(Youtube 풀이 포함) (0) | 2021.03.13 |
[Swift] 2021 KAKAO BLIND RECRUITMENT 신규 아이디 추천 (3) | 2021.03.07 |
댓글