In a country popular for train travel, you have planned some train travelling one year in advance.  The days of the year that you will travel is given as an array days.  Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:

a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.

The passes allow that many days of consecutive travel.  For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
 
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation: 
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.


Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation: 
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.

 

Note:

1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000

Difficulty: interview

Starter code:

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        

Solutions:
class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [0] + [-1 for i in range(days[-1])]
        
        for day in days:
            dp[day] = 0
        
        for i in range(1, len(dp)):
            if dp[i] == -1:
                dp[i] = dp[i-1]
            
            else:
                dp[i] = min(
                    dp[i-1] + costs[0],
                    dp[max(i-7, 0)] + costs[1],
                    dp[max(i-30, 0)] + costs[2],
                )
            
        return dp[-1]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [0] * (days[-1] + 1)
        pos = 0
        
        for i in range(1, days[-1] + 1):
            if i == days[pos]:
                d1 = i - 1
                d2 = i - 7 if i - 7 > 0 else 0
                d3 = i - 30 if i - 30 > 0 else 0
                dp[i] = min(dp[d1] + costs[0], dp[d2] + costs[1], dp[d3] + costs[2])
                pos += 1
            else:
                dp[i] = dp[i - 1]
                
        return dp[-1]


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        
        @lru_cache(None)
        def recur(day):
            if day > 365:
                return 0
            elif day in days:
                
                ret = math.inf
                for c, d in zip(costs, [1,7,30]):
                    ret = min(ret, c + recur(day + d))
                return ret
                    
            else:
                return recur(day + 1)
            
        
        return recur(0)

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        n = len(days)
        m = len(costs)
        ranges = [1, 7, 30]
        dp = [float('inf')] * (n+1) 
        dp[0] = 0
        #print(f\"n={n} m={m} dp={dp}\")
        for j in range(1, n+1):
            for i in range(1, m+1):
                for k in range(j, n+1):
                    if days[k-1] - days[j-1] >= ranges[i-1]:
                        break
                    #print(f\"k={k} i={i} j={j}\")
                    dp[k] = min(dp[k], costs[i-1] + dp[j-1])            
        return dp[n]


class Solution:
    def mincostTickets(self, arr: List[int], costs: List[int]) -> int:
        
        dp = [float('inf')]*(len(arr)+1)
        dp[0] = 0
        
        for i in range(len(arr)):
            
            j = i
            prev = dp[i]
            
            while j < len(arr) and arr[j] < arr[i] + 30:
                
                if arr[j] == arr[i]:
                    dp[j+1] = min(dp[j+1],prev+costs[0],prev+costs[1],prev+costs[2])
                elif arr[j] - arr[i] < 7:
                    dp[j+1] = min(dp[j+1],prev+costs[1],prev+costs[2])
                elif arr[j] - arr[i] < 30:
                    dp[j+1] = min(dp[j+1],prev+costs[2])
                    
                j += 1
                
        print(dp)
                    
        return dp[-1]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        min_costs = [-1 for i in range(days[-1])]
        if 1 in days:
            min_costs[0] = min(costs)
        else:
            min_costs[0] = 0
        return self.helper(days, costs, min_costs, days[-1] - 1)
    
    def helper(self, days: List[int], costs: List[int], min_costs: List[int], i) -> int:
        if i == 0:
            return min_costs[0]
        if i < 0:
            return 0
        if i + 1 not in days:
            min_costs[i] = self.helper(days, costs, min_costs, i-1)
        if min_costs[i] != -1:
            return min_costs[i]
        
        c_1 = self.helper(days, costs, min_costs, i-1)
        c_7 = self.helper(days, costs, min_costs, i-7)
        c_30 = self.helper(days, costs, min_costs, i-30)
        
        min_costs[i] = min(c_1 + costs[0], c_7 + costs[1], c_30 + costs[2])
        return min_costs[i]
        
            
        
        
        
# Brute force: branch for each ticket type: O(3^n)

# min_cost(i) = minimum cost of traveling i days. (1-indexed).


# min_cost(i) = min(
# (min_cost(i-1) + costs[0]), 
# (min_cost(i-1) + costs[1]), 
# (min_cost(i-1) + costs[2]))
'''
min_cost(1) = min(costs[0], costs[1], costs[2])

min_cost(i) = min cost of traveling up to the days[i]th day.
min_cost(0) = min(costs[0], costs[1], costs[2])
min_cost(i < 0) = min(costs[0], costs[1], costs[2])
min_cost(i) = (min_cost(i-1) + costs[0], min_cost(i-7) + costs[1], min_cost(i-30) + costs[2])




min_cost(i) = minimum cost of traveling i days, (1-indexed).
min_cost(1) = min(costs[0], costs[1], costs[2])
min_cost(i < 1) = min(costs[0], costs[1], costs[2]) ???
min_cost(i) = (min_cost(i-1) + costs[0], min_cost(i-7) + costs[1], min_cost(i-30) + costs[2]) 

**** This doesn't account for the case where a longer pass may be cheaper.


'''

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [0] * (days[-1] + 1)
        for day in range(0, days[-1] + 1):
            if day not in days:
                dp[day] = dp[max(0, day - 1)]
            else:
                dp[day] = min(dp[max(0, day - 1)] + costs[0], dp[max(0, day - 7)] + costs[1], dp[max(0, day - 30)] + costs[2])

        return dp[-1]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [float('inf')] * (len(days) + 1)
        dp[0] = 0
        for i in range(1, len(days) + 1):
            j = 0
            while i + j < len(dp):
                if days[i + j - 1] <= days[i - 1]:
                    dp[i + j] = min(dp[i + j], dp[i - 1] + costs[0])
                if days[i + j - 1] <= days[i - 1] + 6:
                    dp[i + j] = min(dp[i + j], dp[i - 1] + costs[1])
                if days[i + j - 1] <= days[i - 1] + 29:
                    dp[i + j] = min(dp[i + j], dp[i - 1] + costs[2])
                else:
                    break
                j += 1
        return dp[-1]


