Approach

[translate by ChatGPT]

This problem gives a list of travel days (days) and the costs of 1-day, 7-day, and 30-day passes. We need the minimum cost to cover all travel days.

This clearly suggests a DP solution. My first idea was to maintain an ansAry where each entry stores the optimal cost up to day i. For each day, we check day-1, day-7, and day-30 and take the minimum.

However, some days are not in days. For example, if travel days are 4 and 7, then when evaluating day 13, the “7 days before” point is day 6, which doesn’t exist in the travel list. We actually need the cost at day 4. One idea was to fill all non-travel days with the previous result, but that felt wasteful, so I tried using a map with upper_bound.

This saves space in sparse cases, but I later realized this approach isn’t ideal in worst-case scenarios. If travel happens every day (up to 365 days), the map becomes large and complexity increases. This made me realize I should always consider all corner cases to understand worst-case complexity.

Another small trap in this problem is that the 7-day pass can be cheaper than the 1-day pass. I noticed this possibility while solving, but still subconsciously assumed it wouldn’t happen—so it’s something to stay alert about.

Also, the map’s first entry should be initialized with a value outside the valid range, so that when we query dates earlier than the range, we can still retrieve a valid cost.

Code

class Solution {
public:
    int mincostTickets(vector<int>& days, vector<int>& costs) {
        // return MapSolution(days, costs);
        return VecSolution(days, costs);
    }

    int VecSolution(vector<int>& days, vector<int>& costs) {
        int lastDay = days[days.size() - 1];
        vector<int> dp(lastDay + 1, 0);
        
        int i = 0;
        for (int day = 1; day <= lastDay; day++) {
            if (day < days[i]) {
                dp[day] = dp[day - 1];
            } else {
                i++;
                dp[day] = min({dp[day - 1] + costs[0],
                               dp[max(0, day - 7)] + costs[1],
                               dp[max(0, day - 30)] + costs[2]});
            }
        }
     
        return dp[lastDay];
    }

    int MapSolution(vector<int>& days, vector<int>& costs) {
        std::map<int, int> ansMap; // the minimum cost with day 0~i, and the pass is ended before day i
        ansMap[0] = 0; // not cost need for day 0
        int ans = std::min(std::min(costs[0],costs[1]),costs[2]); // first days cost
        ansMap[days[0]] = ans;
        
        for (int i = 1; i < days.size(); ++i) {
            const auto& d = days[i];
            ans = std::min(
                findPrevCost(ansMap, d-1) + costs[0],
                std::min(
                    findPrevCost(ansMap, d-7) + costs[1],
                    findPrevCost(ansMap, d-30) + costs[2])
                );
            ansMap[d] = ans;
        }
        return ans;
    }

    int findPrevCost(std::map<int, int>& ansMap, int day) {
        auto it = ansMap.upper_bound(day); // First day > given key
        return (--it)->second;
    }
};