kt: add «2140. Solving Questions With Brainpower»

URL:	https://leetcode.com/problems/solving-questions-with-brainpower/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-04-01 09:38:23 +02:00
parent efff2000aa
commit d9fbfffbe2
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,33 @@
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
}
}