- Denisse's Newsletter
- Posts
- š Will You Be My LeetCode Valentine? ā¤ļø
š Will You Be My LeetCode Valentine? ā¤ļø
You deserve to open this email.

Thereās a reason 400,000 professionals read this daily.
Join The AI Report, trusted by 400,000+ professionals at Google, Microsoft, and OpenAI. Get daily insights, tools, and strategies to master practical AI skills that drive results.
TLDR
Welcome!
Recommendations I live by!
Resources to check out
Let me invoke you into my cult
JOBS & SCHOLARSHIPS
Hi and welcome back, brave human!
I am so damn proud of you.
For every:
late-night study session,
every failed LeetCode problem,
every job application you sent,
every time you pushed forward when it felt easier to quit,
you're doing amazing. š«
I often donāt talk about this but for the longest time, I watched as all of my friends got Valentine's gifts and secret admirers and Iā¦.. well was waiting for puberty to hit.
But not this year, this year, I am asking you, will you be my Valentine? š
Because if you're opening this email, it means you're investing in yourself, your growth, your skills, your future. And honestly, what better companion could you ask for than you?
And in case nobody tells you this,
Youāre worth celebrating. š
I am very proud of you.
LFG!
LeetCode problem
Day 7 : 983. Minimum Cost For Tickets
You need to buy train passes to cover specific travel days in a year while spending the least amount of money. There are three types of passes:
1-day pass (covers only one day)
7-day pass (covers seven consecutive days)
30-day pass (covers thirty consecutive days)
Given an array of days and costs, return the minimum number of dollars you need to travel every day.
Before we get started let's take a deep breathā¦This is f%$! hard but itās worth doing if you want to pursue a career in tech.
It took me some good time to wrap my head around this problem, so letās take it step by step!
What are we asked? Find the minimum cost required to buy train passes that cover all the given travel days.
If you purchase a multi-day pass, it covers all days within its validity period, so you donāt need to pay again for those days.
Now before we continue, there is an important realization youāll make when doing this problem that is also applicable to real life. It will only be profitable to purchase a ticket that covers multiple days if its cost does not exceed the total cost of buying single-day passes for the same period.
So for each day that we are traveling we need to calculate the cost of buying:
1 Day Pass
7 Day Pass
30 Day Pass
and figuring out which is, cheaper š¤ .
The keyword on identifying that it is a dynamic programming problem is āMinimum Costā.
Let's look at an example with days = [1,4,6,7,8,20], costs = [2,7,15]
This problem is tricky because we need to account for all travel days, therefore, we need to add all of the days to the DP array.
We initialize our dp array with:
vector<int> dp (days.back()+1, -1);
We set the dp array size to days.back() + 1
, which includes our last travel day, making sure we account for all travel days while calculating the minimum cost.
To efficiently check whether a given day is a travel day, we store all travel days in a set, allowing O(1) lookup time when determining whether we need to buy a pass for a particular day.
set<int> travelDays(days.begin(), days.end());
Remember all DP problems follow a formula:
Base Case: We know that on day 0, the cost is 0 since no travel has occurred:
dp[0] = 0; // No cost on day 0
Memoization: We'll use an array dp where dp[i] represents the minimum cost required to travel up to day
i
.If
i
is not a travel day, dp[i] = dp[i - 1] (carry forward the previous day's cost).
Formula : Calculate the cost of the three options and find the cheapest.
int dayPass = dp[x-1]+costs[0];
int sevenDayPass = dp[max(0,x-7)]+costs[1];
int thirtyDayPass = dp[max(0, x-30)] + costs[2];
dp[x] = min({dayPass,sevenDayPass,thirtyDayPass});

Minimum Cost for Tickets Solution
What is the O notation? |
class Solution {
public:
int mincostTickets(vector<int>& days, vector<int>& costs) {
vector<int> dp (days.back()+1, -1);
dp[0]=0;
set<int> travelDays(days.begin(), days.end());
for(int x=1; x<=days.back(); x++){
if( travelDays.find(x)==travelDays.end()){
dp[x] = dp[x-1];
continue;
}
int dayPass = dp[x-1]+costs[0];
int sevenDayPass = dp[max(0,x-7)]+costs[1];
int thirtyDayPass = dp[max(0, x-30)] + costs[2];
dp[x] = min({dayPass,sevenDayPass,thirtyDayPass});
}
return dp[dp.size()-1];
}
};
Recommendations
Level up your inbox by subscribing to what I am subscribed to!
Let me invoke you into my cult
Media worth seeing
Jobs and Scholarships
Cheers to you hacking your week!
Denisse
Reply