Last active
June 27, 2019 02:54
-
-
Save cg4jins/9dea2300efbefe79828eddf84dd09960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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