Minimum cost to reach the top of the floor by climbing stairs | C # program to find the minimum cost required to reach the n - th floor space - optimized solution ; function to find the minimum cost to reach N - th floor ; traverse till N - th stair ; update the last two stairs value ; Driver Code
using System ; class GFG { static int minimumCost ( int [ ] cost , int n ) { int dp1 = 0 , dp2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int dp0 = cost [ i ] + Math . Min ( dp1 , dp2 ) ; dp2 = dp1 ; dp1 = dp0 ; } return Math . Min ( dp1 , dp2 ) ; } public static void Main ( ) { int [ ] a = { 2 , 5 , 3 , 1 , 7 , 3 , 4 } ; int n = a . Length ; Console . Write ( minimumCost ( a , n ) ) ; } }