码迷,mamicode.com
首页 > 其他好文 > 详细

力扣第983题 最低票价

时间:2020-05-07 01:05:00      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:图片   c++   fun   code   http   info   image   private   size   

力扣第983题 最低票价

技术图片

技术图片

class Solution {
private:
    vector<int> days, costs;
    vector<int> dp;
    int durations[3] = {1, 7, 30};
public:
    int mincostTickets(vector<int>& days, vector<int>& costs)
    {
        this->days = days;
        this->costs = costs;
        dp.assign(days.size(), -1);
        return func(0);
    }

    int func(int i)
    {
        int len = days.size();
        if(len <= i)
            return 0;
        if(dp[i] != -1)
            return dp[i];
        dp[i] = INT_MAX;
        int j = i;
        for(int k = 0; k < 3; k++)
        {
            while(j < len && days[j] < days[i] + durations[k])
                j++;
            dp[i] = min(dp[i], func(j) + costs[k]);
        }
        return dp[i];
    }

};

力扣第983题 最低票价

标签:图片   c++   fun   code   http   info   image   private   size   

原文地址:https://www.cnblogs.com/woodjay/p/12840027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!