카테고리 없음
[programmers/Swift] 크레인 인형뽑기 게임 (2019 카카오 개발자 겨울 인턴쉽)
Marco
2022. 5. 8. 21:46
728x90
programmers.co.kr - 코딩테스트연습 - Lv.1 - 2019 카카오 개발자 겨울 인턴쉽 - 크레인 인형뽑기 게임
Constraints :
- 2 ≤ id_list의 길이 ≤ 1,000
//
// Created by Yongwoo Marco on 2022/05/06.
// Copyright © 2022 Yongwoo Marco Kim. All rights reserved.
//
func solution(_ board:[[Int]], _ moves:[Int]) -> Int {
var verticalBoard = [[Int]](repeating: [], count: board.count)
var stack = [Int](), result = 0
for horizontal in board {
for item in horizontal.enumerated() {
if item.element != 0 {
verticalBoard[item.offset].append(item.element)
}
}
}
for move in moves {
let index = move - 1
if !verticalBoard[index].isEmpty {
let removeFirst = verticalBoard[index].removeFirst()
if let last = stack.last, last == removeFirst {
stack.removeLast()
result += 2
} else {
stack.append(removeFirst)
}
}
}
return result
}
print(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
How I tried this :
다행히 내 구현도 통과는 되었다..
살짝 고민한점이 내 구현은 일단 board 크기는 반드시 돈다.
N * N 크기를 첫 for 문을 이용해서 방문해서 새로운 mutable 큐를 만들고
moves for문 moves.count 만큼 더 돌면 답이 나온다.
만약 한번에 접근을 한다면 최악의 경우가 moves.count * N 이라 방문수는 내 코드보다 적다..
그래서 시간초과를 걱정했는데 그래서 Lv 1 인가보다 했다..
문제에 관한 모든 저작권 : https://programmers.co.kr
728x90