题目如下:

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 from 1 to 365.

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. 1 <= days.length <= 365
  2. 1 <= days[i] <= 365
  3. days is in strictly increasing order.
  4. costs.length == 3
  5. 1 <= 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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】1167. Minimum Cost to Connect Sticks 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetcod ...

  3. 【LeetCode】857. Minimum Cost to Hire K Workers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  7. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  8. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  9. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

随机推荐

  1. Django学习——collectstatic错误

    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STAT ...

  2. CPU性能优化

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11521331.html CPU性能指标 根据指标找工具 根据工具查指标 top.vmstat 和 pi ...

  3. 为你的AliOS Things应用增加自定义cli命令

    摘要: 怎么才能在RTOS系统中,通过 串口shell控制LED的开关. 在日常嵌入式开发中,我们经常会用串口命令来使设备进入某种特定的状态,或执行某个特定的操作.如系统自检,模拟运行,或者进入手动模 ...

  4. web前端到底怎么学?

    互联网+的火爆,让互联网行业快速的扩张.越来越多的人想通过学习的途径进入这个行业,java开发.WEB前端开发.UI设计等专业受到大众追捧.小编这次主要介绍一下WEB前端开发,为想要学习web前端开发 ...

  5. 【Linux】服务器间免密登录、免确认机器指纹

    1.生成密钥 ssh-keygen -t rsa -C "<填写自己方便识别的注释>" -b 4096  没什么问题就执行三次空格. 三次问题是1.填入生成密钥对的路径 ...

  6. LUOGU P3759 [TJOI2017]不勤劳的图书管理员(树套树)

    传送门 解题思路 和以前做过的一道题有点像,就是区间逆序对之类的问题,用的是\(BIT\)套权值线段树,交换时讨论一下计算答案..跑的不如暴力快.. 代码 #include<iostream&g ...

  7. 洛谷 4099 [HEOI2013]SAO——树形DP

    题目:https://www.luogu.org/problemnew/show/P4099 结果还是看了题解才会…… 关键是状态,f[ i ][ j ] 表示 i 子树. i 号点是第 j 个出现的 ...

  8. vue-cli2.X之simple项目搭建过程

    1.vue init webpack-simple vuedemo02 2.按提示操作 3. 项目目录: ps:可能遇到的问题

  9. npm ERR! { Error: EPERM: operation not permitted, mkdir 'C:\Program Files\nodejs\node_cache\_locks'

    vue项目安装json-server报错npm ERR!  { Error: EPERM: operation not permitted, mkdir 'C:\Program Files\nodej ...

  10. FWT公式一览

    总表 真值表 对应运算 FWT IFWT A=B=0 A≠B A=B=1 左项 右项 左项 右项 0 0 1 & L+R R L-R R 0 1 0 ^ L+R L-R (L+R)/2 (L- ...