Skip to content

[우석] 가장 긴 팰린드롬, 다단계 칫솔 판매 #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 최우석/가장 긴 팰린드롬.md
Original file line number Diff line number Diff line change
@@ -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
}
}
```
51 changes: 51 additions & 0 deletions 최우석/다단계 칫솔 판매.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 다단계 칫솔 판매

```kotlin
class Solution {
fun solution(enroll: Array<String>, referral: Array<String>, seller: Array<String>, amount: IntArray): IntArray {
var answer: IntArray = intArrayOf()

val employees = mutableListOf<Employee>()

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)
}
}
}


```