๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ๋ชจ์˜๊ณ ์‚ฌ ์™„์ „ํƒ์ƒ‰ #42840

by ํ‚ค์œค 2023. 12. 27.

#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. ์‚ฌ๋žŒ 1,2,3์ด ์ž‘์„ฑํ•œ ๋‹ต์˜ ํŒจํ„ด์„ p1, p2, p3์— ๋ฐฐ์—ด์— ๋„ฃ์–ด ์ฃผ์—ˆ๋‹ค.
  2. ๊ฐ ์‚ฌ๋žŒ ๋‹น ๋งž์ถ˜ ๋ฌธ์ œ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด mutableMap๋„ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
  3. for๋ฌธ์œผ๋กœ ์‹ค์ œ ๋‹ต์˜ ์ธ๋ฑ์Šค์— ๋งž๊ฒŒ ๋Œ๋ ค์ฃผ์—ˆ๋‹ค.
  4. ๋‹ต์„ ๊ฐ๊ฐ p1, p2 ์™€ p3์˜ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ทธ ๋‚˜๋จธ์ง€์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง„ p1/p2/p3์˜ ๊ฐ’์„ answer[i]์™€ ๋น„๊ตํ•ด ์ฃผ์—ˆ๋‹ค. 
  5. ๊ฐ™๋‹ค๋ฉด mutableMap grade์˜ value ๊ฐ’์„ 1๋งŒํผ์”ฉ ๋”ํ•ด ์ฃผ์—ˆ๋‹ค.
  6. for loop์ด ๋๋‚˜๋ฉด ๋งž์ถ˜ ๊ฐœ์ˆ˜๊ฐ€ 0๊ฐœ์ธ ๊ฐ’์€ ์ง€์›Œ์ค€๋‹ค.
  7. ๊ทธ ํ›„ 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

์–ด๋ ต๋‹ค ์–ด๋ ค์›Œ..