LeetCode Entry
3693. Climbing Stairs II
Min cost c[j]+(j-i)^2 to reach the end
3693. Climbing Stairs II medium substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1538
Problem TLDR
Min cost c[j]+(j-i)^2 to reach the end
Intuition
Attention to the 1-indexed part. Otherwise it is a basic choice problem of the last 3 choices.
Approach
- we can modify c in place
- fold is the shortest
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
fun climbStairs(n: Int, c: IntArray) =
c.fold(List(3){0}){(a,b,d),x->listOf(x+minOf(a+1,b+4,d+9),a,b)}[0]
pub fn climb_stairs(n: i32, c: Vec<i32>) -> i32 {
c.iter().fold([0;3],|[a,b,d],x|[x+(a+1).min(b+4).min(d+9),a,b])[0]
}
Comments