当前位置:   article > 正文

LeetCode第 983 题:最低票价(C++)_c++最低票价

c++最低票价

983. 最低票价 - 力扣(LeetCode)

状态转移表

动态规划,要求最小值,我们首先可以尝试使用状态转移表,使用一维数组就可以了:

class Solution {
public:
    int mincostTickets(vector<int>& days, vector<int>& costs) {
        int help[3] = {1,7,30};//和costs一一对应
        int n = days.size();
        int len = days[n-1] - days[0] + 1;//旅行的日期跨度
        vector<int> f(len + 1, INT_MAX);
        //BFS,状态转移表
        for(int i = 0; i < costs.size(); ++i){//特殊处理第一个阶段的状态
            if(help[i] <= len) f[help[i]] = costs[i];//到第help[i]天的时候,消费的价格
            else f[len] = min(f[len], costs[i]);
        }
		
		//只考虑需要出行的日子
        for(int i = 1; i < n; ++i){//最多会有 i 个阶段
            int idx = days[i] - days[0] + 1;//当前时间对应在f中的下标+1为idx
            for(int k = 0; k < idx; ++k){//状态只能从当前状态之前的状态转移过来
                if(f[k] != INT_MAX){
                    for(int j = 0; j < costs.size(); ++j){//每次都可以买不同天数的票
                        int tmp = idx + help[j] - 1; //选择的票会转移到f[tmp]状态
                        if(tmp <= len)    f[tmp] = min(f[tmp], f[k] + costs[j]);//是从状态k转移过来
                        else    f[len] = min(f[len], f[k] + costs[j]);//长度超出索引就只能转移到f[len],即最后一个状态
                    }
                    f[k] = INT_MAX;//之前的状态已经转移,转移过的状态就不再需要了
                }
            }
        }
        return f[len];
    }
};
  • 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

不过上面的代码写的思路不是很清晰。。。

从后往前进行dp

今天买哪种通行证,得看后几天怎么安排,即前面依赖后面–>从后向前来买

看了题解之后才觉得这题我想的太复杂了-_-

最低票价 - 最低票价 - 力扣(LeetCode)

dp[i] = min(决策1, 决策2, 决策3);//第 i 天开始,所需的最小费用
      = min(c[0] + 1天后不包, c[1] + 7天后不包, c[2] + 30天不包);
      = min(c[0] + dp[i + 1], c[1] + dp[i + 7], c[2] + dp[i + 30]);
  • 1
  • 2
  • 3

官方题解的:

class Solution {
public:
    unordered_set<int> dayset;//set方便查询
    vector<int> costs;
    vector<int> memo;
    int dp(int i){
        if(i > 365) return 0;
        if(memo[i] != -1)   return memo[i];
        if(dayset.count(i)){
            memo[i] = min(min(dp(i+1) + costs[0], dp(i + 7) + costs[1]), dp(i + 30) + costs[2]);
        }else   
            memo[i] = dp(i + 1);
        return memo[i];
    }
    int mincostTickets(vector<int>& days, vector<int>& costs) {
        this->costs = costs;
        memo = vector<int>(366, -1);
        for(const auto &c : days)    dayset.insert(c);
        return dp(1);
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

这儿的图解可以看一下,画的挺清晰:

[java] 动态规划思路步骤 (从后向前迭代) - 最低票价 - 力扣(LeetCode)

class Solution {
public:
    int mincostTickets(vector<int>& days, vector<int>& costs) {
        int len = days.size();
        int maxDay = days[len - 1], minDay = days[0];
        vector<int> dp(maxDay + 31, 0);//简化边界的判断,反正多出来的初始化为0
        for(int d = maxDay, i = len - 1; d >= minDay; --d){//从后往前
            //i 表示days的索引
            if(d == days[i]){//只考虑需要出行的日子
                dp[d] = min(dp[d + 1] + costs[0], dp[d + 7] + costs[1]);
                dp[d] = min(dp[d], dp[d + 30] + costs[2]);
                --i;
            }else dp[d] = dp[d + 1];
        }
        return dp[minDay];
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

从前往后dp

其实从前往后也是可以的,只是不太好理解:

C++动态规划从前向后推导,更具一般性的思路 - 最低票价 - 力扣(LeetCode)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/357229
推荐阅读
相关标签
  

闽ICP备14008679号