当前位置: 首页 > news >正文

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

 

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

相关文章:

  • BZOJ 2810 [Apio2012]kunai
  • HashMap剖析之内部结构
  • OpenvSwitch/OpenFlow 架构解析与实践案例
  • CSS opacity设置不透明度
  • runC爆严重安全漏洞,主机可被攻击!使用容器的快打补丁
  • CH5102 Mobile Service
  • 区块链共识机制优缺点对比都是什么
  • Python数据可视化的10种技能
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • sql语句实战
  • 小程序微服务单个SSL证书部署多个项目解决方案
  • Async注解的使用,异步进行代码解耦
  • 我们在编写python代码时应该注意那几件事 !
  • Collection和Collections的区别是什么?
  • 根据出生日期计算年龄
  • CentOS 7 防火墙操作
  • gops —— Go 程序诊断分析工具
  • js如何打印object对象
  • python学习笔记-类对象的信息
  • Terraform入门 - 1. 安装Terraform
  • text-decoration与color属性
  • ubuntu 下nginx安装 并支持https协议
  • Web Storage相关
  • 聊一聊前端的监控
  • 前端js -- this指向总结。
  • 如何实现 font-size 的响应式
  • 软件开发学习的5大技巧,你知道吗?
  • 手机端车牌号码键盘的vue组件
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • ionic异常记录
  • Java数据解析之JSON
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • Spring第一个helloWorld
  • #14vue3生成表单并跳转到外部地址的方式
  • #pragma once
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (C语言)球球大作战
  • (篇九)MySQL常用内置函数
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (十六)Flask之蓝图
  • (一)kafka实战——kafka源码编译启动
  • (一)基于IDEA的JAVA基础12
  • (转)Unity3DUnity3D在android下调试
  • ****Linux下Mysql的安装和配置
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .NET 5种线程安全集合
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .net core webapi 大文件上传到wwwroot文件夹
  • .net 微服务 服务保护 自动重试 Polly
  • .NET序列化 serializable,反序列化