๋ฌธ์
์์
ํ์ด
- ๊ธฐ๋ณธ์ ์ผ๋ก๋ ๋ฌธ์ ์์ ์๊ตฌํ๋๋ก ์ผ์๊ณผ ์ค๋ฅธ์์ผ๋ก ์
๋ ฅํ ์ซ์๋ก ๋ถ๊ธฐ์ฒ๋ฆฌ ํฉ๋๋ค.
- 2,5,8,0 ์์๋ ์ผ์๊ณผ ์ค๋ฅธ์์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ธฐ ์ํด์ ์ขํ๋ก ํํํฉ๋๋ค. ํ๋์ ์ขํ๋ 3์ผ๋ก ๋๋ ์ซ์์ธ num/3, ๋ค๋ฅธ ํ๋๋ 3์ผ๋ก ๋๋ ๋๋จธ์ง์ธ num%3 ์
๋๋ค.
- ๊ฐ ์ขํ๋ก ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ ํ ๊ฐ์ ๊ฑฐ๋ฆฌ์ผ ๊ฒฝ์ฐ์ hand ๊ฐ์ ๋ฐ๋ผ ๋ถ๊ธฐ์ฒ๋ฆฌ ํ๋ฉด ๋ฉ๋๋ค.
import kotlin.math.*
class Solution {
fun solution(numbers: IntArray, hand: String): String {
var answer = ""
var left = 9
var right = 11
numbers.map{ if(it == 0) 10 else it - 1}.forEach{ num ->
if(num in arrayOf(0,3,6)){
answer = answer + 'L'
left = num
}else if(num in arrayOf(2,5,8)){
answer = answer + 'R'
right = num
}else{
val lrd = abs(left/3 - num/3)
val lcd = abs(left%3 - num%3)
val rrd = abs(right/3 - num/3)
val rcd = abs(right%3 - num%3)
if(lrd + lcd < rrd + rcd){
answer = answer + 'L'
left = num
}else if(lrd + lcd > rrd + rcd){
answer = answer + 'R'
right = num
}else{
if(hand == "right"){
answer = answer + 'R'
right = num
}else{
answer = answer + 'L'
left = num
}
}
}
}
return answer
}
}
์ฐธ๊ณ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ธ kotlin์ ํน์ฑ์ ๋ง๊ฒ ํจ์๋ฅผ ๋ง์ด ์ตํ์ ๐ฝ