原题链接在这里: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. 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

题解:

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的更多相关文章

  1. 【leetcode】983. Minimum Cost For Tickets

    题目如下: In a country popular for train travel, you have planned some train travelling one year in adva ...

  2. LC 983. Minimum Cost For Tickets

    In a country popular for train travel, you have planned some train travelling one year in advance.  ...

  3. 983. Minimum Cost For Tickets

    网址:https://leetcode.com/problems/minimum-cost-for-tickets/ 参考:https://leetcode.com/problems/minimum- ...

  4. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  5. LeetCode 1000. Minimum Cost to Merge Stones

    原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...

  6. LeetCode 1130. Minimum Cost Tree From Leaf Values

    原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...

  7. 【LeetCode】983. 最低票价 Minimum Cost For Tickets(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  8. [Swift]LeetCode983. 最低票价 | Minimum Cost For Tickets

    In a country popular for train travel, you have planned some train travelling one year in advance.  ...

  9. [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 ...

随机推荐

  1. STL源码剖析——序列式容器#4 Stack & Queue

    Stack stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口,元素的新增.删除.最顶端访问都在该出口进行,没有其他位置和方法可以存取stack的元素. ...

  2. Delphi快递鸟【支持快递查询和单号识别】

    作者QQ:(648437169) 点击下载➨Delphi快递鸟 [delphi快递鸟]支持快递查询.单号识别.

  3. Python2 和 Python3区别

    字符串类型不同 py3: str bytes py2: unicode str 默认解释器编码 输入输出 int int long 除法 range和xrang 模块和包 字典 keys py2:列表 ...

  4. RHEL6.5 移植使用CentOS 的YUM 步骤

    问题:使用 Red Hat Enterprise Linux Server(RHEL) yum安装软件时显示 This system is not registered with RHN. RHN s ...

  5. 关于.Net使用企业库访问MySql数据库

    关于.Net使用企业库访问MySql数据库 在网上看了很多又重写又加WebConfig中的内容,其实不用那么麻烦 企业库5.0访问MySql数据库只需要在Web服务器安装mysql-connector ...

  6. Tomcat组件梳理--Catalina

    Tomcat组件梳理--Catalina 1.定义和功能 Catalina是Tomcat的核心组件,是Servlet容器,Catalina包含了所有的容器组件,其他模块均为Catalina提供支撑.通 ...

  7. Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口

    RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...

  8. JavaScript变量存储浅析(一)

    Hello! 上一篇关于JS中函数传参(http://www.cnblogs.com/souvenir/p/4969092.html)的介绍中提到了JS的另外一个基本概念:JS变量存储, 今天我们就用 ...

  9. kubeadm部署高可用K8S集群(v1.14.2)

    1. 简介 测试环境Kubernetes 1.14.2版本高可用搭建文档,搭建方式为kubeadm 2. 服务器版本和架构信息 系统版本:CentOS Linux release 7.6.1810 ( ...

  10. 使用python模拟实现KNN算法

    一.KNN简介 1.KNN算法也称为K邻近算法,是数据挖掘分类技术之一.所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代表. 2.KNN算法的核心思想是如果一个样本 ...