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 ...
随机推荐
- flex布局下img图片变形的解决方法
图片正常效果 图片变形效果 一.flex-shrink: 0 给 img 设置 flex-shrink: 0; flex-shrink 的默认值为1,如果没有显示定义该属性,将会自动按照默认值 ...
- mesos-master启动失败,报错Failed to load unknown flag 'quorum.rpmsave'
[现象] mesos启动失败,查看mesos状态报错: [root@hps102 ~]# systemctl status mesos-master ● mesos-master.service - ...
- 连接mongodb服务器
连接mongodb有几种方法 一种是使用mongodb编译时生成的客户端进行连接,就是我们之前介绍过的mongo客户端 另一种是使用各种驱动进行连接 这次使用mongo客户端进行连接,之前我们启动了一 ...
- go语言合并两个数组
https://stackoverflow.com/questions/16248241/concatenate-two-slices-in-go Add dots after the second ...
- 1115 JAVAScript基础
目录 JavaScript 1.基础 1.1 注释 1.2 引入方式 2. 变量 2.1 变量声明 2.2 变量的命名规范 3.常量 4. 数据类型 4.1 数值型 Number 4.2 字符串型 S ...
- asp.net core 读取appsettings.json配置项
1.新建一个asp.net core 项目 2.打开appsettings.json,加入配置项 { "Logging": { "IncludeScopes": ...
- thinkphp session跨域
1 .在config.PHP中添加 'SESSION_OPTIONS'=>array('domain'=>'.caizhimofang.con'),//session配置 'COOK ...
- 开发中少不了的Fun -- 前端本地存储
存储sessionStorage function setSessionStore (name, content) { if (!name) return if (typeof content !== ...
- hbase实践之写流程拾遗
keyvalue KeyValue中包含了丰富的自我描述信息: KeyValue是支撑"稀疏矩阵"设计的一个关键点:一些Key相同的任意数量的独立KeyValue就可以构成一行数据 ...
- python 函数中,os.linesep是干什么的
os.linesep字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'. def write_file(filename, subdom ...