def prev(x,day,dp):
    ans=0
    for i in day:
        if i<=x:
        #    print(dp[i],i,x)
            ans = dp[i]
        else:
            break
    return ans
class Solution:
    def mincostTickets(self, day: List[int], cos: List[int]) -> int:
        dp=[10000000]*(370)
        tmp=[0]*32
        tmp[1]=cos[0]
        tmp[7]=cos[1]
        tmp[30]=cos[2]
        day.sort()
        n = len(day)
        for i in range(n):
            d = day[i]
            for j in [1,7,30]:
                ab = prev(max(0,d-j),day,dp) + tmp[j]
                #print(ab)
                dp[d] = min(dp[d] , ab)
        #print(dp[200:])
        return dp[day[-1]]
                
            
        
        


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [0] * (days[-1]+1)
        dp[0] = 0
        for j in range(len(days)):
                i = days[j]
                for k in range(1,i+1):
                    if k == i:
                        if k-7 >= 0 and k-30 >= 0:
                            dp[k] = min(dp[k-1]+costs[0], dp[k-7]+costs[1], dp[k-30]+costs[2])
                        elif k-7 >= 0:
                            dp[k] = min(dp[k-1]+costs[0], dp[k-7]+costs[1], costs[2])
                        else:
                            dp[k] = min(dp[k-1]+costs[0], costs[1], costs[2])
                    else:
                        if k != 1:
                            if dp[k] == 0:
                                dp[k] = dp[k-1]
        return dp[-1]
                    
        


class Solution:
    '''
    days = [1,4,6,7,8,20]
    dp   = 11 9 8 6 4  2
            2 7        2
    '''
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        # if costs = [7,2,15] change to [2,2,15]
        for i in range(1,-1,-1):
            if costs[i] > costs[i+1]: costs[i] = costs[i+1]
    
        N = len(days)
        dp = [0 for _ in range(N+1)]
        dp[-2] = costs[0]
        dp[-1] = 0    # trick to avoid list index out of bounds
        
        for i in range(N-2,-1,-1):
            dp[i] = costs[0] + dp[i+1]
            for j in range(i+1,N):
                if days[j] - days[i] < 7:
                    dp[i] = min(dp[i], costs[1] + dp[j+1])
                elif days[j] - days[i] < 30:
                    dp[i] = min(dp[i], costs[2] + dp[j+1])
        return dp[0]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [float('inf') for _ in range(days[-1] + 1)]
        dp[0] = 0
        set_days = set(days)
        for i in range(1, len(dp)):
            if i not in set_days:
                dp[i] = dp[i - 1]
            else:
                dp[i] = min(dp[max(0, i - 1)] + costs[0],
                            dp[max(0, i - 7)] + costs[1],
                            dp[max(0, i - 30)] + costs[2])
        return dp[-1]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        k = len(costs)
        n = len(days)
        dp = [[0] * (k+1) for _ in range(n)]

        for i in range(n):
            dp[i][0] = float('inf')

            for d in range(i):
                if days[i] - days[d] < 30:
                    dp[i][0] = min(dp[i][0], dp[d][3])
                if days[i] - days[d] < 7:
                    dp[i][0] = min(dp[i][0], dp[d][2])

            for j in range(1, k+1):
                if i == 0:
                    dp[i][j] = costs[j-1]
                else:
                    dp[i][j] = costs[j-1] + min(dp[i-1])

        return min(dp[-1])


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        n = len(days)
        if not days:
            return 0
        
        total = [float('inf') for _ in range(n)]
        total[0] = min(costs)
        
        for i in range(1,n):
            curday = days[i]
            total[i] = total[i-1]+min(costs)
            
            for j in range(i-1, -1, -1):
                diff = curday-days[j]
                if diff < 7:
                    if j>0:
                        total[i] = min(total[i], total[j-1]+costs[1])
                    else:
                        total[i] = min(total[i], costs[1])
                
                if diff < 30:
                    if j>0:
                        total[i] = min(total[i], total[j-1]+costs[2])
                    else:
                        total[i] = min(total[i], costs[2])
                
                else:
                    break
        
        return total[-1]


