Minimum cost to reach the top of the floor by climbing stairs | Java program to find the minimum cost required to reach the n - th floor ; function to find the minimum cost to reach N - th floor ; declare an array ; base case ; initially to climb till 0 - th or 1 th stair ; iterate for finding the cost ; return the minimum ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minimumCost ( int cost [ ] , int n ) { int dp [ ] = new int [ n ] ; if ( n == 1 ) return cost [ 0 ] ; dp [ 0 ] = cost [ 0 ] ; dp [ 1 ] = cost [ 1 ] ; for ( int i = 2 ; i < n ; i ++ ) { dp [ i ] = Math . min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] ; } return Math . min ( dp [ n - 2 ] , dp [ n - 1 ] ) ; } public static void main ( String args [ ] ) { int a [ ] = { 16 , 19 , 10 , 12 , 18 } ; int n = a . length ; System . out . print ( minimumCost ( a , n ) ) ; } }