LeetCode 350 | Intersection of Two Arrays II | Solution Explained (Java + Whiteboard)

Поділитися
Вставка
  • Опубліковано 11 жов 2024
  • Today we go over the sort method to solving this problem!
    Running Time: O(mlogm + nlogn)
    Space Complexity: O(logm + logn)
    Always be pluggin:
    Slack Channel: join.slack.com...
    Github: github.com/xav...
    Facebook: / xavier.hollingsworth.3
    Instagram: / xavierelon
    LinkedIn: / xavier-hollingsworth-5...
    Twitter: / elon_xavier

КОМЕНТАРІ • 43

  • @fsxaviator
    @fsxaviator 3 роки тому +6

    Thank you!!! Finally, this was the first explanation I was actually able to understand.

  • @JordanTaylor-y4f
    @JordanTaylor-y4f Рік тому

    Thank you. I have always wondered why there was a pre-increment and a post increment, and this is a perfect use case for post-increment. It's the small things sometimes. I've never seen that before.

  • @Priyam6F4
    @Priyam6F4 Рік тому +2

    Hey. The solution doesn't work for : int[] arr1 = {4,9,5};
    int[] arr2 = {9,4,9,8,4}; please check.

  • @ritchievales
    @ritchievales 2 роки тому

    I had it so close.... I was increasing the indexes depending on nums1 and nums2 sizes... that approach worked for a few testcases but I couldn't figure out until saw this video, thanks! For sure I was not able to solve it by myself but still learned a lot :)

  • @someshsahu4638
    @someshsahu4638 Рік тому +1

    Very nice explanation thankyou 🙏🏼🙏🏼🙏🏼

  • @athangkulkarni8245
    @athangkulkarni8245 11 місяців тому

    At max, minimum of size of both arrays can be duplicates, so instead of doing length1 * length2, you can do min(length1, length2)

  • @sasgaro
    @sasgaro 4 роки тому +3

    Great video, you definitely deserve more subs, one note: that music started waaaayyy too early and was kinda unexpected covering your voice over.

    • @xavierelon
      @xavierelon  4 роки тому +2

      Thanks man. And thanks for the critique I'll be sure to fix it. What do you think about my intro I made? I'm still working on it

    • @sasgaro
      @sasgaro 4 роки тому

      @@xavierelon I don't usually pay much attention to intros and for me, the best intro is the one I barely notice and I didn't notice yours. Don't get me wrong, I don't mean it in a bad way, on the contrary
      , I think it's visually great, not irritating like in some channels, and most importantly - short, but people usually come for the content and not for the intros.

    • @xavierelon
      @xavierelon  4 роки тому +2

      Serdar Asgarov awesome thank you for the constructive criticism. Make sure you subscribe if you haven’t already hehe

    • @CD-xd6fm
      @CD-xd6fm 3 роки тому

      @@xavierelon Hey , can you show us how you made your intro bcoz its cool or is it a purchased plugin?

  • @sayantaniguha8519
    @sayantaniguha8519 2 роки тому

    Finally an explanation using constant space!

  • @HenggaoCai
    @HenggaoCai 4 роки тому +1

    Keep up the good work! You're doing great.

  • @pubamx
    @pubamx 3 роки тому +1

    Great explanation. Just wasn't clear to me why SC is O(logm + long). Is it due to the sort of arrays generates overrides the initial value of nums array or why? Thanks in advance.

  • @lighto263
    @lighto263 3 роки тому +1

    Solution without using extra space is amazing thanks! Bit time complexity basically is O(N), isn't it?

    • @xavierelon
      @xavierelon  3 роки тому +1

      Yes should be. Did I forget to mention time complexity in this video? Sorry if so

    • @vikasraaja5760
      @vikasraaja5760 2 роки тому

      Wouldn't it be O(nlogn) because that's the time complexity of Arrays.sort()?

    • @yogeshyts
      @yogeshyts 2 роки тому +1

      @@vikasraaja5760 lgta h timeComplexity india m jada matter krti h

    • @peterpace3379
      @peterpace3379 Рік тому +1

      ​@@yogeshyts of course, har company ke coding round mein aata hai ye sab

  • @nishukumari8791
    @nishukumari8791 5 місяців тому

    waw its really simple and awesome

  • @ginasomara267
    @ginasomara267 2 роки тому +1

    nice and understandable!! TY

  • @AbhishekKumar-ff9vg
    @AbhishekKumar-ff9vg 3 роки тому +1

    how to implement the same in c++ with same space com.
    i tried but more elements are getting copied can you give code for same. thanks for video

  • @bina.soche.samjhe
    @bina.soche.samjhe Рік тому

    why did we use nums1 and not nums2 to store the common elements

  • @monikajha3500
    @monikajha3500 3 роки тому +1

    I am little confused about the third array(ans) size as length1 * length2. Can we take the size as the smallest of length1 and length2?

    • @monikajha3500
      @monikajha3500 3 роки тому

      Oops ! sorry. I commented before I finished this video...

    • @Candyroxnrulz
      @Candyroxnrulz 2 роки тому

      wait i still don't get it why

  • @adithyang6672
    @adithyang6672 Рік тому

    What about the values which are already present in nums1 how are they getting deleted

  • @qianjun1856
    @qianjun1856 2 роки тому

    Am I missing anything, I feel this algorithm is not working.
    A=[1,2,2,1,3,2]
    B=[1,2,3]
    A_sorted=[1,1,2,2,2,3]
    expected output: [1,2]
    The Algorithm will produce[1,2,3]

    • @Kreedlord
      @Kreedlord 2 роки тому

      expected output is actually 1,2,3...d problem is to find intersection, similar-distinct elements in both array

  • @sayantaniguha8519
    @sayantaniguha8519 2 роки тому

    How is the space *O(log m + log n)* ?

  • @odongoemmanuel885
    @odongoemmanuel885 2 роки тому

    where are you getting the nums in return Arrays.copyOfRange(nums, 0, k);

  • @samareshms4591
    @samareshms4591 2 роки тому

    could have optimized to O(mlogm + n)

  • @pqr.priyanshu
    @pqr.priyanshu 2 роки тому

    Which software do you use as a whiteboard??
    thanks

  • @artemvolkov5682
    @artemvolkov5682 9 місяців тому

    So smart to add background music while you're unable to articulate well the n/m complexity. I've broken my ears