力扣第983题 最低票价

力扣第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];
    }

};

posted on 2020-05-06 23:54  woodjay  阅读(102)  评论(0编辑  收藏  举报

导航