Greedy Algorithms Tutorial – Solve Coding Challenges

297,474
0
Published 2022-06-27
Learn how to use greedy algorithms to solve coding challenges. Many tech companies want people to solve coding challenges during interviews and many of the challenges can be solved using a greedy algorithm. A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage.

✏️ Course from Tanishq Chaudhary. Check out his channel: youtube.com/c/TanishqChaudhary1337/

Links to coding challenges on InterviewBit:
🔗 Highest product: www.interviewbit.com/problems/highest-product/
🔗 Bulbs: www.interviewbit.com/problems/interview-questions/
🔗 Disjoint intervals: www.interviewbit.com/problems/disjoint-intervals/
🔗 Largest permutation: www.interviewbit.com/problems/largest-permutation/
🔗 Meeting rooms: www.interviewbit.com/problems/meeting-rooms/
🔗 Distribute candy: www.interviewbit.com/problems/distribute-candy/
🔗 Seats: www.interviewbit.com/problems/seats/
🔗 Assign mice to holes: www.interviewbit.com/problems/assign-mice-to-holes…
🔗 Majority element: www.interviewbit.com/problems/majority-element/
🔗 Gas station: www.interviewbit.com/problems/gas-station/

⭐️ Course contents ⭐️
⌨️ (0:00:00) Greedy introduction
⌨️ (0:04:32) Bulbs
⌨️ (0:12:11) Highest product
⌨️ (0:17:08) Disjoint intervals
⌨️ (0:28:43) Largest permutation
⌨️ (0:38:30) Meeting rooms
⌨️ (0:49:25) Distribute candy
⌨️ (1:03:13) Seats
⌨️ (1:19:13) Assign mice to holes
⌨️ (1:24:19) Majority element
⌨️ (1:35:28) Gas station
⌨️ (1:52:39) End

--

🎉 Thanks to our Champion and Sponsor supporters:
👾 Raymond Odero
👾 Agustín Kussrow
👾 aldo ferretti
👾 Otis Morgan
👾 DeezMaster

--

Learn to code for free and get a developer job: www.freecodecamp.org/

Read hundreds of articles on programming: freecodecamp.org/news

All Comments (21)
  • @mankritsingh
    A little help for Disjoint interval(I too got stuck on it) When you first see the question,you make make the following observations in the following order 1)We need to select sets without overlap.This is done by observing the start and end 2)As we know how to understand whether there is overlap or not,next we aim to find how to understand which set to take and which to avoid? 3)Now we see one thing that we can select the sets based on thier start,end or length. The length is not practical as a long set may not overlap with any of the element. 4)Now in start and end. Just think that when we look at the next set to add,which element sets the rule/limit? Its obvious that end is setting the limit. 5)Now we have formed the thinking,we will do the code part //code bool compare(vector &v1,vector&v2){ return v2[1]>v1[1]; } int Solution::solve(vector > &A) { sort(A.begin(),A.end(),compare); int ans=0,last=0;; for(vector it:A){ if(it[0]>last){ ans++; last=it[1]; } } return ans; }
  • Awesome tanishq, need more videos on greedy technique, leetcode problems are still a lot challenging and really struggling to "when to apply" greedy. Please provide more videos and practice problems.
  • Thank you so much for your efforts. And thanx Beau for posting such useful content on this channel
  • it’d be so so helpful if there were more videos like this on different topics
  • @yashkumar9671
    excellent video, thanks for the effort man. Helped me a lot
  • What amazing solutions for all the videos !!! especially for majority element. When you need space o(1) just use bits..wow will always keep this is mind. Thank you 🙏
  • Fun fact.. in Reinforcement Learning, if you know the optimal value function (which tells you how valuable it is to be in a given state), then the greedy policy with respect to that value function is the globally optimal policy! Sometimes the greedy approach is the best approach :)
  • @kingmaker9082
    In gas station question, you can avoid extra iteration like keeping condition if start>=n: break Thanks for the amazing tutorial 👍
  • @ritwik_shivam
    Amazing and very helpful video, I am daily referring this to learn greedy algorithm concepts, and finding it very trick as well, but your explanation, pace everything is fantastic. One small doubt for 28:51-38:31 largest permutation- def maxSwapsPossible(A,B): i = 0 max_i = len(A) d = {x:i for i, x in enumerate(A)} while B and i<len(A): j = d[max_i] if i==j: pass else: B-=1 A[i],A[j]=A[j],A[i] d[A[i]],d[A[j]] = d[A[j]],d[A[i]] i+=1 max_i-=1 return A # For input A = [4,5,2,1], B = 2 print(maxSwapsPossible([4,5,1,2],2)) #is giving key error, may be because you already fixed max_i = len(A) and it is not really necessary that max element will me at that position.
  • @garvgoel1743
    Hie @tanishq.... Loved the style of your coding. Learnt a lot from your coding style. Thank you❤
  • @karljuhaa9055
    Wow.......That' s so cool. Keep going Sir. TNice tutorials motivtes too.