| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 sealed class ClimbingStairsIterativeFibonacci : IClimbingStairs |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 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 step1 = 1; |
| | 7 | 34 | | var step2 = 1; |
| | | 35 | | |
| | 84 | 36 | | for (var i = 2; i < n + 1; i++) |
| | 35 | 37 | | { |
| | 35 | 38 | | var step3 = step1 + step2; |
| | | 39 | | |
| | 35 | 40 | | step1 = step2; |
| | 35 | 41 | | step2 = step3; |
| | 35 | 42 | | } |
| | | 43 | | |
| | 7 | 44 | | return step2; |
| | 9 | 45 | | } |
| | | 46 | | } |