博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】983. Minimum Cost For Tickets
阅读量:7231 次
发布时间:2019-06-29

本文共 2885 字,大约阅读时间需要 9 分钟。

题目如下:

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: 11Explanation: 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: 17Explanation: 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

 

转载于:https://www.cnblogs.com/seyjs/p/10356000.html

你可能感兴趣的文章
Spreading the Wealth uva 11300
查看>>
Eclipse 报java.lang.UnsupportedClassVersionError: ("yourclass") bad major version at offset=6
查看>>
快读快输板子
查看>>
vue中父组件给子组件额外添加参数
查看>>
分片上传
查看>>
网络编程初识和socket套接字
查看>>
什么是构造函数?它和普通函数的区别?
查看>>
mysql中key 、primary key 、unique key 与index区别
查看>>
zabbix使用企业微信发送告警信息
查看>>
zabbix4.0离线快速编译安装(编译安装方法)
查看>>
[Java开发之路](7)RandomAccessFile类详解
查看>>
Linux中的tty与pts
查看>>
Java socket示例(demo)TCP/IP
查看>>
error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches
查看>>
计算UITextView的滑动高度
查看>>
AngularJs的UI组件ui-Bootstrap分享(四)——Datepicker Popup
查看>>
Java虚拟机------垃圾收集器
查看>>
UVA 1376 Animal Run 最短路
查看>>
oracle12c之 单机12.1.0.1打补丁
查看>>
封装了集中常用的文件读的方法
查看>>