diff --git "a/\354\265\234\354\232\260\354\204\235/\352\260\200\354\236\245 \352\270\264 \355\214\260\353\246\260\353\223\234\353\241\254.md" "b/\354\265\234\354\232\260\354\204\235/\352\260\200\354\236\245 \352\270\264 \355\214\260\353\246\260\353\223\234\353\241\254.md" new file mode 100644 index 0000000..48cd449 --- /dev/null +++ "b/\354\265\234\354\232\260\354\204\235/\352\260\200\354\236\245 \352\270\264 \355\214\260\353\246\260\353\223\234\353\241\254.md" @@ -0,0 +1,50 @@ +# 가장 긴 팰린드롬 + +```kotlin +import kotlin.math.* + +class Solution { + fun solution(s: String): Int { + var answer = 0 + + for ((i, c) in s.withIndex()) { + answer = max(answer, palindrome(s, i, c)) + } + + return answer + } + + fun palindrome(s: String, i: Int, c: Char): Int { + var t = 1 + + var twinLength = 1 + while (0 <= i - t && i + t < s.length && s[i - t] == s[i + t]) { + t += 1 + twinLength += 2 + } + + t = 1 + var rightLength = 1 + while (i + t < s.length && s[i + t] == c) { + t += 1 + rightLength += 1 + } + + var result = max(twinLength, rightLength) + + var twinLength2 = 2 + if (i + t < s.length && s[i + 1] == c) { + t = 1 + var l_i = i + var r_i = i + 1 + while (0 <= l_i - t && r_i + t < s.length && s[l_i - t] == s[r_i + t]) { + t += 1 + twinLength2 += 2 + } + result = max(result, twinLength2) + } + + return result + } +} +``` \ No newline at end of file diff --git "a/\354\265\234\354\232\260\354\204\235/\353\213\244\353\213\250\352\263\204 \354\271\253\354\206\224 \355\214\220\353\247\244.md" "b/\354\265\234\354\232\260\354\204\235/\353\213\244\353\213\250\352\263\204 \354\271\253\354\206\224 \355\214\220\353\247\244.md" new file mode 100644 index 0000000..015ef15 --- /dev/null +++ "b/\354\265\234\354\232\260\354\204\235/\353\213\244\353\213\250\352\263\204 \354\271\253\354\206\224 \355\214\220\353\247\244.md" @@ -0,0 +1,51 @@ +# 다단계 칫솔 판매 + +```kotlin +class Solution { + fun solution(enroll: Array, referral: Array, seller: Array, amount: IntArray): IntArray { + var answer: IntArray = intArrayOf() + + val employees = mutableListOf() + + for (i in 0 until enroll.size) { + val referralEmployee = employees.find { it.name == referral[i] } + if (referralEmployee == null) { + employees.add(Employee(enroll[i], null)) + continue + } + employees.add(Employee(enroll[i], referralEmployee)) + } + + for (i in 0 until seller.size) { + val employee = employees.find { it.name == seller[i] } + employee!!.sell(amount[i] * 100) + } + + return employees.map { it.profit }.toIntArray() + } +} + +class Employee( + val name: String, + val referralEmployee: Employee?, + var profit: Int = 0 +) { + + fun sell(profit: Int) { + val interest = profit / 10 + + if (interest == 0) { + this.profit += profit + return + } + + this.profit += profit - interest + + if (referralEmployee != null) { + referralEmployee.sell(interest) + } + } +} + + +``` \ No newline at end of file