Тёмный

Find the k'th Largest or Smallest Element of an Array: From Sorting To Heaps To Partitioning 

Back To Back SWE
Подписаться 240 тыс.
Просмотров 267 тыс.
50% 1

Code & Problem Statement @ backtobackswe.com/platform/co...
Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given an array and k, find the k'th largest element as efficiently as possible. Return the actual value of the k'th largest item.
I got this question in my final round of Facebook internship interviews...this is before I knew how to solve recurrences and analyze algorithms, etc...I fucked it up but who cares...I talked for so long and thought the optimal solution was log(n)...but I was really wrong.
Approaches
Sort: O(n*log(n))
Use A Min-Heap: O(n*log(k))
Remember QuickSort...how does it work...what does it fundamentally do...partition
The kth largest element will be at index n - k
Ex:
arr = [ 3, 2, 1, 5, 6, 4 ]
k = 2
The first largest element should be at the last index ... index 5 ... (6) - (1) = 5
The 2nd largest element should be at index (6) - (2) = 4
The n'th largest element should be at the first index ... index 0 ... (6) - (6) = 0
So...
finalPivotPosition == n - k ... The pivot is the k'th largest element
finalPivotPosition greater than n - k ... k'th largest must be in the left partition
finalPivotPosition less than n - k ... k'th largest must be in the right partition
We an pick a pivot however we want but random is best since we have a equal likelihood to pick any element.
The worst case is O(n²) but the likelihood this can happen goes down exponentially because it can only happen if the greatest or least item is chosen as a pivot repeatedly.
Complexities
Time: O(n)
On average we will be splitting the input in half leading to a recurrence of T(n) = T(n/2) + (n - 1)
If we solve for these exact amount of comparisons we see that we stay to the order of linear time.
Space: O(1)
Everything is done in-place.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is question 12.8 in the fantastic book "Elements of Programming Interviews".

Наука

Опубликовано:

 

