To minimize the initial energy required to complete all tasks, process tasks in descending order of their delta (actual energy minus minimum energy required), as this greedy strategy ensures that tasks with larger energy consumption are completed when you have more energy available, reducing the total initial energy needed.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Daily Leetcode #2233: May 12, 2026 - 1665. Minimum Initial Energy to Finish TasksAdded:
Hey, hey everybody. This is Larry. This is another daily challenge. Hit the like button, subscribe button, join me on Discord. Let me know what you think about today. Guess where I am.
That's it. Hit all the buttons.
Hey. Hey everybody. This is Larry. This is day 12 of the Leo Day challenge. Hit the like button and subscribe button.
Join me in Discord. Let me know what you think about today's spot. Uh, let me know your guesses as to where I am. I don't think it's too much to too hard to guess. But yeah, today we have a hard problem. So, let's jump right to it. I am way tired. I woke up at 5. No, no. I woke up at I didn't sleep yesterday actually so that I could take um car service at 3:00 a.m. to the airport at 6:00 a.m. to get on a flight at 9:00 a.m. to get here at 300 p.m. And then somehow it took me 3 hours to get to get checked in. So that that's basically my entire day. It's been very tiring. I'm ready to pass out. So, let's take a look at today's hard problem. 1665 minimum in initial energy to finish tasks. The last time I was in this country, I actually did a thing where at the end of the uh of the video, I just show myself eating a random ice cream or popsicle. So, I'm going to do that again. Why not, right?
Unless I forgot. If I forget, leave a comment. Anyway, let's take a look. You give a away a task test of I act minimum uh actual surprises the amount of energy you spend to finish and minimum energy required to begin.
Right? For example, if task is 101 12 and your current energy is 11, you cannot start this task. However, your current is 13 complete and your energy will be three after it. Okay, that's kind of weird, but you can finish the task in any order you want. Minimum initial energy you will need to finish all the tasks. Um, okay. So I mean that that is immediately a binary search answer right um I mean that's not the only thing that we need to think about but binary search comes very intuitive because you have n things you have some you try to find some minimum uh and it kind of makes intuitive sense right because um because if you have more initial energy then you can finish it less than uh um and if you have less initial energy, then you're less likely to finish it.
And it's and intuitively it makes sense that this is a continuous curve, right?
Um meaning that there's no like jumps, right? Where like okay, five energy is good, six energy is no good, and then seven energy is good again, right?
That's what I mean by like these gaps.
Um and it doesn't look like it doesn't feel like this is that the problem that would happen. So binary search is going to be the first observation, right? The second is well okay given an initial energy X how do you finish it right um I think there are a couple of things we can maybe greedy on right require to begin is really weird all these okay so the first thing to notice is that all these are positive numbers right Um uh and the key thing there is just that you cannot have any minimum and also minimum is going it's over or I guess equal to actual so you also cannot go into the negative right um you have to finish all the tasks so there's no choice so what would you choose first right well you I I think trying to think right and there a couple of candidates that I'm thinking through a couple of price right one is just the delta between them right um but then the other one is that just the minimum energy has like my my intuition might be to um uh uh to just sort by the minimum energy required because well yeah uh because the more energy you need in the beginning then you just do them first, right? But it's more like a binary thing, right?
So maybe because basically like what I want to say is cuz I I don't know I I don't know how to sort it in a way but maybe there's some sort of like binary index or not bind a sorted list type thing where you kind of keep track of things but the idea here is that you want to be able to after completing a task still be able to um do all the other tasks, right? And you want to sort it in a way that supports that. Um and I think the minimum uh so all the things you can do then you do the minimum, right? So then I think I I I don't know quite but I think you want to do the minimum uh actual energy one where after solving it or doing it you still are able to do all of them right because I don't know because I think I have a case where like maybe you have like uh uh I don't know like one and eight right eight is the minimum one is actual Well, and then you have like uh I don't know that's a good one, but nine maybe like eight and eight and nine and one. Well, well, in this case, you obviously do the 91 first and then you do the eight and then you're Gucci, right? But what if you have like something like uh 18, right? Um I guess in this case, do you still do the nine first?
Well, let's say you have nine, right?
You wouldn't do the well you have 10 then you could do this first right and and then you could do this one because if you do this one first if you have 10 then this one just cannot be done right h um I think the first thing I would also try to think about when I think about sorting right uh when I think about this and you could kind of see the genesis of it um which I maybe could have came up with a little bit earlier is exchange principle, right? Meaning that um okay, let's say you have these two tasks, right? And we want to sort them. Um and you know, uh we sort we have to assume uh transitive right property, right?
Meaning that if A is less than B, then B is less than C then A is less than C.
Otherwise, you have [snorts] a a cycle kind of thing and you can you just can't do sorting on that, right? Okay. Okay.
So let's say that let's maybe assume that we do have a an ordering that allow us to sort. Then here what is the case?
Right? So then [snorts] how how would I represent this as two things that sort? Um so and does that depend on the current energy because if you have a lot of energy it doesn't matter. If you have 10 for example then you have to do this one first right because then we do this one doesn't then it doesn't right. So that's true for 10 and maybe 11 all the way up to like 17 or something and 17 then neither of these matter right uh or something like that 16 maybe actually but um so what's the principle here right about this uh idea of sorting And the the reason why I want to kind of compare these two things is that you know we only have one thing then we only compare one thing right um somehow but if we have like multiple then maybe you can have like oh you use the energy here plus this or something like that may subtract or some relationship that you can play around with. Um, I don't know yet though. Huh.
Do we always s I'm just looking at the examples to see if I have an idea.
And this this I guess they give you an example where you don't always greetly choose the 12 because you choose the 12 then you do I do it backwards maybe I'm thinking of it maybe binary search is wrong I don't know like how do you compare two tasks Is it just a doctor?
I mean this shows that well do they all have delta of six here? So is this just a random order or does that matter?
I think this is just like one of those shenanigan things they just try to show you. I think but also like it's kind of weird for this, right? Because in a way here all it's saying is that if the last task has 12 then you you have enough to do all the other ones and none of them actually matter, right? like the order doesn't matter anymore and you just kind of sum it up because you satisfy all of them. So that the energy has to be bigger than 12. And it's kind of the same on this case, right? Um maybe maybe we just choose which one is the last one. Does that work?
Okay. So let's say this is the last one, right?
Then you have this 981 thing.
[snorts] Um, and then none of the other ones matter because like you could go and just to kind of confirm that's not always the case. Well, that's not always the case. You could always just choose the the I'm trying to think, right? Um because none of these matters, right?
So, okay. So, let's say for example two, we choose the last one to be this one instead, right? Then now what does that look like? So, you have 12, you have 10.
So, your left over is going to be two.
That's that's fine, right? Um and then now you just add eight eight because whatever it is then and then the sum of it is going to be 8 + 10 + 2 so 20 21 so 21 thingies right um 21 + 12 it's going to be 33 right okay but the reason why is because this delta is two instead of 981 Okay.
And because you have two, that means that you have to sum and and all these things. This is going to be the leftover one, right?
But only the last one matters. Okay. So, let's say we do have a target. Maybe it doesn't matter whether we have the or depends on the the initial energy, right? So let's say we have 32 energy.
Uh what does that mean?
I'm trying to come up with a counter example where um what I said is kind of wrong, right? Where okay, let's say you have delta is zero, right? So you have I don't know 880 and then maybe you have like I don't know a th00and 1,000 and then 900, right? That's waste of 100.
Right.
So this doesn't matter even though this has quotequote the efficiency of whatever because you're still dominated by this and then now a th00and works but nothing more or less. I mean obviously more works but it's just not as efficient. Less obviously doesn't work.
But you want the delta to be as well.
Wait for this one. Why do we use 126? Do we have to use 126?
Let's say we the last one is 115, right?
You have six left over. I guess that's the same. Just before that then um just take six. So it's 18. But then 12 six would work anyway. So we just take six or 17. Okay. I guess that's for that one. It doesn't really matter.
H what I mean this one we have other answers right for for example 11 10 has corn efficiency of one because you just right uh one left over and then now you can do 8 n you just which is just eight and then 10 whatever Does it matter what order you do it in?
I mean, it does matter, of course, because we showed that the last number matters a little bit, but it's some proxy of the last number because here is not the last number. Um because you know um for example maybe you might even a more uh obvious example of like kind of 10 and then you get rid of seven um and then you have like one one right here.
This one doesn't matter at all.
How do you binary search or what does that mean? Like what order do you process them?
You always take the highest level. Take the delta delta and then sort it by the highest.
I don't get why that would work though necessarily.
I don't have a good idea.
Oh, here you I don't know that I know this one. I have some ideas. I mean, we could play around with it, but like like we have something like this where but doesn't I don't know where like the delta thing makes sense for me as to I think Because ideally you want the smallest delta things to be at the end as much as possible.
So you go through it this way and you're guaranteed that. it.
I don't have the minimum crypto though, right?
I guess there is some exchange idea of 88 which is delta of zero. You have like kind of 79.
We always want 88 last because then now this one would just go whatever way. But what if the minimum is really high cuz they're next to each other. But what if we have like 20 22?
I guess it still doesn't matter because this put here this number is you know plus 20 is still bigger. What if you have one one then now and then now in this case you have 21 now you have 23 right because you have to do well you start with 23 you go here is 22 and then you do this one right or you go 23 go this one and this one I guess the order doesn't matter in this case but in all the other cases this then here you want to do this one last and what what if you have like 30 30 you still want to do this one last and then this one you just add it on. So I guess like by case analysis it makes sense but um because this because if you do this one last you have a I see I think now I understand why um because I was thinking about what to do first but I think in in and um we kind of touched upon this about what what um task to do last and the thing is that the bigger the delta um you want to do them first only because then they're not last. And the last thing you do the the quoteunquote what I call efficiency which is which is the delta the delta you want that to be as small as possible because then now you have these scenarios where um well it just doesn't matter right I think we're And yeah, maybe maybe I don't know. Let's see go to I know it doesn't really matter.
Is that too big? It probably is too big.
Only goes up to 10 to the 9th or something.
10 to 10, right? Is it just regular binary search stuff? Um, if I'm not dumb enough to know this is good. What happens if this is good? This is good. Then we try a smaller number.
Right.
Something like this.
Why is my intenting like all weird?
Um, anyway, did I solve this? I guess so. What? I wonder what I wrote last time, but yeah.
Uh, for a min in tasks, if target is less than, then return force.
Uh, did I do this the right order? No, I do rest them. So I think this is right over there otherwise return true right something like that yolo summit h I think the I mean I don't really have a great explanation on this one I think the intuition is what I said about efficiency right um because I and I think a lot of it is just well one is the exchange principle right and Then the other is we did an exhaustiveish case analysis um of two deltas right and it's not a strict I mean it is a strict thing but because you can I use some examples but um but but you can kind of you know make them more abstract right for example what I did was like I let's say just say let's just do another one right 32 then there are two examples of a smaller delta right one is just that the more.
In this case, you you um well, what happens, right? What happens is that if you use this one last, then now you have a cost of 33 to start because you have 32, then now you have three and then one, right? So, um so yeah. So then, so then here 33 would be the case if you start with um start with this one as your first thing.
Wait, no, I can confuse myself. No, if you use this one as your first thing um and then this one, then you only need 32, right? So yeah, which is what I said actually, right? Um so 32 I think 32 is using the other way, right? So here in this case, um this is one one last.
Uh this one is three 32 last, right? Um so here we can see that we always want this one last, right? Um and then the and then another case might be just something that's more I don't know, let's say dirty dirty, right? Um in this case um we we want this one to be last right. So if this one is last then you have 60 um excuse me you could play around with more examples but then but I'm just going over like all the possibilities of um the min with respect to the 32 right so let's say you have these two right um and then How uh let's say we have 8080 less. What what do we get?
Is it 112?
No. Uh no, no, no. It's just 110, right?
Cuz you have 110. That's bigger than this. And then Yeah. Right. And then the other one is if you use 30 32 less, then this is 112, right? because here you so in this ca I mean and these are the scenarios of like this uh the delta is small smaller anyway you can probably use different numbers I chose them zero for easier math um but yeah so this is basically the exchange argument right so that's that explains the sorting um in exhaustive search kind of way as long as the delta is smaller because you would never picked this one to be earlier because it just has more efficiency what as I call it and and you know these are the three scenarios kind of so that's kind of like the abstract about it and that's what I was kind of working out in my mind but this is ridiculous I don't know uh actually let me take a look what did I do last time uh that was a long time ago actually uh apparently I was I mean I did the same thing last time but maybe I've watched the video to understand what the hints.
Yeah, we got that one. Figure out a sorting pattern. Thanks. Um I don't know.
But yeah, but that's it. That's all I have for today. Let me know what you think. Thanks for watching. Stay good.
Stay healthy to good mental health. I'll see y'all later and take care. Bye-bye.
Honestly, this is just as good as I remembered it.
Thanks for watching everybody. Good night. Bye-bye.
it.
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
Re: ๐ฃ๏ธ๐theprophedu๐2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 viewsโข2026-06-04
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











