LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/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 <= days.length <= 365
1 <= days[i] <= 365
days
is in strictly increasing order.costs.length == 3
1 <= costs[i] <= 1000
题解:
For all the days when travel is not needed, its min cost should be the same as previous day.
When it comes to the day travel is needed, there are 3 options, 1 day pass + up to previous day minimum cost, 7 days pass + up to previous 7 days minimum cost, 30 days pass + up to previous 30 days minimum cost.
Time Complexity: O(n). n = days[days.length-1].
Space: O(n).
AC Java:
class Solution {
public int mincostTickets(int[] days, int[] costs) {
int n = days.length;
int last = days[n - 1];
int [] dp = new int[last + 1];
int ind = 0;
for(int i = 1; i <= last; i++){
if(i != days[ind]){
dp[i] = dp[i - 1];
}else{
int can1 = dp[i - 1] + costs[0];
int can2 = dp[Math.max(0, i - 7)] + costs[1];
int can3 = dp[Math.max(0, i - 30)] + costs[2];
dp[i] = Math.min(can1, Math.min(can2, can3));
ind++;
}
} return dp[last];
}
}
The truth is we only need to maintain last 30s data.
Thus it couls limit dp array to 30 days.
Time Complexity: O(n).
Space: O(1).
AC Java:
class Solution {
public int mincostTickets(int[] days, int[] costs) {
int [] dp = new int[30];
int ind = 0;
for(int i = days[0]; i<=days[days.length-1]; i++){
if(i != days[ind]){
dp[i%30] = dp[(i-1)%30];
}else{
dp[i%30] = Math.min(dp[(i-1)%30] + costs[0], Math.min(dp[Math.max(0, i-7) % 30] + costs[1], dp[Math.max(0, i-30) % 30] + costs[2]));
ind++;
}
} return dp[days[days.length-1]%30];
}
}
类似Coin Change.
LeetCode 983. Minimum Cost For Tickets的更多相关文章
- 【leetcode】983. Minimum Cost For Tickets
题目如下: In a country popular for train travel, you have planned some train travelling one year in adva ...
- LC 983. Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- 983. Minimum Cost For Tickets
网址:https://leetcode.com/problems/minimum-cost-for-tickets/ 参考:https://leetcode.com/problems/minimum- ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...
- LeetCode 1130. Minimum Cost Tree From Leaf Values
原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...
- 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- [LeetCode] 857. Minimum Cost to Hire K Workers 雇佣K名工人的最低成本
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. Now w ...
随机推荐
- libevent源码分析一--io事件响应
这篇文章将分析libevent如何组织io事件,如何捕捉事件的发生并进行相应的响应.这里不会详细分析event与event_base的细节,仅描述io事件如何存储与如何响应. 1. select l ...
- Go调用Delphi编写的DLL
参数整数没有问题,但是如果是字符串,要注意几个细节. 记录如下: 1.Delphi定义函数的时候,字符串参数需要使用PChar类型 2.Go传递参数的时候要将字符串转成UTF16的指针,接收的时候采用 ...
- SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
最近,配置项目,使用SpringBoot2.2.1,配置mybatis访问db,配好后,使用自定义的数据源.启动发生: APPLICATION FAILED TO START ************ ...
- DevExpress中GridColumnCollection实现父子表数据绑定
绑定数据: 父表: DataTable _parent = _dvFlt.ToTable().Copy(); 子表: DataTable _child = _dvLog.ToTable().Copy( ...
- 通过Logstash由SQLServer向Elasticsearch同步数据
延用上篇ELK所需环境,新增logstash配置文件 需要数据库链接驱动 Microsoft JDBC driver 6.2 for SQL Server 下载地址: https://www.micr ...
- JS前端加密JAVA后端解密详解
最近有一个加解密的需求,其实没有什么难度,但是实践过程中踩了很多坑,把踩坑过程分享出来. 1.前端JS加密 /** * 加密(需要先加载aes.min.js文件) * @param word * @r ...
- Java之路---Day05
2019-10-19-21:09:31 面向对象的封装性 封装性 概念:封装就是将一些细节信息隐藏起来,对于外界不可见 面向对象封装性在Java中的体现 1.方法就是一种封装 public class ...
- 解除Ubuntu系统的root登录图形界面限制
Ubuntu18.04.1开发团队为了Ubuntu18.04.1系统的安全,默认root不能登录图形界面,普通用户需要使用root权限时,只能通过sudo [命令] [参数] 临时使用root权限,或 ...
- 【转载】C#中Convert.ToInt32方法将字符串转换为Int32类型
在C#编程过程中,可以使用Convert.ToInt32方法将字符串或者其他可转换为数字的对象变量转换为ToInt32类型,Convert.ToInt32方法有多个重载方法,最常使用的一个方法将字符串 ...
- JavaScript 相关的工具代码
博客地址:https://ainyi.com/49 记录一些数据处理需要的方法工具代码 持续更新中... 时间戳转与日期格式相互转换 时间戳转换成日期格式 function timestampToTi ...