func solution(_ name: String) -> Int {
let name = Array(name)
let aValue = Int(Character("A").asciiValue!)
let zValue = Int(Character("Z").asciiValue!)
var upDown = 0
var leftRight = name.count-1
for startIdx in 0..<name.count {
upDown += min(Int(name[startIdx].asciiValue!) - aValue,
zValue - Int(name[startIdx].asciiValue!) + 1)
var endIdx = startIdx + 1
while endIdx < name.count && name[endIdx] == "A" {
endIdx += 1
}
let moveFront = startIdx * 2 + (name.count - endIdx)
let moveBack = (name.count - endIdx) * 2 + startIdx
leftRight = min(leftRight, moveFront)
leftRight = min(leftRight, moveBack)
}
return upDown + leftRight
}
[Greedy] 조이스틱
bromp • 2023년 1월 18일 PM 02:59 • 17 • 1
댓글
첫 번째 댓글을 남겨보세요 :)