LC 983. 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
.
int mincostTickets(vector<int>& days, vector<int>& costs) {
unordered_set<int> travel(begin(days), end(days));
int dp[] = {};
for (int i = ; i < ; ++i) {
if (travel.find(i) == travel.end()) dp[i] = dp[i - ];
else dp[i] = min({ dp[i - ] + costs[], dp[max(, i - )] + costs[], dp[max(, i - )] + costs[]});
}
return dp[];
}
class Solution {
public:
int mincostTickets(vector<int>& days, vector<int>& costs) {
unordered_set<int> travel(begin(days), end(days));
int dp[] = {};
for (int i = days.front(); i <= days.back(); ++i) {
if (travel.find(i) == travel.end()) dp[i % ] = dp[(i - ) % ];
else dp[i % ] = min({ dp[(i - ) % ] + costs[],
dp[max(, i - ) % ] + costs[], dp[max(, i - ) % ] + costs[] });
}
return dp[days.back() % ];
}
};
LC 983. Minimum Cost For Tickets的更多相关文章
- LeetCode 983. Minimum Cost For Tickets
原题链接在这里:https://leetcode.com/problems/minimum-cost-for-tickets/ 题目: In a country popular for train t ...
- 【leetcode】983. Minimum Cost For Tickets
题目如下: In a country popular for train travel, you have planned some train travelling one year in adva ...
- 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】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. ...
- Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- Minimum Cost(最小费用最大流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
随机推荐
- [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...
- CentOS7安装CDH 第六章:CDH的管理-CDH5.12
相关文章链接 CentOS7安装CDH 第一章:CentOS7系统安装 CentOS7安装CDH 第二章:CentOS7各个软件安装和启动 CentOS7安装CDH 第三章:CDH中的问题和解决方法 ...
- 2018江苏徐州icpc试题-A-生化危机【多源点-基础广搜】
- linux tcp server bind
如果不是系统管理员: bind()函数 返回失败
- 0007SpringBoot配置不同环境内容及指定启动哪个环境
1.多profiles的形式 分别新增application-dev.properties和application-prod.properties配置文件, 其中application-dev.pro ...
- Please, commit your changes or stash them before you can merge. Aborting
1.stash 通常遇到这个问题,你可以直接commit你的修改:但我这次不想这样. 看看git stash是如何做的. git stash git pull git stash pop ...
- 控制warning信息在控制台的显示
在运行代码时,有时出现warning信息, 1.当你后台不需要warning信息的时候,可以直接把warning信息省略掉. 2.如果代码是循环,则会在控制台打印多次warning信息,这会使得war ...
- [Google Guava] 2.4-集合扩展工具类
原文链接 译文链接 译者:沈义扬,校对:丁一 简介 有时候你需要实现自己的集合扩展.也许你想要在元素被添加到列表时增加特定的行为,或者你想实现一个Iterable,其底层实际上是遍历数据库查询的结果集 ...
- C# 未能加载项目文件
在使用VS打开从网上下载或者从其他地方复制得来的解决方案时,经常会出现这样一个错误,"在解决方案中的一个或多个项目由于以下原因未能加载项目文件或网站已移动或已重命名,或者不在您的计算机上.& ...
- 利用Js的console对象,在控制台打印调式信息测试Js
一次偶然的机会,打开百度的时候按下了F12,然后就见控制台里面输出了百度的招聘广告,感觉挺帅气的,再然后就有了这篇博文. 既然可以这样在控制台输出信息,那以后再调试Js的时候不就可以省去很多麻烦了嘛! ...