-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmincosttickets.cpp
49 lines (44 loc) · 1.01 KB
/
mincosttickets.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
44
45
46
47
48
49
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
using namespace std;
class Solution {
public:
int helper(vector<int>days,vector<int>costs,int pos,vector<int>&memo){
cout<<pos<<endl;
if(pos>=days.size()){
return 0;
}
if(memo[pos]!=-1){
return memo[pos];
}
int ch1=helper(days,costs,pos+1,memo)+costs[0];
int ind=pos;
int curr=days[pos];
while(curr+7>days[ind]){
ind++;
if(ind>=days.size()){
break;
}
}
int ch2=helper(days,costs,ind,memo)+costs[1];
ind=pos;
curr=days[pos];
while(curr+30>days[ind]){
ind++;
if(ind>=days.size()){
break;
}
}
int ch3=helper(days,costs, ind,memo)+costs[2];
return min(ch1,min(ch2,ch3));
}
int mincostTickets(vector<int>& days, vector<int>& costs) {
vector<int>memo(days.size()+1,-1);
memo[days.size()]=0;
for(int i=days.size()-1;i>=0;i--){
memo[i]=helper(days,costs,0,memo);
}
return memo[0];
}
};