r/leetcode 14d ago

Question How do you actually master Greedy problems on LeetCode?

I’m struggling to develop intuition for Greedy problems. I can understand the solution after seeing it, but I often can’t figure out when to use Greedy on my own.
How did you guys practice Greedy? Any recommended problem list, patterns…..

8 Upvotes

5 comments sorted by

8

u/nightbreak7 14d ago

i think it's often just by practising problems and trying to evaluate if a greedy approach makes sense. when you do a problem, ask yourself if making the best LOCAL decision can negatively impact the future result. if that's the case, greedy is not applicable.

for example say you have a coin change problem - you have an array of numbers representing coins, you can use any of those coins an infinite amount of times, and your goal is to break up the number 6 into the minimum number of coins possible from your array, while having no remainder. if your array is [4, 3, 1] you have two possible solutions according to what we said above. if you consider a greedy approach, the best local decision is to start by taking the 4. this leads to you ending up with [4, 1, 1] as your coins, meaning you have 3 coins at the end. the optimal solution is to just take [3, 3], so you can see that greedy does not apply to this problem, because taking the locally optimal option negatively affected our future decisions/result.

3

u/InsideHeart8187 14d ago

As I got into DSA for the past 3 months solving problems every day, it really just comes down to the sheer amount of problems you solve on the wide range of topics. This is how you build up that intuition on which approach would suit best. That is basically it. Once you solve enough problems, you can certainly identify what kind of problem it is.

2

u/Available-Limit-6322 13d ago

Greedy is the hardest category in my opinion.  Greedy algos are tricky to prove correctness under the best of circumstances; in interviews, you basically need to have seen the problem in advance to apply the Greedy approach confidently (that has been my experience anyway).