
코드카타 문자열 나누기 문제 입출력 예시 정답class Solution { fun solution(s: String): Int { var rs = s // 남은 문자열 var res = 0 // 분배한 문자열의 개수 // 남은 문자열이 공백이 될 때까지 문자열 분배 과정 반복 while (rs.isNotEmpty()) { // 변수 초기화 구문 var x = rs[0] // 첫 글자 var xCnt = 0 // x와 같은 글자가 나온 횟수 var nCnt = 0 // x와 다른 글자가 나온 횟수 va..

코드카타 옹알이 (2) 문제 정답class Solution { fun solution(babbling: Array): Int { val able = listOf("aya", "ye", "woo", "ma") return babbling.count { i -> var word = i for (a in able) { if (!word.contains(a.repeat(2))) { word = word.replace(a, " ") } } word.all { it.isWhitespace() } } }..

코드카타 로또의 최고 순위와 최저 순위 문제 정답class Solution { fun solution(lottos: IntArray, win_nums: IntArray): IntArray { val m = lottos.count { it in win_nums } val z = lottos.count { it == 0 } fun getRank(m: Int): Int { return when (m) { 6 -> 1 5 -> 2 4 -> 3 3 -> 4 2 -> 5 else -> 6 ..

코드카타 기사단원의 무기 문제 정답class Solution { fun solution(number: Int, limit: Int, power: Int): Int { fun findDiv(n: Int): Int { val s = Math.sqrt(n.toDouble()).toInt() return (1..s).count { n % it == 0 } * 2 - (if (s * s == n) 1 else 0) } var divArr = IntArray(number) { findDiv(it + 1) } divArr.forEachIndexed { i, v -> if (v > limit) divArr..

코드카타 덧칠하기 문제 정답class Solution { fun solution(n: Int, m: Int, section: IntArray): Int { var pos = 0 var res = 0 for (i in section.indices) { if (pos 해당 문제는 그리디 알고리즘을 통해 해결할 수 있다. var pos = 0var res = 0pos는 페인트를 칠하기 시작하는 현재 위치,res는 페인트를 칠해야 되는 최소 횟수에 해당한다. for (i in section.indices) { if (pos 페인트를 칠해야 되는 구역을 순회하며 롤러의 위치가 해당 구역보다 뒤에 위치한다면 롤러의 위치를 해당구역에서 페인트를 칠..

코드카타 모의고사 문제 정답class Solution { fun solution(answers: IntArray): IntArray { val patterns = listOf( listOf(1, 2, 3, 4, 5), listOf(2, 1, 2, 3, 2, 4, 2, 5), listOf(3, 3, 1, 1, 2, 2, 4, 4, 5, 5) ) val scores = IntArray(3) for ((i, answer) in answers.withIndex()) { for ((j, pattern) in patterns.withIndex()) { ..

코드카타 카드 뭉치 문제 정답class Solution { fun solution(cards1: Array, cards2: Array, goal: Array): String { var a = 0 var b = 0 for (i in goal) { when { a a++ b b++ else -> return "No" } } return "Yes" }}각 단어를 goal배열에서 순차적으로 확인하며 두 카드 뭉치에서 차례대로 단어를 비교해준다. 카드를 순차적으로 배치하여 goal을 만들 수 있다면 "Yes"를,그렇지..