728x90
특이한 정렬
import kotlin.math.abs
class Solution {
fun solution(numlist: IntArray, n: Int): IntArray {
var answer: IntArray = intArrayOf()
var temp: MutableMap<Int, Int> = mutableMapOf()
for (i in 0 until numlist.size) {
temp.set(numlist[i], abs(numlist[i] - n))
}
temp = temp.toList().sortedBy { it.second }.toMap().toMutableMap()
for (i in 0 until temp.size - 1) {
if (temp.toList().get(i).second == temp.toList().get(i + 1).second) {
if (temp.toList().get(i + 1).first > temp.toList().get(i).first) {
answer = answer.plus(temp.toList().get(i + 1).first)
answer = answer.plus(temp.toList().get(i).first)
}else{
answer = answer.plus(temp.toList().get(i).first)
}
} else if (!answer.contains(temp.toList().get(i).first)) {
answer = answer.plus(temp.toList().get(i).first)
}
}
answer = answer.plus(temp.toList().last().first)
return answer
}
}
등수 매기기
class Solution {
fun solution(score: Array<IntArray>): IntArray {
var answer: IntArray = IntArray(score.size)
val temp = score.map { (it.first().toFloat() + it.last())/2 }.toList()
val temp2 = temp.toSet().sortedDescending().toList()
var sum = 0
for (i in 0 until temp2.size){
var count = 0
for (j in 0 until temp.size){
if (temp2[i] == temp[j]){
answer.set(j,i +1 + sum)
count++
}
}
if (count > 1) sum += count - 1
}
return answer
}
}
옹알이 (1)
class Solution {
fun solution(babbling: Array<String>): Int {
val example: Array<String> = arrayOf("aya", "ye", "woo", "ma")
var answer = 0
babbling.forEach {
var canSlice = false
var str = it
while (true){
for (i in example){
if (str.startsWith(i)){
if (i.length != str.length){
str = str.slice(i.length .. str.length-1)
canSlice = true
break;
}
answer++
break
}
}
if (!canSlice) break
canSlice = false
}
}
return answer
}
}
로그인 성공?
class Solution {
fun solution(id_pw: Array<String>, db: Array<Array<String>>): String {
val id = id_pw[0]
val pw = id_pw[1]
var correctId = false
db.forEach {
if(id == it.get(0)){
if (pw == it.get(1))
return "login"
correctId = true
}
}
return if (correctId) "wrong pw" else "fail"
}
}
728x90