Task Scheduler | Easy Intuition | Using Heap | Leetcode 621 | codestorywithMIK

  Рет қаралды 12,534

codestorywithMIK

codestorywithMIK

4 ай бұрын

Whatsapp Community Link : www.whatsapp.com/channel/0029...
This is the 14th Video of our Playlist "Heap (Priority Queue) : Popular Interview Problems".
In this video we will try to solve a very good and famous heap problem : Task Scheduler | Easy Intuition | Using Heap | Leetcode 621 | codestorywithMIK
NOTE - I will make a separate video for discussing another very good Greedy Approach for this problem.
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Task Scheduler | Easy Intuition | Using Heap | Leetcode 621 | codestorywithMIK
Company Tags : META
My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
GfG Problem Link : leetcode.com/problems/task-sc...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
Approach Summary : Simply use a greedy approach and get rid of tasks having max frequency so that we don't have to deal with them alone at last.Story frequency in max-heap. Start processing n+1 tasks(in descending order form max-heap) everytime and reduce the frequency of tasks by 1. If they are still remaining, push them back to heap.And keep calculating the time for processing the tasks. Once the heap is empty. Return the total time taken.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Timelines
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024

Пікірлер: 53
@shreyasudhakaran42
@shreyasudhakaran42 4 ай бұрын
Amazing Content 🎉😊
@gouravdutta9796
@gouravdutta9796 4 ай бұрын
Thanks sir for the awesome explanation. Here is the Java Code for the above explanation : class Solution { public int leastInterval(char[] tasks, int n) { HashMap map = new HashMap(); for(char ch : tasks){ map.put(ch, map.getOrDefault(ch, 0) + 1); } PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); for(Map.Entry entry : map.entrySet()){ pq.add(entry.getValue()); } int countInterval = 0; while(!pq.isEmpty()){ ArrayList temp = new ArrayList(); for(int i=1; i 0){ pq.add(val); } } if(!pq.isEmpty()){ countInterval += n+1; } else{ countInterval += temp.size(); } } return countInterval; } }
@vedbhanushali608
@vedbhanushali608 4 ай бұрын
what a intuition thanks sir, never thought of that.
@patelpriyanshu7830
@patelpriyanshu7830 4 ай бұрын
Teaching style is mind blowing Sir.💫💫
@aswinks9889
@aswinks9889 17 күн бұрын
Best ever
@ManishKumar-kb1wx
@ManishKumar-kb1wx 4 ай бұрын
As always bhaiyaa Amazing Explanation ✨
@user-ub2is4rs4x
@user-ub2is4rs4x 4 ай бұрын
Mind blowing. Your videos exactly contain what i am always looking for
@Thriftinghai
@Thriftinghai 4 ай бұрын
You are just different from other KZbinrs . Loved the explanation
@mdsharique2426
@mdsharique2426 4 ай бұрын
What an amazing explanation Sir!
@shraban8508
@shraban8508 4 ай бұрын
Good explanation sir.
@AnjuGupta-sz1uk
@AnjuGupta-sz1uk 4 ай бұрын
Best Explanation
@tutuimam3381
@tutuimam3381 4 ай бұрын
Amazing explanation ❤❤❤
@UECAshutoshKumar
@UECAshutoshKumar 4 ай бұрын
Thank you 😊
@anuppatankar4294
@anuppatankar4294 4 ай бұрын
Great video 👌🏻
@meme-ku1vs
@meme-ku1vs 11 күн бұрын
chumeshwari ❣
@vinitsunita
@vinitsunita Ай бұрын
Awesome explaination
@CodeMode9313
@CodeMode9313 4 ай бұрын
Thats clear explanation
@AnkitKumar-ge3jn
@AnkitKumar-ge3jn 4 ай бұрын
Nice Explanation
@prajitbanerjee8226
@prajitbanerjee8226 4 ай бұрын
beautiful implementation man
@nishantdehariya5769
@nishantdehariya5769 4 ай бұрын
Very nice 👍
@inderlalchandani
@inderlalchandani 4 ай бұрын
I am thinking about one more approach where a sort the frequency and use the formula if n
@lostcyrus8578
@lostcyrus8578 4 ай бұрын
Good explanation as always. Please upload the gfg question also maximum paths in tree
@akshaychavan5511
@akshaychavan5511 4 ай бұрын
Spent almost 5 hours on this question, and couldn't figure out solution for the edge cases. But, this solution indeed makes a lot of sense! As always, love your explaination
@paul9005
@paul9005 4 ай бұрын
5hrs for one question? ssly
@akshaychavan5511
@akshaychavan5511 4 ай бұрын
@@paul9005 Yeah! I know it sounds terrible. But, this question was a tricky one for me.
@ajinkyakhete1151
@ajinkyakhete1151 4 ай бұрын
Good Explanation bhai .................. also provide greedy 😄
@captvashu8142
@captvashu8142 4 ай бұрын
bro is something else
@mohdalizilani9896
@mohdalizilani9896 4 ай бұрын
could u upload soln of gfg potd , similar type of problem came in my OT Thoughspot but i was unable to solve. problem name is : possible paths in a tree
@gauravbanerjee2898
@gauravbanerjee2898 4 ай бұрын
Thanks a lot bhaiya ❤❤ & congrats for 50k subs 🥳🥳
@RajGupta-cu9hi
@RajGupta-cu9hi 4 ай бұрын
Can we solve it with set + hashmap ?
@user-tl4ks8vq9p
@user-tl4ks8vq9p 4 ай бұрын
hlw, can you please lemme know when there is no element to store we store an idle time in the vector/array list, where is exactly the logic used in the code to store that? I cant get that thing
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
Everytime you want to pop at max n+1 tasks so that we get cooling period of n for tasks. temp = {f1, f2, f3….} Now when you have popped then and reduced their frequency by 1, Two things can happen : 1) It’s still not 0 - Push it back to heap 2) It becomes 0 - Leave it After two steps above; If we still have tasks(pq is not empty) left it means these tasks will come again in future and hence they need a cool period of n and so we add n+1 in our time (this n+1 can include tasks or idle spots if we didn’t have n+1 tasks to pop)
@user-tl4ks8vq9p
@user-tl4ks8vq9p 4 ай бұрын
@@codestorywithMIK thanks a lot for the detailed explanation! 😃appreciate it!
@riderprovider44
@riderprovider44 4 ай бұрын
kaha gumne jana hai sir ji
@abhisheksinghdangi5027
@abhisheksinghdangi5027 4 ай бұрын
greeedy wala method bhi btao bhaiya , wo bhi seekhna h
@noone1000_xyz
@noone1000_xyz 4 ай бұрын
how many leetcoins do you have?
@magicbullet281
@magicbullet281 4 ай бұрын
bro where is java code pls provide it Faster
@amankumar-ig2fe
@amankumar-ig2fe 4 ай бұрын
bhaiya java me code banaya karo video me
@rvpool9830
@rvpool9830 4 ай бұрын
Github link not working, please fix it
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
Fixed ❤️🙏
@user-nd6no9gs6h
@user-nd6no9gs6h 4 ай бұрын
​@@codestorywithMIKWhere can I find these visually appealing and understandable notes
@sayantanbandyopadhyay7803
@sayantanbandyopadhyay7803 4 ай бұрын
Ghumne kaha ja rahe ho bhaiyaa 😂 ??
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
Will update soon 😁
@aizad786iqbal
@aizad786iqbal 4 ай бұрын
java code...?
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
Added in the Github Link in the description also. class Solution { public int leastInterval(char[] tasks, int p) { // Build frequency map int[] freq = new int[26]; for (char ch : tasks) { freq[ch - 'A']++; } // Max heap to store frequencies PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); for (int i = 0; i < 26; i++) { if (freq[i] > 0) { pq.offer(freq[i]); } } int time = 0; // Process tasks until the heap is empty while (!pq.isEmpty()) { int cycle = p + 1; List store = new ArrayList(); int taskCount = 0; // Execute tasks in each cycle while (cycle-- > 0 && !pq.isEmpty()) { int currentFreq = pq.poll(); if (currentFreq > 1) { store.add(currentFreq - 1); } taskCount++; } // Restore updated frequencies to the heap store.forEach(pq::offer); // Add time for the completed cycle time += (pq.isEmpty() ? taskCount : p + 1); } return time; } }
@izwsm
@izwsm 4 ай бұрын
leetcode -134 😬
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
✌️😁 Gas Station (Google, Amazon, Uber, Microsoft, FactSet, Zoho, Flipkart) : Explanation ➕ Live Coding kzbin.info/www/bejne/qpSylKCEfauHiq8
@izwsm
@izwsm 4 ай бұрын
sorry!! mene search kiya tha but nhi mila ... Thanks for replying .. HAPPY RAMADAN ♥♥♥♥@@codestorywithMIK
@eswarsai5163
@eswarsai5163 4 ай бұрын
bro which software or device you are using to write , it is so smooth please share @codestorywithMIK
@anshumaan1024
@anshumaan1024 4 ай бұрын
ipad
@codestorywithMIK
@codestorywithMIK 4 ай бұрын
I use ipad and default notes app
Leetcode Weekly Contest 404 | Video Solutions - A to D | by Abhinav Kumar | TLE Eliminators
1:49:57
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 70 МЛН
Who has won ?? 😀 #shortvideo #lizzyisaeva
00:24
Lizzy Isaeva
Рет қаралды 64 МЛН
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 31 МЛН
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 77 МЛН
UNSEEN FOOTAGE - BILL GATES MEET DOLLY PARODY
5:42
CarryMinati Productions Official
Рет қаралды 1,4 МЛН
Task Scheduler - Leetcode 621 - Python
17:07
NeetCode
Рет қаралды 152 М.
Remove K Digits | Intuition | Dry Run | Leetcode 402 | codestorywithMIK
35:07
Crowdstruck (Windows Outage) - Computerphile
14:42
Computerphile
Рет қаралды 117 М.
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 70 МЛН