原题链接在这里: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. C++静态库与动态库的区别

    在日常开发中,其实大部分时间我们都会和第三方库或系统库打交道.在 Android 开发音视频开发领域,一般会用到 FFmepg.OpenCV.OpenGL 等等开源库, 我们一般都会编译成动态库共我们 ...

  2. C++冒泡排序及优化

    冒泡排序 1.经典冒泡排序 经典的冒泡排序为从左边开始依次判断排序,每次最终仅将一个数向后冒泡,而对于其他数的排序没有什么帮助:如果已经所有元素已经是有序的,依然执行循环. 2.优化冒泡排序 优化地方 ...

  3. 4. Spark Streaming解析

    4.1 初始化StreamingContext import org.apache.spark._ import org.apache.spark.streaming._ val conf = new ...

  4. Windows状态栏图标显示异常

    1.新建TXT文档 2.写上以下代码 taskkill /im explorer.exe /f cd /d %userprofile%\appdata\local del iconcache.db / ...

  5. 记录个超级Update语句

    -- UPDATE UPDATE affair_list SET deleteState = WHERE gid IN ( SELECT tt.gid FROM ( SELECT a.gid FROM ...

  6. Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口

    HttpURLConnection是JDK自身提供的网络类,不需要引入额外的jar包.文中所使用到的软件版本:Java 1.8.0_191. 1.服务端 参见Java调用Http接口(1)--编写服务 ...

  7. 2019-07-24 PHP中mysql_fetch_assoc 和 mysql_fetch_array 有什么区别?

    mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组 来看下面的例子: 数据库中有上述几条数据,一般我们想取用就要按照如下代码: $con = mysql_connect('12 ...

  8. github上传本地项目代码

    进入github首页,点击新项目new repository,如下图所示: 然后进入如下页面,填写信息: 最后点击Create repository,生成如下页面: 红色圈圈画的步骤,先点击Clone ...

  9. 使用EwoMail搭建属于自己的个人邮件服务器——超详细图文教程

    版权声明:本文为CSDN博主「C_成」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_41692307 ...

  10. 【DATAGUARD】物理dg配置客户端无缝切换 (八.4)--ora-16652 和 ora-16603错误

    [DATAGUARD]物理dg配置客户端无缝切换 (八.4)--ora-16652 和 ora-16603错误 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读 各 ...