Wiggle Sort - Leetcode 280 - Python

Поділитися
Вставка
  • Опубліковано 4 лют 2025

КОМЕНТАРІ • 62

  • @unmeshkadam4876
    @unmeshkadam4876 3 роки тому +34

    Neetcode is at another level, hats off

  • @devonfulcher
    @devonfulcher 3 роки тому +17

    Hey @Neetcode, I really enjoy your content. I watch it everyday to prepare for my interviews. I have a suggestion. Could you post content of you going into a question blind? I would really like to see your thought process when you don't know the answer. Thanks! Keep up the excellent content!

  • @mishkathossain2984
    @mishkathossain2984 3 роки тому +3

    Man you are killing it!!!!!!!!!!!! You are one of my favorite instructor!
    You do, what you do best

  • @mightyshenanigans2607
    @mightyshenanigans2607 3 роки тому +12

    I'm prepping for my final Google round and your videos have been essential in my study. Thanks a ton!

  • @ratikdubey4375
    @ratikdubey4375 2 роки тому +16

    Hey - can you make a video for Wiggle Sort 2?
    As always, this was super easy to understand. Thanks!

  • @ngneerin
    @ngneerin 3 роки тому +13

    Would love a video around your interview details from Google. What rounds were there, what complexity, what was expected, etc.

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

      This can be found easily if you search on google btw

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

    Just a heads up, the problem solved in this video differs slightly from Leetcode's version of the problem. Leetcode's version requires values to be strictly greater or less than ("equal to" is not allowed), so the logic presented here, while solid for the problem shown in the video, breaks down when applied to Leetcode's version of the problem. One example of a test case it fails is [5,5,5,4,4,4,4] because simply swapping successive pairs of elements can't move one of the four values all the way back to the start of the array where it is needed. A solution for the Leetcode version of the problem requires different logic entirely.

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

    interesting that if the requirement is for string < > rather than =, this approach completely breaks down

    • @udayptp
      @udayptp 2 роки тому +2

      It didn't work for input array [5,5,5,5,4,4,4,4]

    • @haitamelgharras
      @haitamelgharras 8 місяців тому

      @@udayptp we must handle the case of a==b also

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

    Thanks!!! Our teacher gave this question but with given median, perhaps it for make it easier. but now i see your solution and it is so simple!

  • @tiberiusvetus9113
    @tiberiusvetus9113 3 роки тому +7

    Lol. Google pays well but not enough to afford Leetcode. You're hilarious.

  • @jackwalker2947
    @jackwalker2947 3 роки тому +3

    Man of explanation 🔥

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

    You could have made the if statement smaller. if (not i%2 ^ nums[i] < nums[i-1]). That way you xor over the both cases and only if both are true or false it will ttigger. It is slightly different though, as the second case would also be executed with

  • @Kshitij-fw7bh
    @Kshitij-fw7bh 3 роки тому +4

    Thanks bro for ur videos.

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

    This explanation was spot on. So grateful

  • @poptart007-b2r
    @poptart007-b2r 3 роки тому +1

    "Google pays me pretty well but not enough to afford leetcode premium of course" xD lmao cracked me up

  • @gasgeek434
    @gasgeek434 3 роки тому +3

    This man was born to explain.

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

    Your explanations are awesome! Would you be able to do a video on 1405. Longest Happy String?

  • @anshdholakia1060
    @anshdholakia1060 3 роки тому +3

    There are so many types of sorts it's crazy. Bogo sort is my favorite tho xd

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

      I believe there is a bogobogo sort too.

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

    rip our boy posted this on valentines day. we need a neetgirl for our neetcode

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

    Hi Neetcode,
    Thank you for the remarkable explanation.
    Can you please make a video on split BST ? That is premium leetcode problem and there are no explanation videos available for that problem.
    Thanks again for the videos and making difficult problems enjoyable.

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

    Hey @NeetCode good works. I like your works. Could you please make a video on wiggle sort 2? thank You

  • @tchsu1140
    @tchsu1140 2 роки тому +2

    Do you mind also covering wiggle sort II?

  • @haitamelgharras
    @haitamelgharras 8 місяців тому

    Thanks NeetCode, I tried to solve the wiggle sort 2 with the same approach but it doesnt work, even when trying to handle the equal case, I think about finding the first diff value and swap one of the repeated values with this diff value but it doesnt work

  • @blancosj
    @blancosj 3 роки тому +2

    I am following you since a year and I will have interview with google soon. Would you consider your videos to have google interview level?

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

    This solution, while it seems simple, doesn't work on Leetcode for the test case [5,5,5,4,4,4,4].
    This is because it is only swapping locally, and so fails to deal with cases where there are multiple repeated elements. Here, there will be a number of 4's at the end, instead of the array being [4,5,4,5,4,5,4].

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

    Thanks for the video! Can you please solve Wiggle Sort 2, Leetcode. 324

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

    Interviews are to understand how you think!

  • @mynk_rjpt
    @mynk_rjpt 3 роки тому +2

    Your explanations are awesome 🔥.

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

    Thanks for this.

  • @anandkumar.3307
    @anandkumar.3307 3 роки тому +2

    Haha i thought this is some different problem but actually I have solved it on leetcode itself but with some different name which is not a premium problem though !!😃😃

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

      Wait really, which one is it?

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

      @@NeetCode the second version of it. Num on leetcode: 324 .
      instead of (gt or eq) or (ls or eq) you have (gt strictly) or (ls strictly)

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

    You're awesome!!!

  • @weifang9143
    @weifang9143 2 роки тому +2

    [5,5,5,4,4,4,4] will make the solution invalid?

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

      Why would that solution not work?
      If we skip to index 3 (the first four) because we don't have to swap any of the fives, and even if we did it wouldn't matter
      3 is odd which means we want it to be larger than or equal to the elements around it
      We find that it is less than index two so we swap those
      We now have
      [5,5,4,5,4,4,4]
      We move on to index 4 which is even so we want it to be less than or equal to neighbors
      It is so we do nothing and all the rest are 4 so we do nothing
      Now if we write out the list with greater than equal, less than equal signs we get
      5 = 4 = 4 =4
      Which is true
      Of course the solution
      [4,5,4,5,4,5,4]
      is more pleasing but it isn't any more correct unless you specify that you are looking strictly < and > rather than =

  • @dipeshjoshi8064
    @dipeshjoshi8064 2 роки тому +2

    [5,5,5,4,4,4,4] this input is not Working. Since you are running loop from index 1 to n, first element is excluded.
    I have used different algo, which might be helpful to other.
    1. run quick selection algorithm for index = n/2. (Make sure to run it inplace, since it is mentioned in question.)
    2. after running quick select your median would be at proper place. So now you can assign left pointer at 0th index and right pointer on (n/2)+1 the index.
    3. Now run loop over array and alternatively (i.e. index%2==1) you can swap a[left] with a[right].

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

      The problem isn't that the loop runs from index 1 to n and excludes the first element - it's that this entire algorithm doesn't work properly on the Leetcode version of this problem, because Leetcode's version of the problem requires values to be strictly greater or less than ("equal to" is not allowed). So simply swapping the "5"s at indexes 0 and 1, or 1 and 2, would never lead to a correct result - we need to pull over one of the "4"s later in the array. Leetcode's version of the problem requires different logic entirely.

  • @krishnachaitanya1265
    @krishnachaitanya1265 3 роки тому +3

    Wow google cant pay 150 dollars surprised XD. Just kidding. its a penny for google software engineers or may be less than penny :)

  • @johncenakiwi
    @johncenakiwi 3 роки тому +9

    Not enough to afford Leetcode premium 😂😂

  • @AkashSharma-ij4sn
    @AkashSharma-ij4sn Рік тому

    If the array contains duplicate elts then?

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

    What about if input array is [5,5,5,5,4,4,4,4] .

    • @SweetDarkViolet
      @SweetDarkViolet 2 роки тому +2

      The array is wiggle sort itself, no reorder needed.

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

    Yeah Lintcode also wants me to log in using my WeChat account. No thanks China 👍

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

      Thanks God Meta and Google don't collect their users data. China bad US good raaaaaaaaaaahhhhhhh 🦅🦅🦅🦅🦅🦅

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

      ​@@andrewknyazkov6877lollll

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

    Google should give employees free leetcode premium as perk~

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

    "google pays me well but not well enough to afford leetcode premium". Come on man!

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

    who's here because they tryna solve Wiggle Sort 2 first lol

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

    Lintcodee user

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

    Looking forward to watching wiggle sort Ⅱ : )