赞
踩
- func mincostTickets(days []int, costs []int) int {
- n:=days[len(days)-1]
- dp:=make([]int,n+1)
- set:=map[int]bool{}
- for _,day:=range days {
- set[day]=true
- }
- dp[days[0]]=min(costs[0],min(costs[1],costs[2]))
- for i:=days[0]+1;i<=n;i++ {
- if set[i] {
- dp[i]=dp[i-1]+costs[0]
- if i-7>=0 {
- dp[i]=min(dp[i],dp[i-7]+costs[1])
- }else {
- dp[i]=min(dp[i],costs[1])
- }
- if i-30>=0 {
- dp[i]=min(dp[i],dp[i-30]+costs[2])
- }else {
- dp[i]=min(dp[i],costs[2])
- }
- }else {
- dp[i]=dp[i-1]
- }
- }
- //fmt.Println(dp)
- return dp[n]
- }
- func min(a,b int)int {
- if a<b {
- return a
- }
- return b
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。