-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathMinCostClimbingStairs.cpp
43 lines (32 loc) Β· 988 Bytes
/
MinCostClimbingStairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//MIN COST CLIMBING STAIRS
//Link of the question :- https://leetcode.com/problems/min-cost-climbing-stairs/
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
int minCost(vector<int>&cost,int currentIndex,unordered_map<int,int>&m)
{
if(currentIndex==cost.size()) return 0;
if(currentIndex>cost.size()) return 999;
int currentKey=currentIndex;
if(m.count(currentKey)) return m[currentKey];
int oneJump= cost[currentIndex] + minCost(cost,currentIndex+1,m);
int twoJump= cost[currentIndex] + minCost(cost,currentIndex+2,m);
m[currentKey]= min(oneJump,twoJump);
return m[currentKey];
}
int minCostClimbingStairs(vector<int>& cost)
{
unordered_map<int,int>m;
int ans = minCost(cost,0,m);
return min(ans,m[1]);
}
int main()
{
int n;
cin>>n;
vector<int>cost(n,0);
for(int i=0;i<n;i++)
cin>>cost[i];
cout<<minCostClimbingStairs(cost);
return 0;
}