Yet another C solution; 0ms and O(1) space

  • 0
    int climbStairs(int n) {
      //if(n == 0) return 0;
      int w[2]; // last two iteration of dynamic programming for uniq ways
      int idx = 0;
      w[0] = w[1] = 1; //1 way to both 0th step and 1st step
      for(int i =2; i <=n; i++)
        w[idx] = w[idx] + w[idx^1];
        idx^=1; //toggle index to make way(x) and way(x-1) becomes way(x-1) and way(x-2) respectively
      return w[idx^1];

Log in to reply

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