C++ Dynamic Programming O(N) Time O(1) Space


  • 0
    M
    class Solution {
    public:
        int climbStairs(int n) {
            if (n <= 2) return n;
            int s0 = 0, s1 = 2, s2 = 1;
            for (int i = 2; i < n; ++i) {
                s0 = s1 + s2;
                s2 = s1;
                s1 = s0;
            }
            return s0;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.