Find Peak Element - Leetcode 162 - Python

Поділитися
Вставка
  • Опубліковано 26 сер 2024

КОМЕНТАРІ • 64

  • @mirceafeder4945
    @mirceafeder4945 3 місяці тому +32

    RIP to all those who got this in an interview without solving it before

    • @tuandino6990
      @tuandino6990 21 день тому

      I think if the interviewer mention o logn, it will be very straightforward

  • @ssgojekblue
    @ssgojekblue Рік тому +79

    peak comment

  • @vasujhawar.6987
    @vasujhawar.6987 Рік тому +19

    Thank you so much for building up the intution, I am going to buy lifetime Neetcode subscription, once I crack a job. You are the best instructor and teacher.💝💝

  • @donotreportmebro
    @donotreportmebro 8 місяців тому +4

    if you calculate mid as int mid = left + (right - left) / 2, it's already a left biased mid (there an alternative approach to make mid right biased), in the case of left biased mid do (m + 1) instead of m - 1 to not worry about out of bounds on the left, left biased mid protects you from the out of bounds condition happening on the right

  • @m.kamalali
    @m.kamalali Рік тому +6

    we can simplify the code a little bit more
    l,r=0,len(nums)-1
    while l < r:
    mid = (l+r)//2
    if nums[mid]< nums[mid+1]:
    l=mid+1
    else:
    r=mid
    return r

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

      I can't understood bro

    • @vukanoa
      @vukanoa Рік тому +5

      @@takerixgaming4170 Well, it's the same as Neet's, but there are three differences:
      1. He uses:
      *mid = (l+r)//2*
      instead of:
      *mid = left + ((right - left) / 2)*
      because he doesn't have to check for the overflow, as Neet himself noted as well. He just showed us this as a general good thing to know about a Binary Search.
      2. His first if statement is:
      *if (nums[mid] < nums[mid + 1])*
      instead of:
      *if (mid > 0 and nums[mid - 1])*
      because this way you don't have to check for "mid > 0" or "mid < len(nums) - 1". Why is this the case? Well, when calculating "mid" pointer at the beginning of the loop we're using an integer division. Meaning it's always "cut down". So we're sure that nums[mid + 1] will never be out of bounds.
      3. He doesn't have an "else" statement that breaks the loop, i.e. that returns "mid", instead his while loop breaks once left becomes greater than or equals to the right.
      Thus, at the end, out of the while loop, just return "left", since we're sure that is the answer.
      Run a Simulation or two on this code and you'll get it.
      I hope this helps.

  • @ngneerin
    @ngneerin 10 місяців тому +11

    Go mid
    If peak return
    If right is greater drop left
    If left is greater drop right
    Repeat

    • @kryptoknight992
      @kryptoknight992 4 місяці тому +1

      what if both left and right element are grater than mid?

    • @ngneerin
      @ngneerin 4 місяці тому

      @@kryptoknight992 Go any way depending on which side your program is checking. Both side there is at least 1 peak

  • @skairways
    @skairways 2 місяці тому

    Great explanation, thanks

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

    Awesome explanation

  • @shivamsiddharthasinghrajaw7671

    A greedy binary search if you will

  • @anujsingh-ej9vb
    @anujsingh-ej9vb Місяць тому

    what if we are at a point and on both the sides we find numbers which are greater than the current number, then in that case, which side do we need to go with our Binary Search?

  • @its_shivam_jha
    @its_shivam_jha 2 місяці тому

    thank you...built the intuition

  • @krzym1
    @krzym1 29 днів тому

    I don't get the explanation at 9:29 if m==0 then it IS greater than its left neighbour as according to problem description the outOfBound element is -inf

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

    You are literally the goat.

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

    I don't know how you make it look pretty simple🙂

  • @anonanon2625
    @anonanon2625 5 місяців тому +1

    ua-cam.com/video/HtSuA80QTyo/v-deo.html
    MITx 6.006 if you want a deeper understanding on Peak Finding. Professor Devadas also talks about 2D Peak Finding.

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

    Great Explanation.

  • @user-le6ts6ci7h
    @user-le6ts6ci7h Рік тому +1

    Whenever I solve this problem , "I am peaky blinder" is the song that comes into my mind

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

    Perfect explanation

  • @neiljohn2637
    @neiljohn2637 6 місяців тому +1

    It won't work for when the list contains the same number throughout right? Coz this code will return m rather None.

  • @erensolmaz2435
    @erensolmaz2435 18 днів тому

    I was thinking what if more than 1 peaks and how i could find all peaks with this solution. Then i read expl. It says any of the peaks. So we just need to get close to one peak and look for it index

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

    Thanks.

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

    Inspiring👍

  • @jisanson
    @jisanson 7 місяців тому

    elegant 👍

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

    What would happen if you had [3,2,3,3]. Then the peak element would be on the left but both are greater than the 2 so how would you know?

  • @antonr786
    @antonr786 Місяць тому

    So basically we just guessing on which side we gonna have the peak?

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

    what if it s a valley element i.e 1 2 1 2 3 with mid at the middle 1. Then both sides are equally applicable for binary search?????

    • @VictorNascimentoo
      @VictorNascimentoo 8 місяців тому +3

      You only need to guarantee the side you're looking for has a peak. You can return any peak, not the global max.

  • @edwardteach2
    @edwardteach2 3 місяці тому

    U a Peak God

  • @mayanksingh7814
    @mayanksingh7814 Місяць тому +1

    A simpler one:
    def findPeakElement(nums: List[int]):
    low, high = 0, len(nums)-1
    while low < high:
    mid = low + (high - low) //2
    if arr[mid+1] > arr[mid]:
    low = mid + 1
    else:
    high = mid
    return low

  • @alisktl
    @alisktl 11 місяців тому +1

    I believe the problem cannot be solved in O(log n) time. For example, given the sequence [1, 2, 1, 2, 3, 4, 5, 7, 7], the correct answer should be 1. However, the solution with a time complexity of O(log n) would yield 7, which is incorrect.

    • @Pratik-tk6ts
      @Pratik-tk6ts 11 місяців тому +3

      no two neighbouring numbers are equal, check constraints

    • @alisktl
      @alisktl 10 місяців тому +1

      @Pratik-tk6ts thanks, did not see that constraint

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

    I'm not sure I understand why there's always a peak. What would be the peak in the array [1, 2, 2, 1]? If the "peak" is directly next to an equal value, it's not strictly greater than it's neighbors, and if the values on the ends of the array aren't larger, is there actually a peak?

  • @NeetCodeIO
    @NeetCodeIO  Рік тому +4

    If you're looking for todays daily LC (design browser history) 👉ua-cam.com/video/i1G-kKnBu8k/v-deo.html

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

    My solution to this problem actually by searching the maximal value of an array then return the index of that value. If using python we just need to call max and using index() method

    • @xrobstar
      @xrobstar 9 місяців тому +3

      The question states that an O(log n) solution is required. Searching for the max element is a linear O(n) solution.

  • @yuurishibuya4797
    @yuurishibuya4797 6 місяців тому

    For binary search to work, the input must be sorted, is it sorted?
    I don’t see that in the requirements/constraints.
    I don’t see about repeated elements either.
    Such a crappy problem description.

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

      It is not sorted. And no, it doesn't have to be sorted unless you are searching for an element in an array. Binary search is more about taking the middle of array and ignoring other side. So in this problem when we take mid element and we see that right side is less but left side is greater we go to left. Peak might be in right side too but it is not guaranteed because it can monotonically decrease. How about left side ? Either this monotonically increases or it goes down and both case is acceptable so we just ignore the right side. Binary search is much more than finding element in sorted array.

    • @adityapratapsinghtomer3624
      @adityapratapsinghtomer3624 21 день тому

      @@shamilgurban6439 Legend !! Thanks man

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

    you need a better font or stop using one letter variables. I've got a time limits because it turned into an infinite loop because i put 1 instead of L in the middle point equation

    • @DinujayaRajakaruna
      @DinujayaRajakaruna Рік тому +13

      I mean that error is easy to spot if you understood his explanation. The point of the video isn't for the viewers to copy his code, its for them to understand the explanation.

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

      @@DinujayaRajakaruna i used another language and wasted a bunch of minutes becaise of it. i understand that leetcode is not about good code, but fast one, but still why not to improve to the sane degree?

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

      @@DeathSugar While I do agree making things more readable helps in general, I think someone that is familiar with binary search will immediately understand what things like "r" and "l" stand for. Especially after he mentions what they are in the video.

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

      @@DinujayaRajakaruna problem not with understaning. It uses "magic" evaluation to prevent from violating boundries - thats where I messed up with the 1/l.

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

      @@DeathSugar it's not "magic" though, m = l + floor((r-l)/2) is exactly how you calculate the midpoint of l and r without getting any overflow issues. The standard way is m = floor((l+r)/2) but if l and r are large enough this overflows. He mentions this in the video, and this fact is mentioned in the Wikipedia page for binary search as well.