leetcode 983. Minimum Cost For Tickets

题目

Minimum Cost For Tickets

总共三种票,一种可以玩一天,一种可以玩一星期,一种可以玩一月
给你指定的天(递增), 让你指定的天内都玩,并且花费最小

思路与代码

class Solution {
public:
    
    int mincostTickets(vector<int>& days, vector<int>& costs) {
        int l = days.size();
        if(l == 0)
            return 0;
        bool vis[366];
        memset(vis,0,sizeof(vis));
        for(auto x : days) vis[x] = true;
        vector<int> dp(366, 0x3f3f3f3f);
        dp[0] = 0;
        for(int i=1; i<=365; i++) {
            if(vis[i] == false) {
                dp[i] = dp[i-1];
                continue;
            }
            dp[i] = dp[i-1] + costs[0];
            dp[i] = min(dp[i], dp[max(0,i-7)] + costs[1]);
            dp[i] = min(dp[i], dp[max(0,i-30)] + costs[2]);
        }
        return dp[days[l-1]];
    }
};
posted @ 2019-06-17 16:18  Draymonder  阅读(320)  评论(0编辑  收藏  举报