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.

  1. int mincostTickets(vector<int>& days, vector<int>& costs) {
  2. unordered_set<int> travel(begin(days), end(days));
  3. int dp[] = {};
  4. for (int i = ; i < ; ++i) {
  5. if (travel.find(i) == travel.end()) dp[i] = dp[i - ];
  6. else dp[i] = min({ dp[i - ] + costs[], dp[max(, i - )] + costs[], dp[max(, i - )] + costs[]});
  7. }
  8. return dp[];
  9. }
  1. class Solution {
  2. public:
  3. int mincostTickets(vector<int>& days, vector<int>& costs) {
  4. unordered_set<int> travel(begin(days), end(days));
  5. int dp[] = {};
  6. for (int i = days.front(); i <= days.back(); ++i) {
  7. if (travel.find(i) == travel.end()) dp[i % ] = dp[(i - ) % ];
  8. else dp[i % ] = min({ dp[(i - ) % ] + costs[],
  9. dp[max(, i - ) % ] + costs[], dp[max(, i - ) % ] + costs[] });
  10. }
  11. return dp[days.back() % ];
  12. }
  13. };

LC 983. Minimum Cost For Tickets的更多相关文章

  1. LeetCode 983. Minimum Cost For Tickets

    原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...

  2. 【leetcode】983. Minimum Cost For Tickets

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

  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】983. 最低票价 Minimum Cost For Tickets(C++ & Python)

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

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

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

  7. Minimum Cost For Tickets

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

  8. Minimum Cost(最小费用最大流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  9. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

随机推荐

  1. 使用Linux的tcpdump命令结合Windows的wireshark抓包和分析

    tcpdump简介 tcpdump是Linux系统下的一款抓包命令集,工作原理是基于网卡抓取流动在网卡上的数据包.在Linux系统中由于tcpdump命令的简单和强大,我们一般直接使用tcpdump命 ...

  2. 去“BAT”这样面试,拿到offer的几率是80%

    一.概述 面试,难还是不难?取决于面试者的底蕴(气场+技能).心态和认知及沟通技巧.面试其实可以理解为一场聊天和谈判,在这过程中有心理.思想上的碰撞和博弈.其实你只需要搞清楚一个逻辑:“面试官为什么会 ...

  3. FPGA学习笔记——点亮LED

    软件平台:win7(64bit) + Quartus II 9.1 (64-Bit) 硬件平台:东理电子Easy-FPGA Cyclone II EP2C5T114C8N 这个开发板买了很长时间了,买 ...

  4. C++——数组形参退化为指针

    数组做形参退化为指针 如果数组作为函数参数,则数组形参会退化为指针,以下代码在编译器看来是等价的 ]); ]); void fun3(int a[]); void fun4(int *a); #inc ...

  5. HTML块级元素与行内元素的区别

    块级元素:块级大多为结构性标记 <address>...</adderss> <center>...</center> 地址文字 <h1>. ...

  6. C C++输出格式 <转载>仅用于个人

    转载链接:C++ C C语言输出格式总结 1 一般格式    printf(格式控制,输出表列)    例如:printf("i=%d,ch=%c\n",i,ch);    说明: ...

  7. 什么是调整后的R方

    当给模型增加自变量时,复决定系数也随之逐步增大,当自变量足够多时总会得到模型拟合良好,而实际却可能并非如此.于是考虑对R2进行调整,记为Ra2,称调整后复决定系数.R2=SSR/SST=1-SSE/S ...

  8. 传说中Python最难理解的点,看这完篇就够了

    本文转载自简书,作者为菜鸟,感谢作者的辛苦付出. 这不是我第一次学Python入门课,去年.前年我都学过Python入门.所以文章的标题一点都没有标题党的意思.但是整个入门篇还有一个最难的东西没有讲, ...

  9. 跨域问题——学习ing

    问题 跨域:我写了一个页面,在js中写了请求,这个请求的url跟我这个页面不在一个域名,那么这个请求就是跨域请求. 跨域会怎么样:没见过,可能就不让你请求呗,为了安全考虑之类的.(涉及浏览器的同源策略 ...

  10. 如何解决web大流量,高并发问题

    对于当今大流量的网站,每天几千万甚至上亿的流量,是如何解决访问量问题的呢? 以下是一些总结的方法:  第一,确认服务器硬件是否足够支持当前的流量.  普通的P4服务器一般最多能支持每天10万独立IP, ...