O(1) space complexity O(n) time complexity Java simple solution


  • 0
    F
    public int climbStairs(int n) {
        //p1: number of ways to reach i - 1
        //p2: number of ways to reach i - 2
        //cur number of ways to reach i
        int p1 = 1, p2 = 0, cur = 0;
        
        for (int i = 0; i < n; i++){
            cur = p1 + p2;
            p2 = p1;
            p1 = cur;
        }
        return cur;
    }

Log in to reply
 

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