go: add «983. Minimum Cost For Tickets»
URL: https://leetcode.com/problems/minimum-cost-for-tickets/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
5cc859c6b4
commit
4e0f9fbca5
1 changed files with 25 additions and 0 deletions
25
go/minimum-cost-for-tickets.go
Normal file
25
go/minimum-cost-for-tickets.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
func mincostTickets(days []int, costs []int) int {
|
||||
daily, weekly, monthly := costs[0], costs[1], costs[2]
|
||||
|
||||
lastDay := days[len(days)-1]
|
||||
|
||||
dp := make([]int, lastDay+1)
|
||||
for i, day := 0, 1; day <= lastDay; day++ {
|
||||
if day < days[i] {
|
||||
// skip tickets when not travelling
|
||||
dp[day] = dp[day-1]
|
||||
continue
|
||||
}
|
||||
|
||||
dp[day] = min(
|
||||
dp[day-1]+daily,
|
||||
dp[max(0, day-7)]+weekly,
|
||||
dp[max(0, day-30)]+monthly,
|
||||
)
|
||||
i++
|
||||
}
|
||||
|
||||
return dp[lastDay]
|
||||
}
|
Loading…
Reference in a new issue