Merge Overlapping Intervals | Brute, Optimal with Precise TC analysis

Sdílet
Vložit
  • čas přidán 30. 06. 2024
  • Problem Link: bit.ly/3ItlwtJ
    Notes/C++/Java/Python codes: takeuforward.org/data-structu...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course
    00:41 Problem Statement
    01:15 Overlapping example
    03:27 Brute force
    03:56 approach
    09:39 Code
    13:30 Complexity
    16:22 Optimal approach
    16:38 Intuition
    18:55 Code
    21:36 Complexity

Komentáře • 192

  • @takeUforward
    @takeUforward  Před 10 měsíci +15

    Please watch our new video on the same topic: czcams.com/video/IexN60k62jo/video.html

  • @takeUforward
    @takeUforward  Před rokem +91

    Do give us a like if you understood everything, it won't cost you much :)

    • @mohdalizilani9896
      @mohdalizilani9896 Před 10 měsíci

      Hey Bro! 🎉 Your Graph and DP series is absolutely fantastic! 👌 Your content is top-notch and stands out from the rest. I've been searching everywhere, and your quality is unmatched. 😍
      I do have a humble request - could you consider creating a compact playlist on Digit DP? Lately, I've encountered several LeetCode contests with questions related to Digit DP, and I'm struggling to crack them. I'm sure many of us are in the same boat. 💡
      If you're on board with the idea of adding videos on DIGIT DP, please show your support by giving this comment a thumbs up! 👍 Let's learn and grow together. 📚💪

  • @Akash-yr2if
    @Akash-yr2if Před 11 měsíci +60

    This is the first question where I took 1hr to understand the Brute force & 10 min to understand the Optimal logic 🙂

    • @tot7997
      @tot7997 Před 11 měsíci +7

      22 min ka to vvideo he lodu

    • @Akash-yr2if
      @Akash-yr2if Před 11 měsíci +25

      @@tot7997 Comment parke samajna ka dimak bhi chahihe chomu

    • @tot7997
      @tot7997 Před 11 měsíci +1

      @@Akash-yr2if thoda humor bhi chahiye ig

    • @user-je7tz6le4k
      @user-je7tz6le4k Před 11 měsíci +2

      coz both are the same... I mean he just decreases the complexity from O(2*n) to O(n) and for Java it didn't even do that bcoz we have to convert a list to an array at the end, So it remains O(2*n)

    • @nuclearbaba1769
      @nuclearbaba1769 Před 10 měsíci

      no problem bro we are playing with logic we don't take seriously to converting languages complexity
      @@user-je7tz6le4k

  • @darshanrokkad5216
    @darshanrokkad5216 Před 4 měsíci +22

    understanding of brute force took me more time than understanding of optimal

    • @OrderEmperor
      @OrderEmperor Před 2 měsíci +4

      To make his video in his regular pattern he intentionally adds unnecessary solutions and calls them brute force..

  • @badass_yt7962
    @badass_yt7962 Před rokem +22

    I generally do not comment even on good videos, but after watching this video, i really wanna say amazing explanation. You made a difficult problem easy. Excellent clarity of concept. Striver, you are the best💯

  • @NithinvKumar-uk2he
    @NithinvKumar-uk2he Před rokem +6

    One of the greatest free course that I ever seen in CZcams

  • @syedFAHIM-el1wr
    @syedFAHIM-el1wr Před rokem +4

    By Liking , We also tell you tube algorithm that provide us with such content instead of distracting useless videos!!!
    When i understood this, I started liking every quality content videos and my recommendation is now really good.
    Understood!
    I was able to build up the logic but could not code it by myself...
    Waiting for that magic to happen when i can code even listening to music

  • @anshikavlogs8269
    @anshikavlogs8269 Před 11 měsíci +3

    you make this question so easy to be understood by your proper explanation .🔥🔥 really thanks a lot

  • @cinime
    @cinime Před rokem +1

    Understood! Super amazing explanation as always, thank you so so much for your effort!!

  • @deathigniter2155
    @deathigniter2155 Před 29 dny +3

    i am glad i was able to think of optimal solution directly and didnt even realised it was optimal. Felt like
    Brute Force was more tougher than the optimal one. LOL

    • @sayamparejiya1510
      @sayamparejiya1510 Před 21 dnem +2

      nice bro but how? through practice ?

    • @deathigniter2155
      @deathigniter2155 Před 19 dny +4

      @@sayamparejiya1510 yes brother... i have solved like 700 problems on CF and 400 + on GFG and Codechef combined.... so it takes alot of practice. but yeah number of questions doesnt matter... Good questions matter which teach you something that you can carry to 10 next questions

    • @sayamparejiya1510
      @sayamparejiya1510 Před 19 dny +1

      @@deathigniter2155 Thank for sharing.

  • @iWontFakeIt
    @iWontFakeIt Před rokem +10

    I think my code is more intuitive (atleast to me) rather than the code you shown.(both approaches are same) (also I am not complaining, I am sharing this so that others can also share their opinion on mine). Below is my code:
    #include
    /*
    intervals[i][0] = start point of i'th interval
    intervals[i][1] = finish point of i'th interval
    */
    vector mergeIntervals(vector &intervals)
    {
    int n = intervals.size();
    sort(begin(intervals), end(intervals));
    vector ans;
    int i = 1;
    int start = intervals[0][0], end = intervals[0][1];
    while(i

    • @jayrathod7957
      @jayrathod7957 Před 6 měsíci +1

      bro........ i just write same code but little different...........
      vector ans;
      int n=arr.size();
      int lastf=-1,lasts=-1;
      for(int i=0;i=first)
      {
      lastf=min(lastf,first);
      lasts=max(lasts,second);
      }
      else{
      vector temp;
      temp.push_back(lastf);
      temp.push_back(lasts);
      ans.push_back(temp);
      lastf=first;
      lasts=second;
      }
      }
      else{
      lastf=first;
      lasts=second;
      }
      }
      vector temp;
      temp.push_back(lastf);
      temp.push_back(lasts);
      ans.push_back(temp);
      return ans;

  • @rishabh1S
    @rishabh1S Před rokem +9

    Two videos in a day ✅

  • @urvashi2410
    @urvashi2410 Před rokem +1

    It was so easy code at the end!! Thanks Striver.

  • @ritikrajsinha5895
    @ritikrajsinha5895 Před 11 měsíci

    bhaiya u re inspiration to all thrown ones like us thank u get a lot hope from u

  • @swetaacharya2969
    @swetaacharya2969 Před rokem +2

    Thank you sir for providing such great content ❤

  • @AyushKumar-ju9jj
    @AyushKumar-ju9jj Před 10 měsíci +1

    This man deserves a salute!

  • @priyankavasam8769
    @priyankavasam8769 Před 11 měsíci

    Couldn't thank you enough sir!!
    You are the savior

  • @mehulthuletiya497
    @mehulthuletiya497 Před rokem +8

    00:41 Problem Statement
    01:15 Overlapping example
    03:27 Brute force
    03:56 approach
    09:39 Code
    13:30 Complexity
    16:22 Optimal approach
    16:38 Intuition
    18:55 Code
    21:36 Complexity

  • @viditvaish7317
    @viditvaish7317 Před 9 měsíci

    amazing explanation always worth watching your videos

  • @Manishgupta200
    @Manishgupta200 Před rokem

    That's really easy to understable. Thankyou

  • @wanderer_ankur
    @wanderer_ankur Před 5 měsíci

    Man i have always been scared of arrays especially but after going through this sheet gradually my confidence is increasing. This is such a good resource for learning DSA. Thanks a lot striver bro for making this :)

  • @AshishBharti-xr5by
    @AshishBharti-xr5by Před 9 měsíci

    i just want to say ,thank you thank you very much ..❤

  • @bandlashashankreddy
    @bandlashashankreddy Před 11 měsíci

    Thank you for your wonderful explanation. Understood🙂

  • @studynewthings1727
    @studynewthings1727 Před 10 měsíci

    Thnk you, its helpful for me! to understand this concept very well.

  • @rohitchanda8461
    @rohitchanda8461 Před 5 měsíci

    So beautifully explained.

  • @selvaarumugam370
    @selvaarumugam370 Před rokem

    as usuall u jus made the problem loook easy bruh!!!!

  • @aryanthapa5540
    @aryanthapa5540 Před 5 měsíci

    was watching this tutorial on your website before after watching the video I came here to just like and comment on this video to tell how beautiful the explanation was loved it striver bhaiya❤❤

  • @venkatsaireddy1412
    @venkatsaireddy1412 Před 4 měsíci

    Understood Bro
    Thank You for Upskilling us.

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo Před 4 měsíci

    understood ,thnx for the amazing expplanation ❤❤❤❤👌👌

  • @bhargavijethva7958
    @bhargavijethva7958 Před 3 měsíci

    Very nice explanation!👏👏

  • @AmanPandey-bd1sj
    @AmanPandey-bd1sj Před 8 měsíci

    Best Explanation bhaiya thanks a lot 🙏

  • @rahuljain224
    @rahuljain224 Před 3 měsíci

    Excellent explanation
    Thank you so much 🎉 sir

  • @matbarjoshi7891
    @matbarjoshi7891 Před rokem

    Inspiration 🙏🏽🙏🏽

  • @yaxlu
    @yaxlu Před rokem

    Thank you! Understood!

  • @3egang_
    @3egang_ Před rokem

    🤩🤩 Thank you Raj Bhaiya...

  • @chandansolanki2566
    @chandansolanki2566 Před měsícem

    thank you sir , very nicely explained😊

  • @aniketkashid8289
    @aniketkashid8289 Před 9 měsíci

    Great Explanation Sir

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 3 měsíci

    Understood, thank you.

  • @eklavya22k34
    @eklavya22k34 Před měsícem

    Understood, amazing coding , love it.

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 Před 3 měsíci

    Understood Sir...........Enjoyed this lec...........

  • @utsavseth6573
    @utsavseth6573 Před 11 měsíci

    Great work raj.

  • @Vardanaggarwal
    @Vardanaggarwal Před 4 měsíci

    awesome explanation bhaiya

  • @dishankbaid9809
    @dishankbaid9809 Před rokem

    Amazing!

  • @jatinukey4062
    @jatinukey4062 Před 28 dny

    The approaches you have taught in this video, 60-70% both the approaches come in my mind but i was struggling to implement it. Thank you very much bhaiya for this video! You are always a great tutor for me and everyone ❤❤

  • @pasej5
    @pasej5 Před 4 měsíci

    Thank you bro this is good teaching

  • @GhostVaibhav
    @GhostVaibhav Před rokem +1

    Understood🔥

  • @user-sp8ne7hj3n
    @user-sp8ne7hj3n Před 10 měsíci

    when I see your video i thought why we don't get this type of teacher in our engineering college .... i think 1.5 st years is enough to crack any coding interview .....💖💖💖💖💖💖❤❤❤❤❤❤❤❤

  • @divyanjalimourya1669
    @divyanjalimourya1669 Před 11 měsíci

    Nice explanation

  • @artifice_abhi
    @artifice_abhi Před 6 měsíci

    A little change in the code (although striver's code is shorter) by updating the end every time we move to the next index:
    vector overlappedInterval(vector& intervals) {
    // Code here
    sort(intervals.begin() , intervals.end());
    vector ans;
    int start = intervals[0][0];
    int end = intervals[0][1];
    for(int i = 1; i

  • @deepanshudahiya8966
    @deepanshudahiya8966 Před měsícem

    Understood and I even made the algorithm myself before watching solution but wasnt able to write the code

  • @YourCodeVerse
    @YourCodeVerse Před 8 měsíci

    Understood✅🔥🔥

  • @abhaypatel379
    @abhaypatel379 Před 19 dny

    Thankyou @TUF 😮

  • @nitinchandrasahu1774
    @nitinchandrasahu1774 Před 3 měsíci

    Thank You😄

  • @user-or5oz1pk2x
    @user-or5oz1pk2x Před 2 měsíci

    Thanks a lot Bhaiya

  • @pulkitgupta669
    @pulkitgupta669 Před 10 měsíci

    Understood ❤❤

  • @SAURABHKUMAR-yo7er
    @SAURABHKUMAR-yo7er Před 19 dny

    i was able to do the optimal solution on my own , feeling good 😌

  • @suyashrahatekar4964
    @suyashrahatekar4964 Před 10 měsíci

    I thought of the same brute force approach (with O(nlogn) time) after much thought but I didnt know u can sort 2d vector like that.

  • @her_soulmate
    @her_soulmate Před 7 měsíci

    Understood 🎉

  • @senseiAree
    @senseiAree Před 9 měsíci

    Understood❤

  • @asheshkaran5843
    @asheshkaran5843 Před rokem

    Understood!

  • @Adarsh_agrahari
    @Adarsh_agrahari Před 5 měsíci

    What a ques but apart from this what a explenation😊

  • @BG-lj6fw
    @BG-lj6fw Před rokem +1

    understood!. great as always. btw @striver the brute force code on the website is same for cpp and js (js code has been given in cpp as well ) kindly correct it.

  • @qjawed9654
    @qjawed9654 Před 2 měsíci

    I usually don't write comment but I think this time I should. Amazing explanation Sir. Thank You

  • @user-nk1mb5fy7j
    @user-nk1mb5fy7j Před rokem

    UNDERSTOOD

  • @RahulKumar-zp1ln
    @RahulKumar-zp1ln Před 10 měsíci

    Amazing

  • @alphaCont
    @alphaCont Před 6 měsíci

    we can do it O(1) also in space, by checking the next element if it is overlapped we just change the current elements [1] element with max, than we just remove the [i+1] from the array and in the end we will have just our answer

  • @culeforever5408
    @culeforever5408 Před 8 měsíci +1

    understood 🤠

  • @user-ck6kn1ty8x
    @user-ck6kn1ty8x Před 5 měsíci

    amazing

  • @codershour
    @codershour Před rokem

    I'm still hustling, but this feeling is good

  • @pankajkumargajpalla2924

    u r awsome sir:)

  • @shivamehta6537
    @shivamehta6537 Před rokem +5

    Just want to ask how to remember so many optimal approaches and what to do if a question comes in an interview which I have never seen before?

    • @takeUforward
      @takeUforward  Před rokem +16

      Comes with practice, you will get used to it.

  • @sahilkholia2355
    @sahilkholia2355 Před 11 měsíci

    true . great

  • @user-uv5or5bm2c
    @user-uv5or5bm2c Před 3 měsíci

    Understood.

  • @yamini436
    @yamini436 Před rokem

    love you striver :)

  • @user-pq9hi1wr5z
    @user-pq9hi1wr5z Před 2 měsíci

    So Instead of making a new int[][] ans. We can use the same given array to store answer. Which can potentially reduce the usage of extra space for storing answer.

  • @AbjSir
    @AbjSir Před 8 měsíci

    understood

  • @notcountdankula
    @notcountdankula Před 11 měsíci +1

    Python Code 🐍
    class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:

    ans = []
    intervals.sort()
    ans.append(intervals[0])
    for i in intervals:
    start = i[0]
    end = i[1]
    if start

  • @TheMadRunner00
    @TheMadRunner00 Před rokem

    Brute force approach would be O(N^2) like you pick every element and loop over others on the right?

  • @sp_9467
    @sp_9467 Před rokem

    awesome

  • @soumiyamuthuraj3516
    @soumiyamuthuraj3516 Před 15 dny

    Awesome

  • @Lucifer-xt7un
    @Lucifer-xt7un Před rokem

    Please maintain consistency brother

  • @dayashankarlakhotia4943

    Understood

  • @souvikbasak7720
    @souvikbasak7720 Před 3 měsíci

    There is an edge case, if ans.back()[0] > arr[I][0] you can put ans.back()[0] = min(ans.back()[0],arr[i][0]); in else part

  • @kumarankit5726
    @kumarankit5726 Před rokem

    instead of continue,if we just i++,will the tc remain same?

  • @rickk3300
    @rickk3300 Před 11 měsíci +1

    Striver ke itne saare DSA ke videos dekh liye, ab soch kar hi ajeeb lagta hai ki bhaiya kisi zamane me CP karte the....😅

  • @habeeblaimusa4466
    @habeeblaimusa4466 Před rokem +4

    Bro, is it normal to come up with optimal solution intuition without bruteforce because, sometimes whenever a question comes up the first solution that sometimes comes up is optimal. It is sometimes hard for me to come up with bruteforce

    • @takeUforward
      @takeUforward  Před rokem +7

      It is okay, but if you know solutions before hand, and you don't want to tell to the interviewer, drive the interview..

  • @kirtipalve1772
    @kirtipalve1772 Před měsícem

    Can one of the solutions be using a set? We mark all the numbers within intervals in a set ranging from smallest number to the largest number in the array. Then we iterate over the set and select the consecutive numbers as one interval?

  • @Soumyadeepd21
    @Soumyadeepd21 Před 14 dny

    brute force seems to be more complex than the optimize one :)....btw superb content

  • @itsbhardwaj1677
    @itsbhardwaj1677 Před rokem

    what will be approach if it asks for subsets not subarrays

  • @utsavjain8731
    @utsavjain8731 Před rokem

    Where can I find the java solution?

  • @riyakumari3771
    @riyakumari3771 Před 9 měsíci

    Can you make a video on word break problem

  • @crix_05
    @crix_05 Před 4 měsíci

    18:33 "I am inside you" - Striver

  • @kushagrakansal2705
    @kushagrakansal2705 Před 5 měsíci

    what is meant by ans.back()? what does it do?

  • @jyotirmaysingh4059
    @jyotirmaysingh4059 Před měsícem

    I was able to make the intutuion but was unable to convert it to code . :(

  • @bravuracoding7721
    @bravuracoding7721 Před rokem

    bhaiya , please add a feature in website where we can mark question for revisiting

  • @yourhonestbro217
    @yourhonestbro217 Před 2 měsíci

    How do you know that your solution is the most optimal,I also thought this answer but I felt sorting is not required

  • @S3aw0lf
    @S3aw0lf Před 10 měsíci

    Isn't this question somewut similar to the taps required to water a garden question from leetcode?

  • @OneBit01
    @OneBit01 Před rokem +1

  • @aayushisrivastava5586
    @aayushisrivastava5586 Před 4 měsíci

    what is the line ans.back()[1] reffer to?

  • @d3vanshmemer
    @d3vanshmemer Před 7 měsíci

    how the time complexity of brute force will be o(2*n) bcz it O(n^2)