From 4e0f9fbca50391f3cd025033d8641a0c83259eee Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 31 Dec 2024 11:36:51 +0100 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB983.=20Minimum=20Cost=20For?= =?UTF-8?q?=20Tickets=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/minimum-cost-for-tickets/ Signed-off-by: Matej Focko --- go/minimum-cost-for-tickets.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 go/minimum-cost-for-tickets.go diff --git a/go/minimum-cost-for-tickets.go b/go/minimum-cost-for-tickets.go new file mode 100644 index 0000000..62f075a --- /dev/null +++ b/go/minimum-cost-for-tickets.go @@ -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] +}