Search in Rotated Sorted Array II - Leetcode 81 - Python

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • Solving leetcode 81, Search in Rotated Sorted Array II, today's dailly leetcode problem on august 9.
    🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    Problem Link: leetcode.com/problems/search-...
    0:00 - Read the problem
    2:02 - Calculating halfway
    3:12 - Drawing Explanation
    13:57 - Coding Explanation
    leetcode 81
    #neetcode #leetcode #python

Komentáře • 31

  • @aadil4236
    @aadil4236 Před 11 měsíci +10

    I feel much safer doing daily leetcode challenges by your return. Thank you! Suggestion: We would love to see explanations of weekly contest as well. After it ends of course.

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

    POV: *you are struggling on a leetcode problem*
    then you found neetcode have solve it
    the BEST feeling ever

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

    glad to having you post regularly again.

  • @carsonfreeman6955
    @carsonfreeman6955 Před 11 měsíci +5

    These explanations are amazing!

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

    A small improvement:
    If nums[l] == nums[m] and nums[l] != nums[r]
    It's guaranteed that the pivot is right of middle i.e. you are on the higher/right part of array
    Because if the pivot is on the left of middle, it means that middle -> right must all be the same number that loops back to the left, however as middle != right (as left != right and left == middle) this isn't the case
    So only l += 1 in the case of nums[m] == nums[l] and nums[l] == nums[r], and extend the other case to be

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

      in the case of nums[m] == nums[l] and nums[l] == nums[r] why only l++, do e- - as well. more efficient.. eliminate the same start and end elements because they aint our target. this will help shorten up the search space.

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

    Good to see you back buddy.

  • @Scarsofevil
    @Scarsofevil Před 3 měsíci +1

    If this problem worstcase senario is o(n). Can't we just integrate through the array and return True in the worstcase scenario?

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

    Thorough explanation as always!

  • @MP-ny3ep
    @MP-ny3ep Před 11 měsíci

    Great explanation as always . Thank you

  • @josepadilla4024
    @josepadilla4024 Před 5 měsíci +1

    He sounded so mad at this problem haha

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

    Thanks for the daily

  • @uttamkumarreddygaggenapall2070

    Thank You

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

    the question mentions to decrease the overall operation steps. how does this algo do that?

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

      It doesn't force a linear search, it always tries to do a binary until it's stuck and removes elements 1 by 1 until it can again
      What they meant by that line I think is even tho worst case is O(n) they wanted a better AVG case

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

      @@polycrylate got it, thanks

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

    Wouldn't it be easier if we just sort the input first and then apply traditional Binary Search? in this problem, we don't need the target's index anyway. It works in this case where we just need to enter true or false.

    • @panmacabre9895
      @panmacabre9895 Před 11 měsíci +6

      sorting would take O(nlogn)

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

      @@panmacabre9895 Yeah. That might be the issue. So, the above solution is the best, if we have to return index, we can with just do a small change

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

    why not just doing:
    return target in nums
    EASYYYYYYYYY

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

    2:08 which day? which problem

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

    sometimes you can understand is that left or right portion not only by left & mid pointers, but also by mid & right pointers, so you will omit some linear operations. Here is the code:
    ```
    class Solution:
    def search(self, nums: List[int], target: int) -> bool:
    lp, rp = 0, len(nums) - 1
    while lp nums[rp] or nums[mp] > nums[lp]: # left sorted portion
    if nums[lp]

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

      On #equal you should move both lp and rp

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

    Would "target in numbers" in Python work?

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

      That is basically a linear scan, it may get accepted but i think it's not the intended solution.

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

    thank you daddy

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

    Not the best problem. Because eliminating left pointer 1 by 1 in the worst case would still be O(n) so it doesn't improve anything if you simply just linearly search.

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

    imo, a little bit overcomplicated... the only case we need to handle in N time - skip duplicates if nums[0] == nums[-1] otherwise it is original solution:
    l = 0
    while l < (len(nums) - 1) and nums[l] == nums[-1]:
    l += 1
    # Code for Search in Rotated Sorted Array I problem
    Beats 86.73% of users with Python3
    right or I'm missing something?

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

      Why not move both left and right pointers?

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

    the only catch in this problem is when you don't know which part is sorted so you just compare mid value with s and e and if (s and mid) are equal then s++ or if (mid or e) are equal then e--

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

    Worst explanation