Skip to content

Latest commit

 

History

History
46 lines (30 loc) · 629 Bytes

File metadata and controls

46 lines (30 loc) · 629 Bytes

Climbing Stairs

Problem Link

https://leetcode.com/problems/climbing-stairs/


Pattern

  • 1D DP

Approach

Each step can be reached from the previous one or the one before that, so compute the Fibonacci-style recurrence.


Time Complexity

O(n)

Space Complexity

O(1)


Java Solution

class Solution {
    public int climbStairs(int n) {
        if (n <= 2) return n;
        int first = 1, second = 2;
        for (int i = 3; i <= n; i++) {
            int current = first + second;
            first = second;
            second = current;
        }
        return second;
    }
}