Skip to content

Commit e44dba4

Browse files
Create 983.cpp
Solved the Minimum Cost for Tickets (983) problem on LeetCode.
1 parent 75d2bff commit e44dba4

File tree

1 file changed

+23
-0
lines changed
  • leetcode/cpp/dynamic programming

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int mincostTickets(vector<int>& days, vector<int>& costs) {
4+
int n_days = (int)days.size();
5+
int last_day = days[n_days - 1];
6+
vector<int> cost(last_day + 1,0);
7+
int j = 0;
8+
int z = 0;
9+
for(int i = 1; i <= last_day; i++){
10+
if(days[j] == i){
11+
j++;
12+
int a = INT_MAX, b = INT_MAX, c = INT_MAX;
13+
a = cost[i - 1] + costs[0];
14+
b = cost[max(z, i - 7)] + costs[1];
15+
c = cost[max(z, i - 30)] + costs[2];
16+
cost[i] = min({a, b, c});
17+
}
18+
else cost[i] = cost[i-1];
19+
}
20+
21+
return cost[last_day];
22+
}
23+
};

0 commit comments

Comments
 (0)