The Minimum Cost For Tickets problem asks you to find the cheapest way to travel on specific days of the year. You can buy three types of tickets:
days you need to travel. The goal is to cover all those days with the minimum total ticket cost.This is an extremely popular Minimum Cost For Tickets interview question at companies like Amazon, Google, and Microsoft. It is a textbook example of Dynamic Programming. It tests your ability to define a state and a recurrence relation where the current choice (which ticket to buy) depends on the optimal cost of covering future or past days.
The Dynamic Programming interview pattern (either top-down with memoization or bottom-up) is the standard solution.
dp[i] represents the minimum cost to cover all travel days from day i to the end of the year.i is not a travel day, dp[i] = dp[i+1].i IS a travel day, you have three choices:
cost[0] + dp[i+1]cost[1] + dp[i+7]cost[2] + dp[i+30]Days: [1, 4, 6, 7, 8, 20], Costs: [2, 7, 15].
2+2+2+2 (four 1-day passes = 8) against 7 (one 7-day pass).Practice both the Top-Down and Bottom-Up versions of this Dynamic Programming coding problem. Interviewers often ask to optimize the space complexity. Since a pass only lasts 30 days, you can actually solve this using only space with a deque.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Maximum Length of Valid Subsequence II | Medium | Solve | |
| Combination Sum IV | Medium | Solve | |
| Find the Maximum Length of Valid Subsequence I | Medium | Solve | |
| Maximum Absolute Sum of Any Subarray | Medium | Solve | |
| Minimum Score Triangulation of Polygon | Medium | Solve |