**A Flight of Stairs - Amazon Top Interview Questions**

### Problem Statement :

There's a staircase with n steps, and you can climb up either 1 or 2 steps at a time. Given an integer n, write a function that returns the number of unique ways you can climb the staircase. The order of the steps matters, so each different order of steps counts as a way. Mod the result by 10 ** 9 + 7. Constraints n ≤ 100,000 Example 1 Input n = 4 Output 5 Explanation There are 5 unique ways: 1, 1, 1, 1 2, 1, 1 1, 2, 1 1, 1, 2 2, 2 Example 2 Input n = 1 Output 1 Explanation There's only one way: climb up 1 step. Example 3 Input n = 3 Output 3 Explanation There are three ways: [1, 1, 1], [2, 1], and [1, 2].

### Solution :

` ````
Solution in C++ :
const int mod = 1e9 + 7;
struct mtx {
long long int a, b, c, d;
mtx(long long int w, long long int x, long long int y, long long int z)
: a(w), b(x), c(y), d(z) {
}
mtx operator*(mtx& other) {
mtx A(a * other.a + b * other.c, a * other.b + b * other.d, c * other.a + d * other.c,
c * other.b + d * other.d);
A.a %= mod;
A.b %= mod;
A.c %= mod;
A.d %= mod;
return A;
}
};
mtx power(mtx A, long long int k) {
mtx B(1, 0, 0, 1);
while (k) {
if (k & 1) {
B = B * A;
}
A = A * A;
k /= 2;
}
return B;
}
int solve(int n) {
mtx A(1, 1, 1, 0);
mtx B = power(A, n);
// cout << B.a << " " << B.b << endl << B.c << " " << B.d << endl;
return B.a;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public int solve(int n) {
int a = 1;
int b = 1;
int c;
for (int i = 2; i <= n; i++) {
c = a;
a = b;
b = (b + c) % 1000000007;
}
return b;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, n):
MOD = 10 ** 9 + 7
def fib():
a = b = 1
yield a
yield b
while True:
yield (a + b) % MOD
a, b = b, (a + b) % MOD
f = fib()
for _ in range(n):
next(f)
return next(f)
```

## View More Similar Problems

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →