Leetcode 1025 Solution

This article provides solution to leetcode question 1025 (minimum-cost-for-tickets)

https://leetcode.com/problems/minimum-cost-for-tickets

Solution

class Solution:
    def dp(self, days, i, costs):
        if i >= len(days):
            return 0

        if i in self.memo:
            return self.memo[i]

        opts = []
        for cost, duration in zip(costs, [1, 7, 30]):
            j = i
            while j < len(days) and days[j] - days[i] < duration:
                j += 1
            opt = self.dp(days, j, costs) + cost
            opts.append(opt)

        self.memo[i] = min(opts)
        return self.memo[i]

    def mincostTickets(self, days, costs):
        self.memo = collections.Counter()
        return self.dp(days, 0, costs)