from collections import defaultdict
class Solution:
    def mincostTickets(self, days: List[int], cost: List[int]) -> int:
        dp = defaultdict(int)
        days = set(days)
        
        for i in range(365, 0, -1):
            if i in days:
                dp[i] = min(dp[i+1] + cost[0], dp[i+7] + cost[1], dp[i+30] + cost[2])
            else:
                dp[i] = dp[i+1]
        
        return dp[1]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        dp = [float('inf') for d in range(days[-1]+1)]
        dp[0] = 0
        pass_num_days = [1, 7, 30]
        days_set = set(days)
        
        for day in range(days[-1]+1):
            if day not in days_set:
                dp[day] = dp[max(0, day-1)]
            else:
                for num_days, cost in zip(pass_num_days, costs):
                    dp[day] = min(dp[day], dp[max(0, day-num_days)] + cost)
        print(dp)
        return dp[-1]    

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        given = {}
        for day in days:
            given[day] = True
        
        dp = [0 for _ in range(days[-1]+1)]
        dp[1] = costs[0]
        for day in range(days[-1]+1):
            if day not in given:
                dp[day] = dp[day-1]
            else:
                # purchase one day pass
                one_day_pass = dp[max(0, day-1)] + costs[0]
                # purchase one week pass
                one_week_pass = dp[max(0, day-7)] + costs[1]
                # purchase one month pass
                one_month_pass = dp[max(0,day-30)] + costs[2]
                dp[day] = min(one_day_pass, one_week_pass, one_month_pass)
        print(dp)
        return dp[-1]

class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        
        cost = [0 for j in range(366)]
        
        for i in range(len(cost)):
            if(i in days):
                cost[i] = min((cost[i-1] if i-1>=0 else 0) + costs[0], 
                              (cost[i-7] if i-7>=0 else 0) + costs[1], 
                              (cost[i-30] if i-30>=0 else 0) + costs[2])
            else:
                cost[i] = cost[max(i-1,0)]
            
        return cost[365]
        
#         cost = [0 for j in range(days)]
        
#         for i, d in enumerate(days):
#             if(d <= 7):
#                 cost[i] = cost[i-1] + c[0] # take day pass
#             if(d <= 30): # choose between day pass and month pass
#                 cost[i] = min(cost[i-1]+c[0], cost)


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        if not days: 
            return 0
        lastday = days[-1]
        
        dp = [0] * (lastday + 1)
        
        for i in range(1, lastday + 1): 
            if i not in days: 
                dp[i] = dp[i - 1]
                continue
            prev_1 = dp[i - 1] if i >= 1 else 0
            prev_7 = dp[i - 7] if i >= 7 else 0
            prev_30 = dp[i - 30] if i >= 30 else 0
            dp[i] = min(prev_1 + costs[0], prev_7 + costs[1], prev_30 + costs[2])
        
        return dp[-1]
            


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        if not days: 
            return 0
        
        dp = [0] * len(days)
        
        # given a day, find the dp index should look for
        # include start, not include end
        def search(start: int, end: int,  day:int)-> int:
            if start == end - 1: 
                if day >= days[start]:
                    return start
                else:
                    return -1
            m = int((end - start)/2) + start
            
            if day >= days[m]:
                return search(m, end, day)
            else: 
                return search(start, m, day)
                
        m = {day:i for i, day in enumerate(days)}
        
        for i,day in enumerate(days): 
            prev_1 = day - 1
            prev_7 = day - 7
            prev_30 = day - 30
            c_1 = costs[0] 
            c_7 = costs[1]
            c_30 = costs[2]
            if prev_1 in m: 
                c_1 += dp[m[prev_1]]
            elif prev_1 >= 0 and prev_1 >= days[0]:        
                c_1 += dp[search(0, i, prev_1)]
            if prev_7 in m:
                c_7 += dp[m[prev_7]]
            elif prev_7 >= 0 and prev_7 >= days[0]:
                c_7 += dp[search(0, i, prev_7)]
            if prev_30 in m:
                c_30 += dp[m[prev_30]]
            elif prev_30 >= 0 and prev_30 >= days[0]:
                c_30 += dp[search(0, i, prev_30)]
                
            dp[i] = min(c_1, c_7, c_30)
        
        return dp[-1]
            


class Solution:
    def mincostTickets(self, days: List[int], costs: List[int]) -> int:
        if not days:
            return 0
        dp = {}
        def getMin(idx, valid_count_past):
            if idx == len(days):
                return 0
            if days[idx] <= valid_count_past:
                return getMin(idx+1, valid_count_past)
            if idx in dp:
                return dp[idx]
            m1 = costs[0] + getMin(idx+1, days[idx])
            m7 = costs[1] + getMin(idx+1, days[idx]+6)
            m30 = costs[2] + getMin(idx+1, days[idx]+29)
            dp[idx] = min(m1,m7,m30)
            return dp[idx]
        
        return getMin(0,0)