1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc983.java 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-03-09 16:30 . 20190309
package code;
/*
* 983. Minimum Cost For Tickets
* 题意:两个数组,days代表那一天得去玩,costs表示玩1天,2天,7天的花费,问怎么玩可以把days覆盖,并且花费最少
* 难度:Medium
* 分类:Dynamic Programming
* 思路:猛一看题,感觉很难
* 想了以后,会发现就是典型的数组dp,难的地方主要在于数组不代表每天,如何把days中的天全部覆盖到
* 方法是将days转换为一个365长的arr,代表每一天
* 如果这一天不在days中,则 dp[i] = dp[i-1], 否则 dp[i] = min(d p[i-1]+cost , dp[i-2]+cost ,dp[i-7]+cost )
* Tips:
*/
public class lc983 {
public int mincostTickets(int[] days, int[] costs) {
int[] dp = new int[366]; //366,把0空出来
int days_cur = 0;
for (int i = 1; i < dp.length ; i++) {
if(i==days[days_cur]) { //在days中
int minCost = dp[Math.max(0, i-1)]+costs[0];
minCost = Math.min( minCost, dp[Math.max(0, i-7)]+costs[1] );
minCost = Math.min( minCost, dp[Math.max(0, i-30)]+costs[2] );
dp[i] = minCost;
if(days_cur<days.length-1) days_cur++; //防止越界
}else{
dp[i] = dp[i-1];
}
}
return dp[dp.length-1];
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891