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 <= 3651 <= days[i] <= 365daysis in strictly increasing order.costs.length == 31 <= 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 ...
随机推荐
- Spring 学习指南 第三章 bean的配置 (未完结)
第三章 bean 的配置 在本章中,我们将介绍以下内容: bean 定义的继承: 如何解决 bean 类的构造函数的参数: 如何配置原始类型 (如 int .float 等) .集合类型(如 ja ...
- 读文件时出现这个错误 'utf-8' codec can't decode byte 0xba in position 21: invalid start byte
''' file2 文件内容: 很任性wheniwasyoung ''' 源代码: f = open("file2",'r',encoding="utf-8") ...
- 【Linux】Ubuntu修改root用户密码
使用vmware安装ubuntu的时候,安装完之后发现没有root用户密码,只有新建用户,这个时候需要重新设置root用户密码 在新建用户下面运行 sudo passwd root
- 全栈项目|小书架|微信小程序-登录回调及获取点赞列表功能
效果图 这一节介绍,登录回调 以及 喜欢列表 的实现. 登录回调:这里是指在获取登录完成之后,再进行下一步的操作. 比如效果图中我的页面,默认是未登录状态,积分和喜欢列表的数量都没有获取到. 而登录成 ...
- 《JAVA高并发编程详解》-程序可能出现死锁的场景
- JS实现重载
在js中,我们实现重载常用的方式有: 1.根据传入参数的类型执行不同的操作. 2.利用参数中特殊的参数值进行不同的操作. 3.根据参数的个数进行重载. 这里对第三种重载方式的实现进行说明. 实现第三种 ...
- Node.js 实现 MySQL 数据库增删改查
安装mysql $ npm isntall mysql 连接数据库 需要根据实际配置修改数据库用户名.及密码及数据库名 let mysql = require('mysql'); let connec ...
- TF-IDF词频逆文档频率算法
一.简介 1.RF-IDF[term frequency-inverse document frequency]是一种用于检索与探究的常用加权技术. 2.TF-IDF是一种统计方法,用于评估一个词对于 ...
- idea上maven打包
首先要实现maven打包,pom需要引入依赖 pom.xml <project> <dependencies> …… </dependencies> <bui ...
- 你真的会使用 VMware Workstation 吗
你真的会使用VMware Workstation吗?网上有很多教程,虽然都还可以,但总感觉差强人意.所以笔者在这里分享自己的使用心得,让大家参考一下,个人认为是最好的了. 简介 VMware Work ...