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:
parent
efff2000aa
commit
d9fbfffbe2
1 changed files with 33 additions and 0 deletions
33
kt/solving-questions-with-brainpower.kt
Normal file
33
kt/solving-questions-with-brainpower.kt
Normal 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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue