From b6f5b99ee19a41a7bedeeb6bb0732d5b7042ccea Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 7 Jan 2024 19:57:54 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C746.=20Min=20Cost=20Climbi?= =?UTF-8?q?ng=20Stairs=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/min-cost-climbing-stairs.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 cs/min-cost-climbing-stairs.cs diff --git a/cs/min-cost-climbing-stairs.cs b/cs/min-cost-climbing-stairs.cs new file mode 100644 index 0000000..b7df12c --- /dev/null +++ b/cs/min-cost-climbing-stairs.cs @@ -0,0 +1,17 @@ +public class Solution { + public int MinCostClimbingStairs(int[] cost) { + int Get(int k) { + if (k < 0 || k >= cost.Length) { + return 0; + } + + return cost[k]; + } + + for (var i = cost.Length - 1; i >= 0; --i) { + cost[i] += Math.Min(Get(i + 1), Get(i + 2)); + } + + return Math.Min(cost[0], cost[1]); + } +}