Leetcode 983. 最低票价

1、心路历程

这道题满足最大最小问题,大概率就是用动态规划。接着,发现当days长度为1时最简单,因此递推方向一定是从n到n-1。假设n-1的问题解决了,那就研究从n-1转移到n有几种不同情况,取动作的最小值即可。

这道题自己写的有点麻烦,但是很朴素;有个技巧是按照天来递推而不是索引,这道题属于灵活转化索引与值的范畴。

2、注意的点:

1、循环的方向不要写反了

2、边界值容易搞混,要分清循环到终止点和循环到头这两种不同情况

解法一:普通动态规划

py 复制代码
class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        @cache
        def dp(i):  # 前i天的最低消费
            # print(i)
            if i < 0: return 0
            if i == 0: return min(costs[0], costs[1], costs[2])  # 不一定第一天最便宜
            res1 = costs[0] + dp(i - 1)
            for k1 in range(i-1, -2, -1):  # 注意遍历的顺序不要反了,注意处理边界条件
                if days[k1] <= days[i] - 7: break
            res2 = costs[1] + dp(k1)
            for k2 in range(i-1, -2, -1):
                if days[k2] <= days[i] - 30: break
            res3 = costs[2] + dp(k2)
            res = min(res1, res2, res3)
            # print(i, res, res1, res2, res3, k1)
            return res
        return dp(len(days) - 1)

解法二:精简动态规划:

py 复制代码
class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        lastday = days[-1]
        @cache
        def dp(day_i):
            if day_i <= 0: return 0
            if day_i not in days:  return dp(day_i - 1)  # 不在范围内就不花钱
            return min(costs[0] + dp(day_i - 1), costs[1] + dp(day_i - 7), costs[2] + dp(day_i - 30))
        return dp(lastday)
        
相关推荐
databook2 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar3 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户8356290780513 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_3 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机10 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机11 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机11 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机11 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i11 小时前
drf初步梳理
python·django
每日AI新事件11 小时前
python的异步函数
python