Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 오픈채팅방
- SwiftUI
- RaceCondition
- SeSAC
- 코테
- rxswift
- Realm
- MainScheduler.Instance
- GCD
- cleanarchitecture
- GIT
- 명품cppProgramming c++
- MainScheduler.asyncInstance
- 등굣길
- IOS
- DependencyInjection
- combine
- MainScheduler
- MethodSwilzzling
- 청년취업사관학교
- SRP
- swift
- DispatchQueue
- gitflow
- data_structure
- DynamicMemberLookup
- DiffableDataSource
- leetcode
- CoreBluetooth
- 프로그래머스
Archives
- Today
- Total
Do.
2022 KAKAO BLIND RECRUITMENT - 양궁대회 본문
https://programmers.co.kr/learn/courses/30/lessons/92342
- 너무 무식하게 접근했는데, 다른 사람 풀이보고 공부해야 겠다.
- 기본적으로 bfs 접근 방식이니, 라이언의 info를 set형태로 visited를 체크하면 속도는 좀 올라갈듯.
import Foundation
let scoreBoard = [10,9,8,7,6,5,4,3,2,1,0]
struct Queue {
var leftStack: [Game] = []
var rightStack: [Game] = []
var appeachInfo: [Int] = []
var maxScoreGame: [Int] = [-1]
var maxScore: Int = -1
var count: Int {
leftStack.count + rightStack.count
}
mutating func enqueue(_ element: Game) {
leftStack.append(element)
maxScoreCalculating(element.info)
}
mutating private func maxScoreCalculating(_ lyonGame: [Int]) {
let (appeahScore, lyonScore) = scoreCalculating(appeachInfo, lyonGame)
let scoreDifference = lyonScore - appeahScore
if scoreDifference > 0,
maxScore < scoreDifference {
maxScore = scoreDifference
maxScoreGame = lyonGame
} else if scoreDifference > 0, maxScore == scoreDifference {
for (previous, new) in zip(maxScoreGame, lyonGame).reversed() {
if new > previous {
maxScoreGame = lyonGame
return
} else if new < previous {
return
}
}
}
}
mutating func dequeue() -> Game? {
if rightStack.isEmpty {
rightStack = leftStack.reversed()
leftStack.removeAll()
}
return rightStack.popLast()
}
func scoreCalculating(_ appeach: [Int], _ lyon: [Int]) -> (appeach: Int, lyon: Int) {
var appeachTotalScore = 0
var lyonTotalScore = 0
for (index, (appeachArrow, lyonArrow)) in zip(appeach, lyon).enumerated() {
if appeachArrow != 0 && appeachArrow >= lyonArrow {
appeachTotalScore += scoreBoard[index]
} else if lyonArrow != 0 {
lyonTotalScore += scoreBoard[index]
}
}
return (appeachTotalScore, lyonTotalScore)
}
}
struct Game {
var remainArrow: Int
var info: [Int] = .init(repeating: 0, count: 11)
var index: Int = 0
mutating func shot(_ amountOfArrow: Int) {
defer {
index += 1
}
guard remainArrow >= amountOfArrow,
amountOfArrow > 0 else {
info[index] = index == 10 ? remainArrow : 0
return
}
info[index] = amountOfArrow
remainArrow -= amountOfArrow
return
}
init(remainArrow: Int) {
self.remainArrow = remainArrow
}
init(game: Game) {
self.remainArrow = game.remainArrow
self.info = game.info
self.index = game.index
}
}
func solution(_ n:Int, _ info:[Int]) -> [Int] {
var queue = Queue()
queue.appeachInfo = info
queue.enqueue(Game(remainArrow: n))
for arrow in info {
for _ in 0..<queue.count {
guard let element = queue.dequeue() else { break }
var shot = Game(game: element)
var dontShot = Game(game: element)
shot.shot(arrow + 1)
dontShot.shot(0)
queue.enqueue(shot)
queue.enqueue(dontShot)
}
}
return queue.maxScoreGame
}
'Algorithm' 카테고리의 다른 글
BOJ1158 - 요세푸스 문제, Swift (0) | 2022.03.25 |
---|---|
프로그래머스 - 등굣길, Cpp (0) | 2022.03.22 |
2022 KAKAO BLIND RECRUITMENT - k진수에서 소수 개수 구하기, Swift (0) | 2022.03.15 |
2022 KAKAO BLIND RECRUITMENT - 주차요금 계산하기, Swift (0) | 2022.03.14 |
2019 KAKAO BLIND RECRUITMENT - 오픈 채팅방, Swift (0) | 2022.03.13 |
Comments