This video demonstrates four distinct algorithmic approaches for solving competitive programming problems: (1) Digit Frequency Score uses simple iteration to sum digit values by converting characters to numbers; (2) Maximum Number of Items From Sale I employs dynamic programming where DP[budget] = max(DP[budget], DP[budget - price] + gain) handles unlimited item purchases; (3) Maximum Number of Items From Sale II combines greedy selection with divisor counting to maximize item acquisition from packages; (4) Lexicographically Maximum MEX Array uses suffix MEX calculations to construct the lexicographically largest array by repeatedly selecting the minimum excluded value. These solutions showcase fundamental techniques including DP state transitions, greedy optimization, and suffix-based computations that are essential for competitive programming contests.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
LeetCode Weekly 504 | Solutions by Former LeetCode Contest CoordinatorAdded:
Hey folks.
Welcome to the up solving session of lead code weekly contest 504.
I'm Arpa from algorithmic academy and I participated in this contest.
As you can [snorts] see, I have one I have half an hour remaining.
And I solve all problems. Let me show you I solve the first problem in 1 minute.
The second problem in 19 minutes.
Then the third problem 21 minutes.
And finally the last problem in 13 minutes.
I'm going to show you the problems and solutions one by one and also implementation.
Let's start from the first problem. This problem is simply asks us is simply asking us to find the sum of digits of a number.
Which can be >> [snorts] >> easily calculated.
Uh First I set my answer to zero. I go over all characters of this uh number.
And finally I add this value to the answer.
As you know this is character zero.
And this the difference will return the character itself.
>> [snorts] >> And I will add it to answer and finally return the answer.
Let's move on.
>> [snorts] >> The next problem This problem is asking us to find the maximum number of uh items we can obtain.
So, what we should do here, I mean, at least my solution is DP.
So, uh for each item, either I'm going to pick or I'm not going to pick.
And if I'm going to pick, it will have it will come with some copy with some copies, which I named gain.
So, let's define DP.
The DPB means that DPB means that if I have this budget, what is the maximum number of items I can buy?
Uh this is the definition for the DPB.
So, consider I calculated DPB for the elements uh before the current item, >> [snorts] >> and I want to make the new DP. So, I have some old DP and some new DP.
I make my new new DP based on old DP.
Uh Um what I'll do is so maybe I'm not going to use this item at all, so DPB will be equal to old B.
Or I'm going to use it.
And it matters how many times I'm going to use it.
Because if I use this item several times, it will yeah.
So let me write like this.
DPB is maximum of old B and old B minus item Oh, no, sorry. Let's say price.
price Uh >> [snorts] >> B minus price plus plus gain.
Okay.
plus K Gain is how many multiples this item has in the entire array and it includes itself.
It includes itself, too, okay?
So gain will be something like this.
And DP of B minus price plus one.
Why?
Because I could I can use this item several times. So, this is for handling that case.
Let me show you.
Okay, I start this uh some empty DP. I go over all items.
Uh I make the old I define gain.
>> [snorts] >> I go over all items.
If this item is divisible by if that item is divisible by this item, I'll add one to gain.
I'll go over all these different budgets.
>> [snorts] >> First of all, DPB is maximum of DPB and DPB minus one.
If I can have uh one money less, one budget less, so that's okay, too.
Then, if I can purchase at least one item from this item, uh DPB will be maximum of DPB old of DP old of B minus item one plus gain or DP of uh B minus item plus one.
And finally, returning DP of budgets.
Great. Let's move on to the next problem.
Uh This problem is a bit different.
In this problem, there are some package you can buy that they'll give you two copies.
And some other package you can buy and just gain one copy.
Okay, so I'll start with picking some options that give me two copies.
Like I'll pick them and pick them and pick them.
And finally I'll I'll start picking some options that will give me just one item, okay?
So, first let's calculate minimum price over all prices.
Which is going to show us that when I want I reach the point that I'm going to um choose just items to gain one copy, what is the minimum price?
>> [snorts] >> Okay, let's put that aside. Let's make my package that I can gain two items by picking them.
Okay.
Uh Those packages >> [snorts] >> are coming from an item and its multiple.
So, for each item let's calculate how many multiples this item has.
Then we that, I can find how many packages I can make using this item. Like, if this item has three other items that they are multiples of this, I can make three packages using this item that buying each of them costs like this item, but gives me two copies, because I have this and also that.
And how I can do that?
Uh, let's see how I can do that.
Okay.
So, I define a function that gives me divisors of a number.
And I cache the answers to make sure uh, I'm not going to calculating this two I mean, two times.
By the way, I think that the this can be solved even without this.
Let's try.
Um, yeah.
Yeah, even with without that, it can be solved.
Okay.
So, this function gives me divisors of number X. That's it, okay?
Okay.
So, I'll go over all divisors of this item, and I'll add one to multiples of this number, this divi- this divisor.
Okay?
And also I update my mean price.
Then I have a vector of options.
These options are pairs.
The first one shows The first one shows the price for this item. The second one shows how many packages of this price I have.
Then what I'm what I'm going to what I what I'm going to do is Mhm.
Maybe this is a better implementation.
Mhm?
Seems a bit neater.
Yeah.
Let's up Let's get Oh.
Oh.
It goes to long long and what it was, so >> [snorts] >> I don't prefer that.
Okay?
And let's continue as what we were what we are doing. Okay?
Okay.
So, I have these number of uh options of this price.
Then I define my answer. My answer is simply budget divided by mean price at the beginning.
I sort options based on their price because all of them give me just two items. So, why not to sort?
And current means that how many options I already picked.
Then I go over all options.
I can't pick more than budget divided by option for sure.
Then I do but I decrease the budget by the cost of all copies I want to pick from this option.
>> [snorts] >> Then I'll set I'll add all them to current.
And with the remaining of budget, I'll buy those initial uh items with mean price.
Yeah, that's it. That's it about Q3.
Let's move on.
Move on to the next problem.
Q4.
Um okay.
This is actually easier than the last two problems at least for me.
Um to make the array lexicographically maximal, for sure my choice will be something that comes with max of the entire array.
Good.
So, the first element of result is the most important element of result for sure.
Okay?
So, I need to maximize it.
And what is the maximum possible value for the first element of the result?
That is mex of nums itself.
Okay. So, I need to calculate mex of result.
Mhm, sorry. Calculate mex of nums.
And then find the minimum length prefix of nums that has this mex.
And then repeat and repeat and repeat.
Okay.
To do that, I'll come with some suffix mex, like partial mex.
Okay, from uh from right to left, from the end.
Uh I'll come with that.
Using that, I can calculate I I I can have mex for all the suffixes of all the possible suffixes.
Then, what I'm going to do is is starting from some point.
This is And then setting the target for mex.
Then I go as uh then I go until mex of this subarray is equal to goal.
I'll add this mex to the answer, to the result.
Then I start I start all over again.
Look, like this.
I define this function which this array which will be equal to suffix mix of I. Mhm, this is pretty easy to maintain. I mark it. While this is marked, I add one to mix. That's it.
And finally, I reset the array mark. The array mark is global array here.
Then, I have my answer.
Now, I go over I, J, and I have my mix.
If I is equal to J or mix is still less than what I need, I go over Um I I will add this number, number J, and I'll update mix.
Okay.
Then, I'll add mix the answer, the current mix the answer, and I'll remove all numbers from mark and restart all over again.
Yeah. That's it.
Uh we solve all problems in 20 minutes.
Um thank you for watching, guys.
Join us very soon in the next of solving sessions of Codeforces and LeetCode contests. Thank you. Bye-bye.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











