Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Week2/70_Climbing_Stairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int climbStairs(int n) {
// We will use dp approach here.
// To the i th level we can get either from i-1 or i-2 level, thus ways to get to ith is the sum of mentioned 2.
// Initial state: 1 way to get to the 1st level, 1 way of getting to 0th level.
// It is Fibonacci sequence.

int curLevelWaysCount = 1, prevLevelWaysCount = 1;
for (int level = 2; level <= n; ++level) {
curLevelWaysCount += prevLevelWaysCount;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not readable/clear. Use tmp variable to store the sum.

prevLevelWaysCount = curLevelWaysCount - prevLevelWaysCount;
}
return curLevelWaysCount;
}
};

/*
n - number of stair levels.
Time Complexity T = O(n).
Memory complexity M = O(n).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that M=O(n).


*/