【leetcode】983. Minimum Cost For Tickets
题目如下:
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array
days. Each day is an integer from1to365.Train tickets are sold in 3 different ways:
- a 1-day pass is sold for
costs[0]dollars;- a 7-day pass is sold for
costs[1]dollars;- a 30-day pass is sold for
costs[2]dollars.The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of
days.Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.Note:
1 <= days.length <= 3651 <= days[i] <= 365daysis in strictly increasing order.costs.length == 31 <= costs[i] <= 1000
解题思路:毕竟本人动态规划没有掌握的游刃有余,一时间想不出递推表达式。那就简单粗暴吧,对于任意一个days[i]来说,都有三种买票的方法,买1天,7天和30天,借助DFS的思想依次计算每一种方法的最小值,理论上是有3^365次方种组合,但是计算过程中可以舍去明显不符合条件的组合,因此此方法也能通过。
代码如下:
class Solution(object):
def mincostTickets(self, days, costs):
"""
:type days: List[int]
:type costs: List[int]
:rtype: int
"""
res = len(days) * costs[0]
queue = [(0,0)] #(inx,cost_inx,total)
dp = [366*costs[2]] * (len(days) + 1)
while len(queue) > 0:
#print len(queue)
inx,total = queue.pop(0)
if inx == len(days):
res = min(res,total)
continue
elif total > res:
continue
if dp[inx+1] > total + costs[0]:
queue.insert(0,(inx+1, total + costs[0]))
dp[inx+1] = total + costs[0]
import bisect
next_inx = bisect.bisect_left(days,days[inx]+7)
if dp[next_inx] > total + costs[1]:
queue.insert(0,(next_inx, total + costs[1]))
next_inx = bisect.bisect_left(days, days[inx] + 30)
if dp[next_inx] > total + costs[2]:
queue.insert(0,(next_inx, total + costs[2]))
return res
【leetcode】983. Minimum Cost For Tickets的更多相关文章
- 【leetcode】1217. Minimum Cost to Move Chips to The Same Position
We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...
- 【LeetCode】1167. Minimum Cost to Connect Sticks 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetcod ...
- 【LeetCode】857. Minimum Cost to Hire K Workers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
随机推荐
- 51nod 1714:B君的游戏(博弈 sg打表)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1714 nim游戏的一个变形,需要打出sg函数的表 #incl ...
- JS中的作用域及闭包
1.JS中的作用域 在 es6 出现之前JS中只有全局作用域和函数作用域,没有块级作用域,即 JS 在函数体内有自己的作用域,但是如果不是在函数体的话就全部都是全局作用域.比如在 if.for 等有 ...
- python 操作yaml文件
yaml 5.1版后弃用了yaml.load(file)这个用法,因为觉得很不安全,5.1版后就修改了需要指定Loader,通过默认加载器(FullLoader)禁止执行任意函数yaml 5.1之 ...
- rabbitmqctl常用命令-3
1)启动.关闭 rabbitmq节点和应用 rabbitmq-server -detached #rabbitmq分别启动节点和应用 应用关闭rabbitmqctl stop_app 应用启动 rab ...
- Autoresize UIView to fit subviews
@interface UIView (resizeToFit) -(void)resizeToFitSubviews; -(void)resizeHightToFitSubviews; -(void) ...
- 建站手册-浏览器信息:挪威的 Opera 浏览器
ylbtech-建站手册-浏览器信息:挪威的 Opera 浏览器 1.返回顶部 1. http://www.w3school.com.cn/browsers/browsers_opera.asp 2. ...
- spring4.1.8扩展实战之三:广播与监听
提到广播与监听,我们常常会想到RabbitMQ.Kafka等消息中间件,这些常用于分布式系统中多个应用之间,有时候应用自身内部也有广播和监听的需求(例如某个核心数据发生变化后,有些业务模块希望立即被感 ...
- Get The Treasury【HDU-3642】【扫描线】
题目链接 题目给出的是N个体积块,问的是有多少体积重叠了3次及以上? 那么就是怎么处理体积这样子的问题了,看到Z的种类不多的时候,就想着从Z离散化的角度去考虑这个问题了,然后就是怎样子去处理面积了,这 ...
- select的限制与poll的使用
select的限制 select的并发数受到两个限制:1.一个进程能打开的最大描述符数量;2.select中fd_set集合容量的限制(FD_SETSIZE) 关于进程的最大描述符数量: ulimit ...
- 详解JavaScript数组过滤相同元素的5种方法
详解JavaScript数组过滤相同元素的5种方法:https://www.jb51.net/article/114490.htm