1. Description
A staircase has steps numbered 0 through n. You start at step 0 with no cost.
The array costs has length n, where costs[j - 1] is the landing cost for step j.
Each jump moves forward by 1, 2, or 3 steps. Jumping from step i to step j costs costs[j - 1] + (j - i)$^2$.
Return the minimum total cost to reach step n.
2. Example
Example 1
Input: n = 4, costs = [1,2,3,4]
Output: 13
Explanation: The path 0 → 1 → 2 → 4 costs 2 + 3 + 8 = 13.
Example 2
Input: n = 4, costs = [5,1,6,2]
Output: 11
Explanation: The path 0 → 2 → 4 costs 5 + 6 = 11.
Example 3
Input: n = 3, costs = [9,8,3]
Output: 12
Explanation: Jumping directly from step 0 to step 3 costs 3 + 9 = 12.
3. Constraints
- 1 <= n == costs.length <= 10$^5$
- 1 <= costs[i] <= 10$^4$
4. Solutions
Dynamic Programming
n = costs.size()
Time complexity: O(n)
Space complexity: O(1)
| |