Merge Intervals (LeetCode 56) | Full Solution with diagrams and visuals | Interview Essential

Поделиться
HTML-код
  • Опубликовано: 31 июл 2024
  • Actual Problem: leetcode.com/problems/merge-i...
    Chapters:
    00:00 - Intro
    00:45 - Problem Statement and Description
    03:15 - Brute Force Solution
    04:26 - Visualizing the problem
    09:54 - Dry-run of Code
    12:22 - Final Thoughts
    📚 Links to topics I talk about in the video:
    Brute Force: • Brute Force algorithms...
    What is Big O?: • Big O Notation Simplif...
    Greedy Algorithms: • Greedy Algorithms with...
    LeetCode Solutions: • Leetcode Solutions
    📘 A text based explanation is available at: studyalgorithms.com
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/36pU0JO
    Favorite book to understand algorithms: amzn.to/39w3YLS
    Favorite book for data structures: amzn.to/3oAVBTk
    Get started for interview preparation: amzn.to/39ysbkJ
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    🎥 My Recording Gear:
    Recording Light: amzn.to/3pAqh8O
    Microphone: amzn.to/2MCX7qU
    Recording Camera: amzn.to/3alg9Ky
    Tablet to sketch and draw: amzn.to/3pM6Bi4
    Surface Pen: amzn.to/3pv6tTs
    Laptop to edit videos: amzn.to/2LYpMqn
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #interview

Комментарии • 26

  • @bingochipspass08
    @bingochipspass08 5 месяцев назад +5

    Great solution! I was confused as to why we were sorting intervals based on the starting value: "We want to find values closer to the start & not farther from it since it'll give us a higher chance of finding overlap..."

  • @arjunjadhav6275
    @arjunjadhav6275 Год назад +10

    now i can see improvement in myself, I solved this problem with exactly same approach ,though it took me two hours😅 and came here to see more optimized solustion

  • @shivaniverma4266
    @shivaniverma4266 6 месяцев назад +2

    thank you nikhil you are doing great for creating such beautiful content for us , thats what make you unique fro other teachers your explaination keep doing keep posting , u inspire us

  • @shrirambalaji2915
    @shrirambalaji2915 Год назад +4

    Thank you so much brother...I just start watching your video 3 min into the video and I code the solution without completely watching the video.
    Your explanation is on point
    thank you again

  • @spacelovertelugu44spacelov46
    @spacelovertelugu44spacelov46 8 месяцев назад +2

    Topnotch best explanation for this problem

  • @nandhakumarkr3147
    @nandhakumarkr3147 2 месяца назад +1

    Your tutorial is just awesome

  • @Divya-qt1cf
    @Divya-qt1cf 10 месяцев назад +1

    Great explanation 🎉

  • @abhcode
    @abhcode 10 дней назад

    wonderful explanation!! 🌟I would love to see you sharing some java essentials for coding in leetcode I was not knowing methods like asList and use of comparator . So thats the problem I phased int this video .Hope you will come up with video for this!!
    Eagerly waiting 👀
    Lots of love❤

  • @prashanthshetty8337
    @prashanthshetty8337 4 месяца назад +1

    Thank you very much for the great explanation and the solution to the problem. I got confused with the naming of interval and newInterval variables in the code, which could be used other way.

  • @unemployedcse3514
    @unemployedcse3514 14 дней назад

    awesome ❤

  • @plutomessi21
    @plutomessi21 10 месяцев назад +1

    Thanks bhaiya

  • @subee128
    @subee128 7 месяцев назад +2

    Thanks

  • @gyandeepdigra8461
    @gyandeepdigra8461 9 месяцев назад +2

    nice yr

  • @everyontech2716
    @everyontech2716 Год назад

    using stack
    class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
    # by using sort function, we use lamba to select key, in which x is our key
    # intervals.sort(key = lambda x:x[0]) # without it fail when intervals = [[1,4],[0,4]]
    stack = []
    for i in range(0,len(intervals)):
    # if stack not empty
    if stack and stack[0][1] >= intervals [i][0]:
    # overlapping condition comes..now update end point
    stack[0][1] = max(stack[0][1], intervals[i][1])
    else:
    stack.insert(0,intervals[i])
    # print(i)
    return stack

  • @kalyanamvenumadhav2245
    @kalyanamvenumadhav2245 Год назад +1

    Please Explain 4Sum Problem so that it can be helpful to all of us.

    • @nikoo28
      @nikoo28  Год назад

      Yep..will make a video on it too.

  • @onkardheemate1066
    @onkardheemate1066 Месяц назад

    Salute you sir🫡🫡🫡

  • @manavgora
    @manavgora 24 дня назад

    I am putting you over striver

  • @YasarNaser-mr3if
    @YasarNaser-mr3if Месяц назад

    Which platform you use to explain the question in Problem Statements and Description?

  • @sachinsoma5757
    @sachinsoma5757 10 месяцев назад +2

    public int[][] merge(int[][] intervals) {
    int n = intervals.length;
    int[][] ans = new int[n][2];
    Arrays.sort(intervals, new Comparator() {
    public int compare(int[] a, int[] b) {
    return a[0] - b[0];
    }
    });
    int ansIx = 0;
    ans[ansIx][0] = intervals[0][0];
    ans[ansIx][1] = intervals[0][1];
    for(int i = 1;i

  • @SuriyaT3001
    @SuriyaT3001 Год назад

    i think in if(condition) you used newinterval[1] instead of result[1] ... Becoz i see here you dont update the value in result list .. in else codition you updated new interval value ...am i right?..

    • @nikoo28
      @nikoo28  Год назад

      check it out...we do update the result list in the else block :)

    • @daffapradana8557
      @daffapradana8557 Год назад +2

      ​@@nikoo28 can you explain how do you update the result list in the else block? because i've only seen `result.add(newInterval)` and to my knowledge add is like pushing an element to a list. so am i missing something? or is there something that is wrong to my knowledge?
      because at the first you've already pushed the [1,3] into the result,
      and when did the result become [1,6] ? because i don't see any .set() function being called.

    • @apratimkundu2308
      @apratimkundu2308 Год назад +1

      @@daffapradana8557 it's getting updated in the if block where the newInterval[1] gets updated to the max.
      else block is for the disjoint array