| | 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.MinCostClimbingStairs; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MinCostClimbingStairsDynamicProgramming : IMinCostClimbingStairs |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="cost"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int MinCostClimbingStairs(int[] cost) |
| 6 | 24 | | { |
| 6 | 25 | | if (cost.Length == 0) |
| 1 | 26 | | { |
| 1 | 27 | | return 0; |
| | 28 | | } |
| | 29 | |
|
| 5 | 30 | | var costs = new int[cost.Length + 1]; |
| | 31 | |
|
| 5 | 32 | | costs[0] = 0; |
| 5 | 33 | | costs[1] = 0; |
| | 34 | |
|
| 50 | 35 | | for (var i = 2; i <= cost.Length; i++) |
| 20 | 36 | | { |
| 20 | 37 | | costs[i] = Math.Min(costs[i - 2] + cost[i - 2], costs[i - 1] + cost[i - 1]); |
| 20 | 38 | | } |
| | 39 | |
|
| 5 | 40 | | return costs[cost.Length]; |
| 6 | 41 | | } |
| | 42 | | } |