-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy path983-minimum-cost-for-tickets.cpp
37 lines (35 loc) · 1.08 KB
/
983-minimum-cost-for-tickets.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
class Solution {
public:
int mincostTickets(vector<int>& days, vector<int>& costs) {
int n = days.size();
if (n == 0) return 0;
if (n == 1) return costs[0];
vector<int> minCosts(366, 0);
int currDay = 0;
int dc = costs[0];
int wc = costs[1];
int mc = costs[2];
for (int i = 1; i <= 365; i++) {
if (currDay == n) {
break;
}
if (days[currDay] != i) {
minCosts[i] = minCosts[i - 1];
} else {
int dailyCost = dc, weeklyCost = wc, monthlyCost = mc;
if (i > 1) {
dailyCost += minCosts[i - 1];
}
if (i > 7) {
weeklyCost += minCosts[i - 7];
}
if (i > 30) {
monthlyCost += minCosts[i - 30];
}
minCosts[i] = min(dailyCost, min(weeklyCost, monthlyCost));
currDay++;
}
}
return minCosts[days[n - 1]];
}
};