generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem15.cs
More file actions
33 lines (29 loc) · 889 Bytes
/
Problem15.cs
File metadata and controls
33 lines (29 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/climbing-stairs/">Climbing Stairs</see>.
/// </summary>
public static class Problem15
{
/// <summary>
/// You are climbing a staircase. It takes n steps to reach the top.
/// Each time you can either climb 1 or 2 steps. Find how many distinct ways you can climb to the top.
/// Time complexity: O(n).
/// Space complexity: O(1).
/// </summary>
/// <param name="n">Number of steps to reach the top.</param>
/// <returns>Number of distinct ways you can climb to the top.</returns>
public static int ClimbStairs(int n)
{
if (n < 4)
{
return n;
}
var second = 5;
for (int i = 4, first = 3; i < n; i++)
{
second += first;
first = second - first;
}
return second;
}
}