Merge Sorted Array | LeetCode 88 | Coding Interview Tutorial

Sdílet
Vložit
  • čas přidán 26. 07. 2024
  • Merge Sorted Array solution: LeetCode 88
    Code and written explanation: terriblewhiteboard.com/merge-...
    Link to problem on LeetCode: leetcode.com/problems/merge-s...
    Buy Me a Coffee: www.buymeacoffee.com/terrible...
    AFFILIATE LINKS
    If you're interested in learning algorithms, these are great resources.
    💻 40% off Tech Interview Pro: techinterviewpro.com/terriblew...
    ⌨️ 20% off CoderPro: coderpro.com/terriblewhiteboard
    💲 All coupons and discounts 💲
    terriblewhiteboard.com/coupon...
    Merge Sorted Array | LeetCode 88 | Coding Interview
    #mergesortedarray #leetcode #algorithms #terriblewhiteboard #codinginterview
    Click the time stamp to jump to different parts of the video.
    00:00 Title
    00:06 Problem readout
    00:56 Whiteboard solution
    03:40 Coding solution
    10:40 Result and outro

Komentáře • 45

  • @TerribleWhiteboard
    @TerribleWhiteboard  Před 4 lety +17

    This solution is valid for JavaScript. You may have to modify it to fit your particular language.
    Java solution:
    class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
    int first = m - 1;
    int second = n - 1;
    for (int i = nums1.length - 1; i >= 0; i--) {
    if (second < 0) {
    return;
    }
    if (first >= 0 && nums1[first] > nums2[second]) {
    nums1[i] = nums1[first];
    first--;
    } else {
    nums1[i] = nums2[second];
    second--;
    }
    }
    }
    }

    • @epk1001
      @epk1001 Před 3 lety

      I got stuck at the test case [0], 0, [1],1, I missed the first >= 0 condition. Thanks for your java solution. - :)

    • @quangngo7264
      @quangngo7264 Před 2 lety

      thanks for your solution

  • @YKLUO
    @YKLUO Před 4 lety +17

    such a beautiful solution

  • @rahulsbhatt
    @rahulsbhatt Před 4 lety +9

    It's a very good solution, thank you so much!

  • @15kfelix
    @15kfelix Před 4 lety +8

    need to add:
    if(first < 0){
    nums1[i] = nums2[second];
    second--;
    }
    to account for new test cases

    • @ibrahimalkuwaifi6087
      @ibrahimalkuwaifi6087 Před 3 lety

      Actually you need to add this
      if first < 0:
      while second >= 0:
      nums1[replaceIndex] = nums2[second]
      second-=1
      replaceIndex-=1

  • @cocoarecords
    @cocoarecords Před 4 lety +13

    wow thanks

  • @AbhishekKumar-ff9vg
    @AbhishekKumar-ff9vg Před 3 lety

    good work

  • @adarshmv261
    @adarshmv261 Před 4 lety +15

    can you put the python version of this pls?

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +15

      I would, but I don't have time to make videos in more than one language. I'm hoping the whiteboarding can be translated to multiple languages, though.

    • @louistannudin2486
      @louistannudin2486 Před 4 lety +1

      leetcode.com/problems/merge-sorted-array/discuss/29522/This-is-my-AC-code-may-help-you

  • @nhingo6067
    @nhingo6067 Před 4 lety

    Any idea why the solution fails if I switch the order of if conditions as: if (nums1[first]

  • @leecharlie2513
    @leecharlie2513 Před 3 lety +1

    Why start from the end? And what is the difference if starting from front?

    • @derekmurray5974
      @derekmurray5974 Před 3 lety

      Hello, I am wondering this as well. Please advise. Thanks a lot!

    • @derekmurray5974
      @derekmurray5974 Před 2 lety

      Hi, I'm revisiting this to see why start from the end and whats the difference....Thanks a lot for your contribution through creating this page

  • @SuhasSrivats
    @SuhasSrivats Před 4 lety +3

    It fails for this test-case:
    Input:
    [2,0]
    1
    [1]
    1
    Output:
    [2,2]
    Expected:
    [1,2]

    • @louistannudin2486
      @louistannudin2486 Před 4 lety +1

      leetcode.com/problems/merge-sorted-array/discuss/29522/This-is-my-AC-code-may-help-you

    • @15kfelix
      @15kfelix Před 4 lety

      add if(first < 0){
      nums1[i] = nums2[second];
      second--;
      }
      to top of if/else chain

    • @singforhappiness1
      @singforhappiness1 Před 3 lety

      first = m - 1
      second = n - 1
      for i in range (m+n-1,-1,-1):
      if second < 0 :
      break;
      elif first < 0:
      nums1[i] = nums2[second]
      second -= 1
      elif nums1[first] > nums2[second]:
      nums1[i] = nums1[first]
      first -= 1
      else:
      nums1[i] = nums2[second]
      second -= 1

  • @vaishalirockzz
    @vaishalirockzz Před 4 lety +4

    This solution is only valid if m>n.
    it will not work if m

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +10

      It works for javascript. You might have to make minor adjustments if you're using another language.

    • @KaranKumar-xt2mh
      @KaranKumar-xt2mh Před 4 lety +4

      It's literally given in the question to merge nums2 into nums1

    • @mandeepashiya2229
      @mandeepashiya2229 Před 3 lety

      A note from the question - You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.

  • @tapanpanchal757
    @tapanpanchal757 Před 3 lety +1

    I can't pass one test case i.e:
    [0]
    0
    [1]
    1
    since, first = m-1, and m is 0 in this case, so first becomes negative and it gives a runtime error
    hope you see this

  • @jhessikanda
    @jhessikanda Před 3 lety

    Solution covering edge cases mentioned here:
    var merge = function(nums1, m, nums2, n) {
    let firstLastElementIndex = m -1;
    let secondLastElementIndex = n -1;
    for (let x=(m + n -1); x >= 0; x--) {
    if (secondLastElementIndex < 0) {
    break;
    }
    if (nums1[firstLastElementIndex] < nums2[secondLastElementIndex] ||
    firstLastElementIndex < 0 ||
    m === 0) {
    nums1[x] = nums2[secondLastElementIndex];
    secondLastElementIndex--;
    } else {
    nums1[x] = nums1[firstLastElementIndex];
    firstLastElementIndex--;
    }
    }
    };

  • @ashutoshmaheshwari
    @ashutoshmaheshwari Před 3 lety

    you missed a condition..what if first

  • @hideinbush0
    @hideinbush0 Před 4 lety

    Why is there no return statement to show the output?

    • @real_chimps9570
      @real_chimps9570 Před 4 lety +3

      Because you're supposed to modify nums1, you don't need to return anything to do that

  • @AhmedHemaz
    @AhmedHemaz Před 3 lety +1

    I am dump that's it