70. Climbing Stairs
View on LeetCode ↗
C++
class Solution {public: int climbStairs(int n) { std::vector<int> steps(max(n + 1, 3)); steps[0] = 0; steps[1] = 1; steps[2] = 2; for (int i = 3; i <= n; i++) { steps[i] = steps[i - 1] + steps[i - 2]; } return steps[n]; }};
Beginner DP problem just setup my memo vector, filled in the slots
most important part was steps[i] = steps[i-1] + steps[i-2]; There I realized that DP is really just about breaking down each problem into its smaller problems. since there are only two possible ways to get to the solution for the i-th element of steps. so get the sum of the previous two solutions to get to the solution you want. Base cases also get satisfied from indexes 0 1 and 2. Iterate through entire thing and get to end.
Ik this is really unnecessary for this solution however. Could just have a running two numbers in a loop and save on memory.