10 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 417   
@anthonyesquire9830
@anthonyesquire9830 5 лет назад
Good day. I don't ever comment. However, I just wanted to say that all your videos are really helpful to both me and many others. I respect the level of time and effort you put into these video and how many people you are helping. What most tutors/lecturers don't understand is that even you help that ONE child at the back of the class, they might just end up being able to build the next big startup. This being just because of the extra effort people like you put. Of course the material is NOT easy. However, I respect and thank you for both me and many others who are really struggling or just want enrichment. So hopefully you can keep it up and just know that there is at least someone who benefits from your effort (probably many though). Channels like these are hard to come by. So please keep up the good work. You will never know who it may help!
@BackToBackSWE
@BackToBackSWE 5 лет назад
"even you help that ONE child at the back of the class" I resonate with that the most. The problem with lectures is that it is always tens of people anywhere in the classroom that are confused but don't speak up because a lecture is a hard environment to be a real, inquisitive, student in. (this is why whenever I get a youtube comment I try to say..."keep asking questions" because if a student is afraid to ask...they lose the very curiosity that makes a great lesson...you know that quote....'the teacher and the student make the teaching' or something like that) Thank you for your comment. It is a lot of work and every week I have to tell myself to keep doing this (but I mean...I want to make it sustainable eventually with products I'm building right now, etc...at that point I won't need motivation because this will hopefully be a sustaining business) This comment served as my weekly energy renewal :)
@damodarmahawar9748
@damodarmahawar9748 2 года назад
I6
@Sarah-re7cg
@Sarah-re7cg 4 месяца назад
As the student in the back, thank you. I think it’s videos like these that really make me want to also help students out since we’ve all been there. I feel a great level of gratitude
@addiegupta
@addiegupta 5 лет назад
"We're doing more work than we need to " that is a very clever way to think about a solution's quality while solving a problem.
@BackToBackSWE
@BackToBackSWE 5 лет назад
yeah
@sourabhk2373
@sourabhk2373 4 года назад
Huge respect to this guy. The way he phrased his sentences, like " We are doing more work than we need to" , these will help you and move you in the direction of optimizing your solution. Thanks for the videos my dude.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks - this guy
@CodeSuccessChronicle
@CodeSuccessChronicle 3 года назад
@@BackToBackSWE haha
@sarojinigarapati95
@sarojinigarapati95 5 лет назад
I don't usually comment but this is the best explanation for quick select algorithm ever ! All of your videos are amazing and easier to understand than many other resources online. Thank you so much !!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@vedantiyangar151
@vedantiyangar151 4 года назад
Man, you're like a brother to me. I've learnt so much from your simple videos that I couldn't from my college years. Thanks a lot. I am considering binge watching your whole channel.
@BackToBackSWE
@BackToBackSWE 4 года назад
Haha, nice to hear, I mean, as humans we are all siblings. Anyway, glad they are helping you. - Ben
@abhishek-n-chaudhary
@abhishek-n-chaudhary 5 лет назад
One of your best videos when it comes to an explanation. It's clearly visible that you have put your heart and soul while explaining deeper logic. What an honest effort, may you get all the success you wish for!
@BackToBackSWE
@BackToBackSWE 5 лет назад
Yeah haha, this is fire. Thanks haha
@gyrogojo
@gyrogojo 5 лет назад
Great Video. The way you walked us through the problem and ended up at the average linear time solution was brilliant. Appreciate you taking time out to make such videos.
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@kumarchandan9685
@kumarchandan9685 3 года назад
Among other RU-vidrs, your way of explaining the thought process besides logic (which other coding channels usually lack) is right at the top. Awesome content. Thank you good sir!
@chuckchen2851
@chuckchen2851 3 года назад
It's awesome that you showed the entire thought process, including the well-worth detour of heap solution. Also the time analysis part is eye-opening. Unlike the merge sort, we avoided the sorting in partitions, leaving only the n/2^i as the leading term in big O, which adds up to O(n). Thanks for going into all the details, it really pays off as a better learning outcome!
@helloiam2mas
@helloiam2mas 4 года назад
Hey man, you have the best algo + ds explanations and walkthroughs on the entire internet. Bar none. Was a wahoo but go terps lol.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks! yeah uva is perty
@navyakalra6774
@navyakalra6774 4 года назад
You're awesome!! Appreciate all your efforts that you put in to explain a problem, the thought process, solution, time complexity analysis. Huge respect for you!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks and sure and thx
@amishagupta990
@amishagupta990 4 года назад
Your understanding of the fundamental concepts is phenomenal and rare to find.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@roman_mf
@roman_mf Год назад
I really like how in-depth your videos are. Comparing to many other channels where people either go lightning fast or just gloss over the details, your step by step handholding and the big O analysis rock! I know what is next on my bingewatch list. ;-)
@jiazhengguo5493
@jiazhengguo5493 5 лет назад
the best channel for preparing coding interview
@BackToBackSWE
@BackToBackSWE 5 лет назад
I know. Give me 2 more years. RU-vid is 😴😴😴 on me.
@colorfulcodes
@colorfulcodes 5 лет назад
Facts.
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@colorfulcodes hahaha
@ricardoorellana1168
@ricardoorellana1168 5 лет назад
Ben, it's amazing the amount of knowledge you have on CS fundamentals, I am learning a lot, thank you!
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha, thanks
@kaustubhtrivedi5403
@kaustubhtrivedi5403 5 лет назад
Keep it up man, I really think this channel will be well known very soon.
@BackToBackSWE
@BackToBackSWE 5 лет назад
working on it
@nirajchowdhary7372
@nirajchowdhary7372 5 лет назад
Ben you are a genius. Thank you so much you made this problem so easy!!!. Appreciate you for your efforts and time :D Love your channel!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
I seem smart but I'm not. And nice - sure - and thanks!
@user-yn7dx7ok5u
@user-yn7dx7ok5u 3 года назад
Thanks from China.I liked several videos of you since I met your channel yesterday. Really appreciate the details and the way you present.
@TheSridharraj
@TheSridharraj 3 года назад
I just went through just once. Its been looong that i dont even remember what is quick sort. but with this video i just understood quick sort as well as how and where to apply quick sort. Good work buddy.
@brainstorming1369
@brainstorming1369 2 года назад
You know that feeling when it just clicks. You and NeetCode always get me there. Thanks for all your hard work
@jameshuddle4712
@jameshuddle4712 3 года назад
Excellent vids. Good sense of humor. Clear explanations. Two things: I have seen the partitioning solution *after* the date you posted this, but have never come across it before -- or seen references in videos prior. Is this your solution? Incredible! The other thing: when referring to a "good" pivot, you are of course thinking quicksort. This is different. If I have a million item array and I want the 20th largest, the best first pivot should be just smaller than the 20th largest. You *read* a million elements, and if they're larger, you *swap* them. So if your pivot is (for instance) 999900, you only do the swap like 100 times. This is good for speed. And then your next search is finding your k-spot in a list of 100 items. See how much faster that goes? Instead of n + n/2 + n/4 ... it's more like n+200. YMMV Hope you like this twist HALF as much as I enjoyed discovering "your" partitioning solution. It was a delight! (there *is* a general case and there *is* a quick pivot-finding algo)
@SocajowaRS
@SocajowaRS 4 года назад
11:50 is literal gold, I never thought of representing K and N in that way visually. It makes accessing K and thinking about how to index into it manageable. Freaking awesome.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks haha, I don't remember what I say in like all my videos
@SocajowaRS
@SocajowaRS 4 года назад
@@BackToBackSWE Again appreciate this video, I swear I was struggling all day to try to understand this algorithm, and I watched your video, then your quicksort video, and then whiteboarded some examples on paper and finally got it. Thanks a ton.
@BackToBackSWE
@BackToBackSWE 4 года назад
@@SocajowaRS nice
@PoulJulle-wb9iu
@PoulJulle-wb9iu 4 года назад
cuz u noob
@shiveshbharti5442
@shiveshbharti5442 2 года назад
I have watched so many videos till now and still you are the best. Man you make these concepts feel so easy and your videos are damn amazing and very easy to understand. Thank you so much and also its available for free loved them man
@BullishBuddy
@BullishBuddy 5 лет назад
This is very good, man! Very good!! Thanks for teaching!!!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha thanks
@stk1526
@stk1526 3 года назад
Amazing explanation!This has really helped me understand how partitioning works , and the details that i have missed .
@BackToBackSWE
@BackToBackSWE 3 года назад
great to hear
@Pooja-xu4lp
@Pooja-xu4lp 3 года назад
Benyam, you are a gem of teacher and person. This is by far the best way to make me think. Thanking is not enough but thank you. Specially for keeping it for free, many of us could not have stumbled upon this or be able to afford this level of video. Please do keep up and more love from India. Btw, the donation option doesn't work in India yet and i'm sure your fans like me in India would like to contribute in some capacity.
@adityasoni1207
@adityasoni1207 2 года назад
Huge respect to this guy! Thanks a lot, these videos are amazing!
@redherring27
@redherring27 4 года назад
Congratulations your final dialogue of happiness in everyone's life earned you a subscribe. Aight imma make my chemistry major roommate subscribe too.
@BackToBackSWE
@BackToBackSWE 4 года назад
wut
@johnvanschultz2297
@johnvanschultz2297 2 года назад
This is the best explanation of quick select I have found online. Thank you for this video.
@badal1985
@badal1985 5 лет назад
you are too good Ben! your videos are very helpful. please keep making them.
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha ok, I will, as long as I'm a live
@mubarakoyeyinka8520
@mubarakoyeyinka8520 3 года назад
Thank you for all videos ! Very understanble !
@BackToBackSWE
@BackToBackSWE 3 года назад
Glad you like them!
@SinghFlex
@SinghFlex 3 года назад
14:00 you choose first element as pivot and swap it to the last , rather than doing this directly choose the last value as pivot.
@BackToBackSWE
@BackToBackSWE 3 года назад
Any number can be the pivot? Why the last?
@SinghFlex
@SinghFlex 3 года назад
@@BackToBackSWE yes any element can be the pivot ..But since we have to make a search space from L to R , so its better to choose last element, just an opinion:)
@JoseSagrero1
@JoseSagrero1 4 года назад
Thank you for your videos! You're an awesome for making them.
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@NGBigfield
@NGBigfield 4 года назад
Great video! So much effort that you've put into it!
@BackToBackSWE
@BackToBackSWE 4 года назад
ye, change da world, my final message
@tedytedy8918
@tedytedy8918 5 лет назад
Mannn you are the kinggg❤️❤️ A day before the exam on data structures i saw this video.. N this question was on the exam 🙂🙂
@BackToBackSWE
@BackToBackSWE 5 лет назад
hahaha nice
@ashutoshtiwari4398
@ashutoshtiwari4398 3 года назад
I like the way you walked through your thought process.
@mr.mystiks9968
@mr.mystiks9968 4 года назад
Reading up on quickselect thru leetcode solutions wasn’t effective at all for me but this video is simply brilliant. Realizing that we don’t have to perfectly sort EVERY integer is the first thought that tells us a heap is overkill, then using a pivot from quicksort to partially sort ONLY the half of the array we care about (and split the array further with each pivot) is the second thought that makes sorting FASTER possible. Really going into depth on how we conceptualize quick Select is what’s more valuable than the code itself. Explaining it this way is sure to blow the interviewer’s mind as it shows the raw thought process being formed from simple observations.
@BackToBackSWE
@BackToBackSWE 4 года назад
yuh
@deepraj279
@deepraj279 4 года назад
I am never going to forget quicksort because you taught me how to actually use it. Beautiful explanation
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@francocamborda5076
@francocamborda5076 3 года назад
I also never ever comment videos, and I never ever subscribe, specially when somebody asks me to subscribe. Nevertheless, your videos show a couple things: 1) A great amount of effort to condense the information to what's truly needed 2) An intuitive explanation 3) Barely any overhead in the video itself 4) Charisma when teaching 5) The importance of sharing knowledge For this I am very thankful, subscribed and if you open a Patreon or alike, willing to contribute to your cause. Kudos.
@BackToBackSWE
@BackToBackSWE 3 года назад
thanks for the detailed analysis haha
@qwarlockz8017
@qwarlockz8017 2 года назад
Makes such great sense... you do amazing work. Thanks... Finding the big of the work.. I still totally suck at that .... thanks for doing great work for us all
@Mrwiseguy101690
@Mrwiseguy101690 4 года назад
You have the best explanations I have ever seen. Definitely earned a sub.
@BackToBackSWE
@BackToBackSWE 4 года назад
welcome, you are loved
@ankuragarwal4014
@ankuragarwal4014 5 лет назад
Keep Making Videos please....a humble request from your regular student..you are truly a great teacher
@BackToBackSWE
@BackToBackSWE 5 лет назад
ok, say no more, I got u
@DonTaldo
@DonTaldo 3 года назад
love your content man, kudos to you
@BackToBackSWE
@BackToBackSWE 3 года назад
I appreciate that!
@tapasu7514
@tapasu7514 5 лет назад
No one can ever match your style of explanation. Super !
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@adityasaxena3903
@adityasaxena3903 3 года назад
Dude, your explanation! Absolute magic. Better than the paid courses!
@BackToBackSWE
@BackToBackSWE 3 года назад
thanks
@OP-yw3ws
@OP-yw3ws 10 месяцев назад
Its amazing how easy your explanations are
@tumul1474
@tumul1474 4 года назад
this was an awesome tutorial man ! thanks !
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks and sure
@ojuskhanolkar7146
@ojuskhanolkar7146 3 года назад
I officially love you and I love this channel. You save me!!!!
@BackToBackSWE
@BackToBackSWE 3 года назад
great
@ivanleon6164
@ivanleon6164 3 года назад
you are great! keep the good work. Greetings from Mexico.
@akankshasharma148
@akankshasharma148 3 года назад
Your explanations are so easy to understand.Thanks a lot🙌🙌
@netopedia8733
@netopedia8733 4 года назад
Awesome Explanation 🙏🏻 Thanks a lot
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@bharathik6479
@bharathik6479 4 года назад
Clear Explanation... Thanks a lot for taking the time and effort to make these videos. Good Vibes and Blessings from CS Students.
@BackToBackSWE
@BackToBackSWE 4 года назад
thansk and ye
@Ambs_2024
@Ambs_2024 3 года назад
Very good teaching style. Thanks for the tutorial!
@BackToBackSWE
@BackToBackSWE 3 года назад
sure
@ayushaggarwal7690
@ayushaggarwal7690 3 года назад
That was beautiful, thanks!!!
@BackToBackSWE
@BackToBackSWE 3 года назад
sure!
@AJ3000_
@AJ3000_ Год назад
This man is better than 90% of my cs department at explaining these concepts
@sddhrtha
@sddhrtha 5 лет назад
Love your channel and effort.
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@neetisharma3768
@neetisharma3768 4 года назад
Man!!! you are amazing :) thank you so much
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@Mardil
@Mardil 4 года назад
Your videos are awesome dude, keep it up.
@BackToBackSWE
@BackToBackSWE 4 года назад
thx and ok
@shubhangigupta2211
@shubhangigupta2211 5 лет назад
Great Explanation, thanks
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@sinaanyounus5449
@sinaanyounus5449 3 года назад
studying for my algos final rn thank u so much
@uditswaroopa5809
@uditswaroopa5809 4 года назад
After watching so many videos this video helped thanks a lot brother
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@prateekgvr
@prateekgvr 3 года назад
Great job man.. 🔥
@wanderlustsiddhi
@wanderlustsiddhi 2 года назад
Thank you so much for such in depth explaination! The video content is gold! Love and support from India!😀
@shubhampareek2378
@shubhampareek2378 5 лет назад
This is my 2nd or 3rd comment ever on RU-vid. But all I wanna say is: "I wish there was a provision of giving more than one like". Thanks a lot.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Hahahahhahaha, this vid was fire, not sure if I can drop more like it. Let's see haha
@anubhavsinghal9218
@anubhavsinghal9218 4 года назад
Thanks bro, That was really helpful.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@javiermendoza5173
@javiermendoza5173 4 года назад
this is a gem, thanks for share it
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@pushpendrasingh1819
@pushpendrasingh1819 5 лет назад
BRO... you also need to share your upper push body workout. You are in great shape
@BackToBackSWE
@BackToBackSWE 4 года назад
hahahahaha
@lokeshs9449
@lokeshs9449 3 года назад
Keep it up!! I read all your reply to the public comments that are brilliant& interesting. And I wait for your reply...!!
@BackToBackSWE
@BackToBackSWE 3 года назад
lol most are basic
@minjingyang7383
@minjingyang7383 5 лет назад
Could you provide the order that we should follow to watch those videos? Because the beginning of the video shows that you've already covered the sorting methods, but it is the 2nd video in this "Sorting, Searching, and Heaps" playlist, so I got a bit confused by which one to start. Thanks for teaching, and your videos are really easy to understand and get to the point!
@BackToBackSWE
@BackToBackSWE 5 лет назад
Hey, I'm rapid fire responding to comments (I got behind 250 after 2 weeks) I'd answer this but in a hurry, so sorry I recommend no specific order, just work on what you are weak on ...at one point in time I could answer everything ugh - ben
@trevor8704
@trevor8704 4 года назад
Great, thank you very much
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@aviralgupta9364
@aviralgupta9364 4 года назад
Fantastic explanation !!
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@user-nd2pv6ip5l
@user-nd2pv6ip5l 3 года назад
thanks for your video
@ihmpall
@ihmpall 5 лет назад
Thanks !! Can you do topological sort (leetcode course schedule problem)
@BackToBackSWE
@BackToBackSWE 4 года назад
sure and probably not
@longuyen10119
@longuyen10119 4 года назад
Awesome content as always! Go Terps!
@BackToBackSWE
@BackToBackSWE 4 года назад
hey.
@thanga2317
@thanga2317 5 лет назад
Great video and any plan for box stacking DP ?
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks and no
@chatGPT7
@chatGPT7 4 года назад
Lovely, many thanks.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@mystikyogi
@mystikyogi 4 года назад
Clear explanation, keep it up :)
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@cauaveiga
@cauaveiga 4 года назад
Excellent content!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@TCErnesto
@TCErnesto 2 года назад
as a math lover I really enjoy your explanations, thanks man
@user-gz1ud2qp2s
@user-gz1ud2qp2s 5 лет назад
love your videos styles
@BackToBackSWE
@BackToBackSWE 5 лет назад
thx
@vanducnguyen346
@vanducnguyen346 4 года назад
Always love your enthusiastic when explaining, down to the smallest detail. Keep up the good work. I wish i've chosen CS major
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks & u can always do whatever. life is long in years.
@advertronicssystems8377
@advertronicssystems8377 2 года назад
I am a programming newbie and I am learning a lot from your channels. Just one observation, from your python code, where there is a while loop (while left
@BackToBackSWE
@BackToBackSWE 5 лет назад
Check out the free DSA Mini-Course 👉backtobackswe.com/five-day Table of Contents: Shameless Self Promotion & Useless Talking 0:00 - 0:45 The Problem Introduction 0:45 - 2:20 Approach #1: Just Sort The Array & Count K Back 2:20 - 3:50 Approach #2: Heap Based Approach 3:50 - 5:02 Min Heap Approach Walkthrough 5:02 - 7:41 Seeing How We Can Improve Further 7:41 - 9:51 We Realize What We Need To Do 9:51 - 10:43 Where Will The k'th Largest Element End Up? 10:43 - 13:19 Approach #3: Walking Through A Partition Step 13:19 - 16:58 Approach #3: The Deep Deep Deep Understanding 16:58 - 20:43 Analysis: Looking At The Recurrence 20:43 - 24:53 Analysis: Solving The Recurrence 24:53 - 27:14 Analysis: Our Final Result 27:14 - 28:11 Wrap Up (and space complexity) 28:11 - 29:54 Comments: 27:35 -> What we just solved is the recurrence for the Best Case where we choose a pivot that is the median in the partitioning space and the resulting input gets split perfectly in half. This is not a rigorous Average Case analysis but it approximates what will generally happen very well so that we can see why the asymptotic complexity will be O(n). (and it is also Ω(n)...so therefore the runtime is Ө(n)). The code for this problem is in the description. Fully commented for teaching purposes.
@utsavprabhakar2205
@utsavprabhakar2205 5 лет назад
A big thank you for this video. Also, could you please tell me good resources from where I could practice time complexities of recurrance relations
@BackToBackSWE
@BackToBackSWE 4 года назад
sure and you can just google stuff, no specific resource
@mriKsuN
@mriKsuN 4 года назад
@@BackToBackSWE I don't see the code in the description, I've tried to make this algo in code myself but it's not working out.
@shyammuppidi2092
@shyammuppidi2092 4 года назад
@@mriKsuN i just saw some line explaining the code not the actual code.
@uman9235
@uman9235 4 года назад
@@shyammuppidi2092 github.com/bephrem1/backtobackswe/blob/master/Sorting%2C%20Searching%2C%20%26%20Heaps/KthLargestElement/KthLargestElement.java
@MhZnSO4
@MhZnSO4 3 года назад
Man! You the man!
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@chanish163
@chanish163 4 года назад
U are awesome 👌 ...u are helping many students 🤜🤛
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@colorfulcodes
@colorfulcodes 5 лет назад
I see your comments all over leetcode lol, great vid.
@BackToBackSWE
@BackToBackSWE 5 лет назад
hahaha yeah...those were the days..
@biboswanroy6699
@biboswanroy6699 4 года назад
amazing explanation!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@ANNIE-cr7tk
@ANNIE-cr7tk 4 года назад
excellent explanation!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@ryoyamamoto6488
@ryoyamamoto6488 4 года назад
This channel is fuuuuuckin amazing man. Sorry for cursing but I had to.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks and ur good
@ankitgoyal8556
@ankitgoyal8556 4 года назад
This was the first question interviewer asked me 🥳
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@factsheet4930
@factsheet4930 3 года назад
I think you could have just said, in the complexity analysis part, that the sum from i = 0 to something of (1/2)^i, is strictly less than the sum from i=0 to infinity of (1/2)^i = 2, and so you end up with the answer being less than 2n - log(n), therefore it is linear complexity.
@vikrant_462
@vikrant_462 3 года назад
you are suberb, I always recommend your video to those who ask for help...
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@growingwithtech
@growingwithtech 4 года назад
Buddy, I think you have calculated the average case time complexity. What happens if pivot doesn't manages to split the array in halves, and the pivot happens to be one of the extremes. In that case, the worst case time complexity would be T(n)=T(n-1)+(n-1), which evaluates to O(n^2), which is even worse than heap-based approach. But anyways, I liked the way you think.
@BackToBackSWE
@BackToBackSWE 4 года назад
What was the recurrence I solved?
@growingwithtech
@growingwithtech 4 года назад
You solved for the best case i.e., T(n)=T(n/2)+(n-1)
@BackToBackSWE
@BackToBackSWE 4 года назад
@@growingwithtech Ah, yeah, rewatched it, yes the worst case is O(n^2). I analyzed the best case (where we assume a 50/50 split), and it follows near the same reasoning as the average case (where we assume a 75/25 split) which also bounds to O(n). More on the average case: web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/09/Small09.pdf Thanks for the question
@growingwithtech
@growingwithtech 4 года назад
Exactly!! Thanks for the clarification
@narg3931
@narg3931 4 года назад
great content dude
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@DarshanSenTheComposer
@DarshanSenTheComposer 4 года назад
The distinction between: i. T(N) = T(N / 2) + O(N) ii. T(N) = *2* T(N / 2) + O(N) took me by surprise! _sigh_ stupid me I've been watching some of your lectures for a while. I've gotta say, you're a really good teacher. Keep up the good work! :)
@BackToBackSWE
@BackToBackSWE 4 года назад
Thanks. Yeah, former bounds to linear time, latter bounds to n*log(n)
@peterbarter4647
@peterbarter4647 3 года назад
Bloopers also 😂😂😂good one bro
@BackToBackSWE
@BackToBackSWE 3 года назад
ye
@KeshariPiyush24
@KeshariPiyush24 2 года назад
Can you please make another video for the same question using "median of median" approach which has linear time for worst case as well.
@andrews2945
@andrews2945 4 года назад
Your explanations and visuals are always on point, thanks again. No flame, just thought it was funny, but did you have a stroke @ 20:57 😆
@BackToBackSWE
@BackToBackSWE 4 года назад
sure, and my bad
@laksh5228
@laksh5228 3 года назад
This partitioning is linear time only when we can ensure the pivot is proper right? but when we measure an algorithm's time complexity, do we not go by the worst case? And when we choose the pivot to be the worst(largest element), we would lose the entire left partition and thus losing the actual answer too in some cases right? Please clarify if am I missing something
@adityasrivastava8790
@adityasrivastava8790 3 года назад
Overpowered John!Great work;)
@BackToBackSWE
@BackToBackSWE 3 года назад
lol thx
@adityasrivastava8790
@adityasrivastava8790 3 года назад
@@BackToBackSWE he he @johnlegend aka all of me! Gr8 work byddy
Далее
Qalpoq - O'z eriga qaynona (hajviy ko'rsatuv)
34:31
Просмотров 141 тыс.
The moment we stopped understanding AI [AlexNet]
17:38
Просмотров 879 тыс.
The Clever Way to Count Tanks - Numberphile
16:45
Просмотров 850 тыс.
Fast Inverse Square Root - A Quake III Algorithm
20:08
$25,000 vs. $25,000,000
29:58
Просмотров 2,4 млн
A.I. ‐ Humanity's Final Invention?
18:30
Просмотров 3,3 млн