문제: https://programmers.co.kr/learn/courses/30/lessons/64061
코딩테스트 연습 - 크레인 인형뽑기 게임
[[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
배열 좌표를 잘 생각해보면 쉽게 풀 수 있습니당
import Foundation
func solution(_ board:[[Int]], _ moves:[Int]) -> Int {
var board = board
var queue = [Int]()
var result = 0
func addDoll(doll: Int) {
if queue.last == doll {
result += 2
queue.removeLast()
} else {
queue.append(doll)
}
}
for move in moves {
for i in 0..<board.count {
if board[i][move - 1] == 0 { continue }
// print(move, board[i][move - 1])
addDoll(doll: board[i][move - 1])
board[i][move - 1] = 0
break
}
}
return result
}
solution([[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
'알고리즘' 카테고리의 다른 글
[Swift] 프로그래머스 - 카카오 뉴스 클러스터링 (0) | 2022.02.10 |
---|---|
[Swift] 프로그래머스 - 짝지어 제거하기 (0) | 2022.02.10 |
[Swift] 프로그래머스 - 카카오 오픈채팅방 (0) | 2022.02.10 |
[Swift] 프로그래머스 - 카카오 신고 결과 받기 (0) | 2022.02.10 |
[Swift] 프로그래머스 - 로또의 최고 순위와 최저 순위 (0) | 2022.02.10 |
댓글