I am confused by the reference answer of climbing stairs in leetcode.
Here is the problem:
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
var climbStairs = function(n) {
if (n < 1) return 0;
if (n == 1) return 1;
if (n == 2) return 2;
// a saves the second-to-last sub-state data, b saves the first sub-state data, temp saves the current state data
let a = 1, b = 2;
let temp = a + b;
for (let i = 3; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return temp;
};
The answer uses DP to solve, but I cannot understand how the for loop works,I think I missed some javascript characteristics.