题目如下:

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. after()和append()的区别、before()和prepend()区别、appendTo()和prependTo()、insertAfter()和insertBefore()

    一.after()和before()方法的区别 after()——其方法是将方法里面的参数添加到jquery对象后面去:    如:A.after(B)的意思是将B放到A后面去:    before( ...

  2. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  3. window和linux(centos7)安装mysql5.7

    window mysql 安装步骤 社区版本下载地址: https://dev.mysql.com/downloads/file/?id=474802 下载完成后,得到mysql-installer- ...

  4. APP前置代码

    APP自动化前置代码: #导入包from appium import webdriverimport timedesired_caps = {}desired_caps['platformName'] ...

  5. Java Web学习总结(11)JDBC

    一,简介 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的 ...

  6. vue之条件语句小结

    vue之条件语句小结 v-if, v-else 随机生成一个数字,判断是否大于0.5,然后输出对应信息: <!DOCTYPE html> <html> <head> ...

  7. [CSP-S模拟测试]:Set(随机化)

    题目描述 你手上有$N$个非负整数,你需要在这些数中找出一个非空子集,使得它的元素之和能被$N$整除.如果有多组合法方案,输出任意一组即可.注意:请使用高效的输入输出方式避免输入输出耗时过大. 输入格 ...

  8. 【前端技术】一篇文章搞掂:WeX5

    一.组件 Data组件 http://docs.wex5.com/data/ 遍历输出

  9. VB.NET导出Excel 轻松实现Excel的服务器与客户端交换 服务器不安装Office

    说来VB.Net这个也是之前的一个项目中用到的.今天拿来总结下用途,项目需求,不让在服务器安装Office办公软件.这个也是煞费了一顿. 主要的思路就是 在导出的时候,利用DataTable做中间变量 ...

  10. 【转】 Linux 的目录详解 (Linux基础一)

    前言 转自: http://c.biancheng.net/view/2833.html 进行了一些提炼和修改. 学习 Linux,不仅限于学习各种命令,了解整个 Linux 文件系统的目录结构以及各 ...