-
[Programmers] Lv1.๋ชจ์๊ณ ์ฌ(kotlin)SW Test/Programmers 2022. 6. 4. 14:56๋ฐ์ํ
๋ฌธ์
์์
ํ์ด
1. numarr ๋ฐฐ์ด์ 1๋ฒ, 2๋ฒ, 3๋ฒ ์ํฌ์์ ์ฐ๋ ๋ฐฉ์์ ์ ์ฅํฉ๋๋ค.
2. ์ํฌ์๊ฐ ์ฐ๋ ๋ฐฉ์์ ์ผ์ ํจํด์ผ๋ก ๋ฐ๋ณต๋๊ธฐ์ answers์ ๊ฐฏ์์ numarr[i]์ ๊ฐฏ์๊ฐ ๋ค๋ฅด๋๋ผ๋ ๋๋จธ์ง ์ฐ์ฐ์๋ฅผ ์ด์ฉํ๋ฉด ๋น๊ตํ ์ ์์ต๋๋ค.
3. ์ต๋ ๋์ ์ ์๋ฅผ ๋ฐ์ ๋ฒํธ๋ฅผ ์ฒดํฌํ๊ณ ๋ฐํํฉ๋๋ค.
import kotlin.math.* class Solution { fun solution(answers: IntArray): IntArray { var answer = intArrayOf(0,0,0) val numarr = arrayOf( arrayOf(1,2,3,4,5), arrayOf(2,1,2,3,2,4,2,5), arrayOf(3,3,1,1,2,2,4,4,5,5) ) for(i in 0..2){ var score = 0 for(j in 0 until answers.size){ score += if(numarr[i][j%numarr[i].size] == answers[j]) 1 else 0 } answer[i] = score } return answer.mapIndexed{ index, i -> if(i == answer.maxOf{it}){ index+1 }else{ -1 } }.filter{ it != -1}.toIntArray() } }
์ฐธ๊ณ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ธ kotlin์ ํน์ฑ์ ๋ง๊ฒ ํจ์๋ฅผ ๋ง์ด ์ตํ์ ๐ฝ
- https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-indexed.html
๋ฐ์ํ'SW Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv1.์์ ๋ง๋ค๊ธฐ(kotlin) (0) 2022.07.12 [Programmers] Lv1. K๋ฒ์งธ ์(kotlin) (0) 2022.07.09 [Programmers] Lv1.์ฒด์ก๋ณต(kotlin) (0) 2022.06.02 [Programmers] Lv1. 2019 KAKAO BLIND RECUITMENT ์คํจ์จ(kotlin) (0) 2022.05.26 [Programmers] Lv1.N^2 ๋ฐฐ์ด ์๋ฅด๊ธฐ(Kotlin) (0) 2021.11.15