Descriptive Java Solution 0ms


  • 0
    M

    This is similar to the Fibonacci solution. At any step, the steps needed is the sum of getting to one step below and two steps below. If you've reached the top of the stairs, then you've successfully found a number of paths equal to the amount of paths to one steps below plus the amount of paths to two steps below.

    public class Solution {
        public int climbStairs(int n) {
            if (n <= 1) {
                return 1; 
            }
            
            int rewindTwo = 1;
            int rewindOne = 2;
            int count = 2;
            
            for (int i = 2; i < n; i++) {
                count = rewindTwo + rewindOne;
                rewindTwo = rewindOne;
                rewindOne = count;
            }
            
            return count;
        }
    }
    

Log in to reply
 

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