| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.ClimbingStairs; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ClimbingStairsDynamicProgramming : IClimbingStairs |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="n"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int ClimbStairs(int n) |
| 9 | 24 | | { |
| 9 | 25 | | switch (n) |
| | 26 | | { |
| | 27 | | case 1: |
| 1 | 28 | | return 1; |
| | 29 | | case 2: |
| 1 | 30 | | return 2; |
| | 31 | | } |
| | 32 | |
|
| 7 | 33 | | var values = new int[n + 1]; |
| | 34 | |
|
| 7 | 35 | | values[0] = 1; |
| 7 | 36 | | values[1] = 1; |
| | 37 | |
|
| 84 | 38 | | for (var i = 2; i <= n; i++) |
| 35 | 39 | | { |
| 35 | 40 | | values[i] = values[i - 1] + values[i - 2]; |
| 35 | 41 | | } |
| | 42 | |
|
| 7 | 43 | | return values[n]; |
| 9 | 44 | | } |
| | 45 | | } |