ํคํจ๋ ๋๋ฅด๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift)
2022. 10. 3. 16:35ใํ๋ก๊ทธ๋๋จธ์ค-Swift
ํคํจ๋ ๋๋ฅด๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift)
๋ฌธ์ ์ค๋ช
https://school.programmers.co.kr/learn/courses/30/lessons/67256?language=swift
๋ค๋ฅธ ์ฌ๋์ ํ์ด
ํคํจ๋๋ฅผ ์ขํ๊ฐ์ผ๋ก ๋๊ณ ํ๊ณ ์๋๋ฐ ๊ฐ์ด ์ ๋๋ ๋ถ๋ถ์ ์ฒ๋ฆฌํ์ง ๋ชปํด์ ์ฐธ๊ณ ํ๋ค.
https://jeong9216.tistory.com/222
๊ฐ์ ์๊ฐ์ผ๋ก ์ ๊ทผํ์ง๋ง ๋๋ ํด๊ฒฐํ์ง ๋ชปํ๊ธฐ์ ๊ตฌํ๋ฅ๋ ฅ์ด ์์ง ๋ง์ด ๋ถ์กฑํ ๊ฒ ๊ฐ๋ค. (..๋ ธ๋ ฅํ์!)
import Foundation
func solution(_ numbers:[Int], _ hand:String) -> String {
func setupCursor(_ num: Int) -> (Int, Int) {
var cursor = (0, 0)
switch num {
case 0: cursor = (1, 0)
case 1: cursor = (0, 3)
case 2: cursor = (1, 3)
case 3: cursor = (2, 3)
case 4: cursor = (0, 2)
case 5: cursor = (1, 2)
case 6: cursor = (2, 2)
case 7: cursor = (0, 1)
case 8: cursor = (1, 1)
case 9: cursor = (2, 1)
default: break
}
return cursor
}
func addString(_ num: Int, _ leftDistance: Int, _ rightDistance: Int) -> String {
switch num {
case 1, 4, 7: return "L"
case 3, 6, 9: return "R"
case 2, 5, 8, 0:
if leftDistance > rightDistance {
return "R"
} else if leftDistance < rightDistance {
return "L"
} else {
return hand == "left" ? "L" : "R"
}
default: break
}
return ""
}
var leftCursor = (0, 0)
var rightCursor = (2, 0)
var result = ""
for num in numbers {
let cursor = setupCursor(num)
let leftDistance = abs(cursor.0-leftCursor.0) + abs(cursor.1-leftCursor.1)
let rightDistance = abs(cursor.0-rightCursor.0) + abs(cursor.1-rightCursor.1)
let str = addString(num, leftDistance, rightDistance)
result += str
if str == "L" {
leftCursor = cursor
} else {
rightCursor = cursor
}
}
return result
}
'ํ๋ก๊ทธ๋๋จธ์ค-Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฑ๊ฒฉ์ ํ ๊ฒ์ฌํ๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (1) | 2022.10.05 |
---|---|
ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (0) | 2022.10.04 |
ํ๋ก๊ทธ๋๋จธ์ค - ์ฒด์ก๋ณต(Swift) (0) | 2022.10.02 |
๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (0) | 2022.10.02 |
ํ๋ก๊ทธ๋๋จธ์ค - ๋คํธ๊ฒ์(Swift) (0) | 2022.10.01 |