원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/42840?language=kotlin
<첫번째>
class Solution {
fun solution(answers: IntArray): IntArray {
val map: MutableMap<Int, Int> = mutableMapOf(1 to 0, 2 to 0, 3 to 0)
val person1: MutableList<Int> = mutableListOf(1,2,3,4,5)
val person2: MutableList<Int> = mutableListOf(2,1,2,3,2,4,2,5)
val person3: MutableList<Int> = mutableListOf(3,3,1,1,2,2,4,4,5,5)
for ( idx in answers.indices ) {
if ( answers[idx] == person1[idx % 5]) map[1] = map.getValue(1) + 1
if ( answers[idx] == person2[idx % 8]) map[2] = map.getValue(2) + 1
if ( answers[idx] == person3[idx % 10]) map[3] = map.getValue(3) + 1
}
val maxPair: Pair<Int, Int>? = map.toList().maxBy { (_, value) -> value }
val maxInt: Int? = maxPair?.second
val list: MutableList<Int> = mutableListOf()
for (i in 0 until 3)
if(map[i+1] == maxInt) list.add(i+1)
val answer = IntArray(list.size)
for ( i in answer.indices ) {
answer[i] = list[i]
}
return answer
}
}
<두번째> 2020/06/08
class Solution {
data class Person(val idx: Int, var corret: Int): Comparable<Person>{
override fun compareTo(other: Person): Int {
return this.corret - other.corret
}
}
fun solution(answers: IntArray): IntArray {
val list = mutableListOf<Person>(Person(1 ,0), Person(2, 0), Person(3, 0))
val person1 = intArrayOf(1,2,3,4,5)
val person2 = intArrayOf(2,1,2,3,2,4,2,5)
val person3 = intArrayOf(3,3,1,1,2,2,4,4,5,5)
for (i in answers.indices) {
if (answers[i] == person1[i % 5]) list[0].corret += 1
if (answers[i] == person2[i % 8]) list[1].corret += 1
if (answers[i] == person3[i % 10]) list[2].corret += 1
}
return list.filter { it.corret == list.max()!!.corret }
.map { it.idx }
.sorted()
.toIntArray()
}
}
'프로그래머스 > 코딩연습1' 카테고리의 다른 글
[백준] 스택 (10828)(Kotlin) (0) | 2020.01.20 |
---|---|
[프로그래머스] 쇠막대기(42585)(Kotlin) (0) | 2020.01.16 |
[프로그래머스] 탑(42588)(Kotlin) (0) | 2020.01.16 |
[프로그래머스] 체육복(42862)(Kotlin) (0) | 2020.01.16 |
[프로그래머스] 기능개발(42586)(Kotlin) (0) | 2020.01.14 |
댓글