Skip to content

Commit

Permalink
Create 983. Minimum Cost For Tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Dec 31, 2024
1 parent 8ac13a6 commit 373ec9d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 983. Minimum Cost For Tickets
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int solve(vector<int>& days, int n, vector<int>& costs,vector<int>&dp) {
if (n >= 366) {
return 0;
}
if(dp[n]!=-1)return dp[n];
if (days[n] == 1) {
return dp[n]=min(costs[0] + solve(days, n + 1, costs,dp),
min(costs[1] + solve(days, n + 7, costs,dp),
costs[2] + solve(days, n + 30, costs,dp)));
} else {
return dp[n]=solve(days, n + 1, costs,dp);
}
}
int mincostTickets(vector<int>& days, vector<int>& costs) {
vector<int> d(366, 0);
for (int i = 0; i < days.size(); i++) {
d[days[i]] = 1;
}
vector<int>dp(366,-1);
return solve(d, 1, costs,dp);
}
};

0 comments on commit 373ec9d

Please sign in to comment.