<p>You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array <code>days</code>. Each day is an integer from <code>1</code> to <code>365</code>.</p>

<p>Train tickets are sold in <strong>three different ways</strong>:</p>

<ul>
	<li>a <strong>1-day</strong> pass is sold for <code>costs[0]</code> dollars,</li>
	<li>a <strong>7-day</strong> pass is sold for <code>costs[1]</code> dollars, and</li>
	<li>a <strong>30-day</strong> pass is sold for <code>costs[2]</code> dollars.</li>
</ul>

<p>The passes allow that many days of consecutive travel.</p>

<ul>
	<li>For example, if we get a <strong>7-day</strong> pass on day <code>2</code>, then we can travel for <code>7</code> days: <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, and <code>8</code>.</li>
</ul>

<p>Return <em>the minimum number of dollars you need to travel every day in the given list of days</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> days = [1,4,6,7,8,20], costs = [2,7,15]
<strong>Output:</strong> 11
<strong>Explanation:</strong> For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total, you spent $11 and covered all the days of your travel.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
<strong>Output:</strong> 17
<strong>Explanation:</strong> For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total, you spent $17 and covered all the days of your travel.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= days.length &lt;= 365</code></li>
	<li><code>1 &lt;= days[i] &lt;= 365</code></li>
	<li><code>days</code> is in strictly increasing order.</li>
	<li><code>costs.length == 3</code></li>
	<li><code>1 &lt;= costs[i] &lt;= 1000</code></li>
</ul>

from bisect import bisect_left
from math import inf

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        """
        Extremes:
        - find first day of travel, and buy 30 day tickets from there until last day of 
          travel
        - Buy 1 day ticket for each day of travel

        - At each day of travel, could choose to buy 1,7,30 day ticket

        DFS approach: O(365 * costs) CC & MC
        - Init a cache as a dict, with els of form day: cost
        - Create a set of days
        - Create a DFS helper which takes curr_day, curr_cost
            - if curr_day >= 365, return curr_cost
            - elif days_idx in cache:
                - return cache[days_idx]
            - else if curr_day is NOT in travel_days set
                - cache[curr_day] = dfs(curr_day + 1, curr_cost)
            - else:
                - cache[curr_day] = min of:
                    - dfs(curr_day + 1, curr_cost + costs[0])
                    - dfs(curr_day + 7, curr_cost + costs[1])
                    - dfs(curr_day + 30, curr_cost + costs[2])
            - return cache[curr_day]
        - return dfs(1, 0)

        DFS optimized approach: O(days^2 * log(days))
        - Init a cache as a dict, with els of form day: cost
        - sort days O(days * log(days))
        - Create a DFS helper which takes days_idx, curr_cost
            - if days_idx == len(days):
                - return curr_cost
            - elif days_idx in cache:
                - return cache[days_idx]
            - else:
                - for each of 1,7,30
                    - find the next travel day after that window (bin search) O(days log(days))
                    - call DFS with the index of that
                - set cache val to min of the above
            - return cache val
        - return DFS with min(days), 0
        """
        cache: dict[int, int] = {}
        travel_days = set(days)

        def dfs(day: int):
            if day > 365:
                return 0
            elif day in cache:
                return cache[day]
            elif day not in travel_days:
                cache[day] = dfs(day + 1)
            else:
                cache[day] = min(
                    costs[0] + dfs(day + 1),
                    costs[1] + dfs(day + 7),
                    costs[2] + dfs(day + 30),
                )

            return cache[day]

        return dfs(1)

    # def mincostTicketsSort(self, days: List[int], costs: List[int]) -> int:
    #     cache: dict[int,int] = {}
    #     days.sort()
    #     intervals = [1,7,30]

    #     def dfs(day_idx: int):
    #         if day_idx >= len(days):
    #             return 0
    #         elif day_idx in cache:
    #             return cache[day_idx]
    #         else:
    #             min_cost = inf
    #             for i, interval in enumerate(intervals):
    #                 nxt_day_idx = bisect_left(days, days[day_idx] + interval, lo=day_idx + 1)
    #                 min_cost = min(min_cost, costs[i] + dfs(nxt_day_idx))
    #             cache[day_idx] = min_cost

    #         return cache[day_idx]

    #     return dfs(0)

18200