Skip to content

Instantly share code, notes, and snippets.

@cg4jins
Last active June 27, 2019 02:54
Show Gist options
  • Save cg4jins/9dea2300efbefe79828eddf84dd09960 to your computer and use it in GitHub Desktop.
Save cg4jins/9dea2300efbefe79828eddf84dd09960 to your computer and use it in GitHub Desktop.
class Solution {
fun solution(answers: IntArray): IntArray {
val size = answers.size
val oneReport = oneReport(size)
val twoReport = twoReport(size)
val threeReport = threeReport(size)
val scoreOfOne = scoreCorrect(oneReport, answers)
val scoreOfTwo = scoreCorrect(twoReport, answers)
val scoreOfThree = scoreCorrect(threeReport, answers)
return findMaxScorePersons(scoreOfOne, scoreOfTwo, scoreOfThree)
}
private fun findMaxScorePersons(scoreOfOne: Int, scoreOfTwo: Int, scoreOfThree: Int): IntArray {
val list = mutableListOf(scoreOfOne to 1, scoreOfTwo to 2, scoreOfThree to 3)
list.sortByDescending { pair -> pair.first }
var ret = mutableListOf<Int>()
var previousScore = 0
for (i in 0 until list.size) {
if (list[i].first == 0){
break
}
if (ret.size == 0) {
ret.add(list[i].second)
}
if (previousScore == list[i].first) {
ret.add(list[i].second)
}
previousScore = list[i].first
}
return ret.toIntArray()
}
private fun scoreCorrect(oneReport: IntArray, answers: IntArray): Int {
var ret = 0
for (answer in answers.withIndex()) {
if (answer.value == oneReport[answer.index]) {
ret++
}
}
return ret
}
private fun threeReport(size: Int): IntArray {
var ret = Array(size) { 0 }
for (i in 0 until size) {
when {
i % 10 < 2 -> ret[i] = 3
i % 10 < 4 -> ret[i] = 1
i % 10 < 6 -> ret[i] = 2
i % 10 < 8 -> ret[i] = 4
i % 10 < 10 -> ret[i] = 5
}
}
return ret.toIntArray()
}
private fun twoReport(size: Int): IntArray {
var ret = Array(size) { 0 }
for (i in 0 until size) {
if (i % 2 == 0) {
ret[i] = 2
} else {
when {
i % 8 == 1 -> ret[i] = 1
i % 8 == 3 -> ret[i] = 3
i % 8 == 5 -> ret[i] = 4
i % 8 == 7 -> ret[i] = 5
}
}
}
return ret.toIntArray()
}
private fun oneReport(size: Int): IntArray {
var ret = Array(size) { 0 }
for (i in 0 until size) {
ret[i] = (i % 5) + 1
}
return ret.toIntArray()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment