LeetCode/kt/solving-questions-with-brainpower.kt

33 lines
1 KiB
Kotlin

class Solution {
private data class Question(val answered: Long, val skipped: Long) {
companion object {
private fun getWithOffset(
stack: List<Question>,
offset: Long,
): Long =
(stack.size.toLong() - offset - 1).let { i ->
when {
i < 0 || i >= stack.size.toLong() -> 0
else -> stack[i.toInt()].max
}
}
}
val max: Long
get() = maxOf(answered, skipped)
constructor(q: IntArray, stack: List<Question>) : this(
answered = q[0].toLong() + getWithOffset(stack, q[1].toLong()),
skipped = getWithOffset(stack, 0),
) {}
}
fun mostPoints(questions: Array<IntArray>): Long =
mutableListOf<Question>().let { st ->
questions.asIterable().reversed().forEach { q ->
st.add(Question(q, st))
}
st.last().max
}
}