Advertisement

Minimum Cost For Tickets - LeetCode 983 Solution

Minimum Cost For Tickets - Complete Solution Guide

Minimum Cost For Tickets is LeetCode problem 983, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 days . Each day is an integer from 1 to 365 . Train tickets are sold in three different ways : a 1-day pass is sold for costs[0] dollars, a 7-day pass is sold for costs[1] dollars, and 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: 2 ,

Detailed Explanation

The problem asks us to find the minimum cost to travel on specific days of the year, given three types of tickets: a 1-day pass costing `costs[0]`, a 7-day pass costing `costs[1]`, and a 30-day pass costing `costs[2]`. We are given a list of days `days` on which we need to travel. The goal is to minimize the total cost of tickets purchased to cover all travel days. `days` is a sorted array of integers from 1 to 365, and `costs` is an array of length 3 representing the costs of the three types of passes.

Solution Approach

The solution uses dynamic programming to calculate the minimum cost to travel up to each day. It iterates through each day from 1 to the last travel day. If the current day is not a travel day, the minimum cost is the same as the previous day. If the current day is a travel day, it considers the cost of buying a 1-day pass, a 7-day pass, and a 30-day pass, and chooses the minimum among these options. The DP array `dp[i]` stores the minimum cost to travel up to day `i`.

Step-by-Step Algorithm

  1. Step 1: Create a set (or equivalent data structure) `travel_days` to store all the days on which we need to travel. This helps in O(1) lookup.
  2. Step 2: Create a DP array `dp` of size `last_day + 1`, where `last_day` is the last day on which we travel. Initialize all elements to 0.
  3. Step 3: Iterate from `i = 1` to `last_day`. For each `i`, check if `i` is a travel day (i.e., present in `travel_days`).
  4. Step 4: If `i` is not a travel day, then `dp[i] = dp[i-1]` (no additional cost as we don't need to travel).
  5. Step 5: If `i` is a travel day, calculate the cost of buying each type of pass and choose the minimum cost:
  6. Step 6: - `cost1 = dp[i-1] + costs[0]` (buy a 1-day pass).
  7. Step 7: - `cost7 = dp[max(0, i-7)] + costs[1]` (buy a 7-day pass, consider edge case where `i-7 < 0`).
  8. Step 8: - `cost30 = dp[max(0, i-30)] + costs[2]` (buy a 30-day pass, consider edge case where `i-30 < 0`).
  9. Step 9: - `dp[i] = min(cost1, cost7, cost30)`.
  10. Step 10: Return `dp[last_day]`, which represents the minimum cost to travel up to the last travel day.

Key Insights

  • Insight 1: This is a dynamic programming problem where the optimal cost for day `i` depends on the optimal cost of previous days.
  • Insight 2: We can build a DP array where `dp[i]` represents the minimum cost to travel up to day `i`.
  • Insight 3: For each day, we need to consider three options: buy a 1-day pass, a 7-day pass, or a 30-day pass and choose the option that gives the minimum cost.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Dynamic Programming.

Companies

Asked at: Flipkart, Grab, Intuit, Snap, Turing, Zepto.