#42840
๐ Question ?
https://school.programmers.co.kr/learn/courses/30/lessons/138477
class Solution {
fun solution(k: Int, score: IntArray): IntArray {
var answer: IntArray = intArrayOf()
return answer
}
}
๐งฉ Thought Process
- ์ฌ๋ 1,2,3์ด ์์ฑํ ๋ต์ ํจํด์ p1, p2, p3์ ๋ฐฐ์ด์ ๋ฃ์ด ์ฃผ์๋ค.
- ๊ฐ ์ฌ๋ ๋น ๋ง์ถ ๋ฌธ์ ์๋ฅผ ์ ์ฅํ๊ธฐ ์ํด mutableMap๋ ๋ง๋ค์ด์ฃผ์๋ค.
- for๋ฌธ์ผ๋ก ์ค์ ๋ต์ ์ธ๋ฑ์ค์ ๋ง๊ฒ ๋๋ ค์ฃผ์๋ค.
- ๋ต์ ๊ฐ๊ฐ p1, p2 ์ p3์ ํฌ๊ธฐ๋ก ๋๋๊ณ ๊ทธ ๋๋จธ์ง์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง p1/p2/p3์ ๊ฐ์ answer[i]์ ๋น๊ตํด ์ฃผ์๋ค.
- ๊ฐ๋ค๋ฉด mutableMap grade์ value ๊ฐ์ 1๋งํผ์ฉ ๋ํด ์ฃผ์๋ค.
- for loop์ด ๋๋๋ฉด ๋ง์ถ ๊ฐ์๊ฐ 0๊ฐ์ธ ๊ฐ์ ์ง์์ค๋ค.
- ๊ทธ ํ value๊ฐ์ ๋ง๋๋ก grade ๋งต์ sortํด์ฃผ๊ณ key ๊ฐ์ array๋ก ์ถ๋ ฅํด์ค๋ค.
import kotlin.math.*
class Solution {
fun solution(answers: IntArray): IntArray {
var answer = intArrayOf()
var p1 = intArrayOf(1, 2, 3, 4, 5)
var p2 = intArrayOf(2, 1, 2, 3, 2, 4, 2, 5)
var p3 = intArrayOf(3, 3, 1, 1, 2, 2, 4, 4 ,5, 5)
var grade = mutableMapOf(1 to 0,2 to 0,3 to 0)
for (i in answers.indices) {
when (answers[i]) {
p1[i%p1.size] -> grade[1] = grade[1]!! +1
p2[i%p2.size] -> grade[2] = grade[2]!! +1
p3[i%p3.size] -> grade[3] = grade[3]!! +1
}
}
for (j in 1..3) {
if (grade[j] == 0) {
grade.remove(j)
}
}
grade.toList().sortedBy{ (key, value) -> value }.toMap()
return grade.keys.toIntArray()
}
}
๐ Answer
๋ค๋ฅธ ์ฌ๋ ํ์ด #1
import java.util.*
internal class Solution {
fun solution(answers: IntArray): IntArray {
val score = IntArray(3)
val a = intArrayOf(1, 2, 3, 4, 5)
val b = intArrayOf(2, 1, 2, 3, 2, 4, 2, 5)
val c = intArrayOf(3, 3, 1, 1, 2, 2, 4, 4, 5, 5)
for (i in answers.indices) {
if (answers[i] == a[i % a.size]) score[0]++
if (answers[i] == b[i % b.size]) score[1]++
if (answers[i] == c[i % c.size]) score[2]++
}
val max = score[0].coerceAtLeast(score[1].coerceAtLeast(score[2]))
val list: MutableList<Int> = ArrayList()
if (max == score[0]) {
list.add(1)
}
if (max == score[1]) {
list.add(2)
}
if (max == score[2]) {
list.add(3)
}
val answer = IntArray(list.size)
for (i in list.indices) {
answer[i] = list[i]
}
return answer
}
}
class Solution {
fun solution(answers: IntArray): IntArray {
val userAnswers = arrayOf(
intArrayOf(1,2,3,4,5), intArrayOf(2,1,2,3,2,4,2,5), intArrayOf(3,3,1,1,2,2,4,4,5,5))
val cnt = IntArray(3)
val answer = mutableListOf<Int>()
userAnswers.forEachIndexed { i, userAns ->
cnt[i] = answers.filterIndexed { j, ans -> ans == userAns[j % userAns.size] }.count()
}
cnt.forEachIndexed{ idx, i -> if(cnt.max() == i) answer.add(idx + 1) }
return answer.toIntArray()
}
}
์ถ์ฒ: https://yline.tistory.com/107 [Y_LINE's_Repository:ํฐ์คํ ๋ฆฌ]
๐ Result
๐ Comment
์ด๋ ต๋ค ์ด๋ ค์..
'Algorithm > Kotlin' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ๋ง์น ํ๊ธฐ #161989 (2) | 2024.01.05 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] Generating Prime Number #12977 (0) | 2023.12.29 |
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] 2016๋ #12901 (0) | 2023.12.19 |
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ์นด๋ ๋ญ์น #159994 (0) | 2023.12.19 |
[ํ๋ก๊ทธ๋๋จธ์ค โข ์ฝํ๋ฆฐ] ๋ช ์์ ์ ๋น(1) #138477 (0) | 2023.12.14 |