- Find min cost to reach the top of the floor.
Given that each i-th step of stair has non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps.
int minCost(vector<int>& cost) {
cost.push_back(0);
vector <int> totalCost(cost.size(), -1);
totalCost[0] = cost[0];
totalCost[1] = cost[1];
int last_index = cost.size()-1;
return findMin(totalCost, cost, last_index);
}
int findMin(vector<int>& totalCost, vector<int>& cost, int i){
if(totalCost[i] == -1){
totalCost[i] = cost[i] + min(findMin(totalCost, cost, i-1), findMin(totalCost, cost, i-2));
}
return totalCost[i];
}