Search in rotated sorted array - Leetcode 33 - Python

Поделиться
HTML-код
  • Опубликовано: 7 фев 2025

Комментарии • 349

  • @NeetCode
    @NeetCode  4 года назад +23

    🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    Search in Rotated Sorted Array II - ruclips.net/video/oUnF7o88_Xc/видео.html

  • @expansivegymnast1020
    @expansivegymnast1020 2 года назад +278

    This is lowkey one of the hardest problems I've done so far. Great video!

    • @feitan8651
      @feitan8651 Год назад +17

      I watched this video for 3 times to understand the solution, so yeah ur right.

  • @wonderfulsnow
    @wonderfulsnow 4 года назад +340

    You can use binary search to find the pivot index, and then another binary search in the appropriate part of the array.

    • @erikm9768
      @erikm9768 3 года назад +37

      This is a really good idea, less confusing

    • @aadil4236
      @aadil4236 2 года назад +8

      How?

    • @import_numpy_as_pd2330
      @import_numpy_as_pd2330 2 года назад +40

      @@wonderfulsnow you responded to a year old comment, damn!

    • @dk20can86
      @dk20can86 2 года назад +20

      @@wonderfulsnow Thank you, this is so much easier to grasp mentally than the other solution. This was my first thought but then i dismissed it because i couldn't figure out how to know if the pivot was greater or less than my current element.

    • @thongchaikolyutsakul4123
      @thongchaikolyutsakul4123 2 года назад +1

      @@wonderfulsnow good explanation!!

  • @kevindebruyne17
    @kevindebruyne17 2 года назад +123

    One of the most annoying questions of Leetcode explained so easily! I hope I could develop the skill to implement this without getting confused again and again! Amazing work!

    • @tanaykamath1415
      @tanaykamath1415 2 года назад +3

      so me!

    • @pinakadhara7650
      @pinakadhara7650 Год назад

      Can't relate more

    • @fays_bn4321
      @fays_bn4321 Год назад +1

      Same, you should have that KDB vision !

    • @LeakCentral
      @LeakCentral Год назад +1

      So amazing, Kevin DeBruyne doing Leetcode just for fun when he makes millions playing soccer

    • @dhawalc4975
      @dhawalc4975 Год назад +3

      I like how you can talk now!

  • @tanvirahmed7993
    @tanvirahmed7993 11 месяцев назад +7

    I struggled with this problem for 3+ years and tried to memorize the solution as I was always got confused when thinking many different options. This is the best explanation.

  • @ahmedbenchakhter
    @ahmedbenchakhter Месяц назад +3

    Thank you for your explanation! I believe the most straightforward way to clarify this problem and the previous problem *153. Find Minimum in Rotated Sorted Array* is to recognize that in a rotated array, we always have two portions: two sorted arrays divided by a pivot.
    For example, consider the array [4, 5, 6, 7, 0, 1, 2]:
    We have two sorted portions: [4, 5, 6, 7] and [0, 1, 2].
    So, whatever index you are at, there is always a sorted part. It’s easy to check if your target belongs to it.
    Another perspective on the same array [4, 5, 6, 7, 0, 1, 2]:
    If we split this array at the midpoint (6), we get two parts: [4, 5, 6] (sorted) and [7, 0, 1, 2] (unsorted).
    By checking the sorted part, we can determine whether the target lies in it. If not, we focus on the other part.
    One more example: in the array [6, 7, 0, 1, 2, 4, 5]:
    Splitting at the midpoint (1), we get [6, 7, 0, 1] (unsorted) and [2, 4, 5] (sorted).
    If the target is, for example, 3, it lies in [2, 4, 5], so we focus there. If not, we search [6, 7, 0, 1].
    By following this approach, we can efficiently narrow down the search in both problems. Thanks again for the great explanation!

  • @symbol767
    @symbol767 2 года назад +49

    This problem was difficult as hell because of how we need to setup so many conditions and move the pointers correctly depending on where we are currently with mid pointer, jeez. I hate this problem honestly.
    Thank you for your solution, liked and commented for support.

    • @averychen4633
      @averychen4633 Год назад +3

      yes. Seriously, what is the point of this kind of problem?

    • @sebastianramirez5781
      @sebastianramirez5781 6 месяцев назад +1

      I feel like you've watched every single one of these videos and left a comment as to why it was hard, been doing neetcode 150 and you're always there

    • @dekumidoriya-qi2is
      @dekumidoriya-qi2is 5 месяцев назад +2

      a simple or rather more intuitive approach is to find the pivot and then apply binary search on (0, pivot-1) and (pivot, n)

    • @AsliArtist
      @AsliArtist 5 месяцев назад +1

      @@dekumidoriya-qi2is Yeah I thought so too, to be honest once you find the pivot you dont even have to apply it on both sides just apply to the side where the lower bound of the array is less than the target.
      So this problem can be an extension to the previous problem in the LC 150 list of finding the minimum element as that approach can be used to find the pivot.

    • @dekumidoriya-qi2is
      @dekumidoriya-qi2is 5 месяцев назад +2

      @@AsliArtist Yep i missed that nice catch once we know the pivot we can actually check only one side which is relevant !!

  • @iamsmitthakkar
    @iamsmitthakkar 2 года назад +23

    thanks for the solution. For those who gets confuse in the if conditions (just like me) in this solution, you can refer to below solution. It works
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    l, r = 0, len(nums)-1
    while l nums[mid] and target

    • @allnewdevelopers
      @allnewdevelopers 2 года назад

      thanks this way makes way more sense to me did it myself just following your comments guide! The OR in his solution threw me off lol

    • @shonoma
      @shonoma Год назад

      I found your explanation very helpful.
      As @allnewdevelopers mentioned, I was also confused about the OR.
      Thank you for your contribution!

    • @xaviermeimei
      @xaviermeimei 6 месяцев назад

      good ol' demorgan's law. thanks!

    • @chrisaa100
      @chrisaa100 3 месяца назад

      This is the easiest version, Glad I saw your comment 🙂

    • @homeroserna9983
      @homeroserna9983 2 месяца назад

      This solution is more intuitive for me. Its a lot easier to think about the cases where your target is in the range of the current section you are in, rather than thinking about all the cases where it falls out of range (like how neet thinks about it)

  • @ThePacemaker45
    @ThePacemaker45 Год назад +25

    First leetcode medium I was able to solve by myself without looking at the solution cause of your solution vid for Problem 153. I know it's just baby steps but it's sooo encouraging. Thank you so much man 😅

  • @danielgareev8877
    @danielgareev8877 2 года назад +182

    For me this solution was a bit more intuitive:
    l = 0
    r = len(nums) - 1
    while l = nums[l]:
    if nums[l]

    • @Arthurgarzajr
      @Arthurgarzajr 2 года назад +7

      I agree. This one reads better.

    • @crosswalker45
      @crosswalker45 2 года назад +5

      Hey.. Even I came up wid same solution

    • @djsosofresh
      @djsosofresh Год назад +3

      Agreed, able to reason this solution much more easily

    • @jeffinkachappilly9708
      @jeffinkachappilly9708 Год назад

      Thanks for sharing this.(really easy to understand)

    • @pinakadhara7650
      @pinakadhara7650 Год назад +1

      This is much easier to read

  • @sudiptosen5915
    @sudiptosen5915 3 дня назад

    Thanks a lot for that graph explanation. I'll never forget this fact about array rotation!
    You're a great teacher for going the extra mile to try and explain the reasoning behind your actions

  • @kryddan
    @kryddan Год назад +8

    I think of it like this:
    if mid > than R, everything left of mid is sorted
    if mid < than R, everything right of mid is sorted
    Check if target would fall into the range of the sorted part, and if so move the L or R pointer to binary search that segment. Otherwise move to the unsorted segment and repeat:
    def search(nums: List[int], target: int) -> int:
    l, r = 0, len(nums) - 1
    while l

    • @victoriatfarrell
      @victoriatfarrell Год назад +1

      Thanks for sharing! I found your presentation of it helpful.

  • @sidazhong2019
    @sidazhong2019 3 года назад +22

    if nums[l]

    • @leeroymlg4692
      @leeroymlg4692 2 года назад

      Nice one. I like this solution better

    • @youngsuko8910
      @youngsuko8910 7 месяцев назад +1

      This make so much sense to me! Thank you

    • @SKSB5502
      @SKSB5502 3 месяца назад

      Thank you bro...made more sense to me now

  • @legendarygaming7790
    @legendarygaming7790 4 года назад +17

    very clearly explained. Thank you for this. I was also considering the right rotated array and was trying to find a generalised soln. guess that the question only demanded for the left roated array.

  • @zenyujin_
    @zenyujin_ Год назад +5

    I think using the inequality signs like this makes it a lot more intuitive:
    def search(self, nums, target):
    L, R = 0, len(nums) - 1
    while L

  • @rextlfung
    @rextlfung 4 года назад +12

    Great explanation! The visuals really helped. Somehow when I tried this problem I got stuck with "and" conditions but now it makes complete sense why it's "or"

    • @JackWutang
      @JackWutang 3 года назад +7

      It's not wrong to use "and" instead of "or". Depending on what your condition is, "and" could work as well.

    • @carloscarrillo201
      @carloscarrillo201 2 года назад +1

      You can also use 'and':
      if target >= nums[start] and target < nums[mid]:
      end = mid - 1
      else:
      start = mid + 1

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk 3 года назад +12

    Amazing explanation, will donate when I get the job.

  • @priyavardhanpatel6155
    @priyavardhanpatel6155 Год назад +1

    I have spent more than 2 hours on this problem but couldnt solve it. You made it very clean and simple, thank you so much!!!

  • @user-wf3bp5zu3u
    @user-wf3bp5zu3u 2 года назад +7

    That "or" is what got me. I figured out you could check for either condition, but not that you had to check both.
    I got confused and assumed that knowing whether you were in the right/left sorted portion gave you some bounds guarantees, and you only had to check one of the conditionals.
    Going to spend some time drawing this out to make sure it sunk. Thank you so much NeetCode!

    • @iamsmitthakkar
      @iamsmitthakkar 2 года назад +7

      I got confused with those condition as well. You can try my solution (commented above) with different if/else conditions.
      class Solution:
      def search(self, nums: List[int], target: int) -> int:
      l, r = 0, len(nums)-1
      while l nums[mid] and target

  • @free-palestine000
    @free-palestine000 2 года назад +6

    this has been one of the most solutions to wrap my head around for some reason

  • @vuminhnhat4061
    @vuminhnhat4061 3 года назад +8

    To reduce the if else condition in left and right sorted portion. I think, for each sorted portion, we can apply binary search directly. If you find target value, then return. If not find, we move left and right index respectively.

  • @hankmoody-s6j
    @hankmoody-s6j 2 месяца назад +1

    for the comparison i did this
    if (M > L and L

    • @johnyha9236
      @johnyha9236 24 дня назад +1

      i had something similar, makes the if statement a bit convoluted but is overall a much more cleaner solution to my eyes

    • @hankmoody-s6j
      @hankmoody-s6j 24 дня назад +1

      @@johnyha9236 Great minds think alike brother if the solution is clear on paper then the code is clear

  • @TheKapei22
    @TheKapei22 Год назад

    I started watching your videos for Blind 75. This was the first one I made without watching any video. Your video optimizes what I did, though. Great job!

  • @gregp83
    @gregp83 2 года назад +1

    nice explanation - kind of figured out myself general idea but its so easy to get confused when use "

  • @algorithmimplementer415
    @algorithmimplementer415 3 года назад +10

    Extremely good explanation. LC solutions were confusing at their website, so if I had not found your post, I may not have understood the bunch of if else statements. So, thank you very much!

  • @tanaykamath1415
    @tanaykamath1415 2 года назад

    Man idk how you manage to explain so elegantly!

  • @azamatik3
    @azamatik3 Год назад

    I won't stop talking, you explain so well. Thank you so much

  • @krypton7792
    @krypton7792 3 года назад +3

    Amazing Explanation! thank you, the handwritten stuff really helped me understand this easier.

  • @yoonsw12
    @yoonsw12 Год назад

    Excellent solution with great visualization of the graph. Really helped me understand why we need to "pivot" and decide whether to stay put on the portion we chose or pivot entirely to the other side and conitnue.

  • @laughingdog0
    @laughingdog0 16 дней назад

    This was a great explanation, the best of several that I watched. Thank you!

  • @legspinismylife
    @legspinismylife 3 года назад +39

    array problems make me feel like i have IQ below 80

  • @Andrew-dd2vf
    @Andrew-dd2vf 9 месяцев назад

    The visualization did wonders to help me tackle this problem. I didn't even have to look at the actual implementation to solve it after looking at it!

  • @rudya.hernandez7238
    @rudya.hernandez7238 5 месяцев назад

    I had worked out this solution, but just couldn't break it down into code. The moment I saw the simple comments "left sorted portion" and "right sorted portion" it was cake. Thanks Neet

  • @nikhil_a01
    @nikhil_a01 Год назад +1

    Overall a pretty good explanation but IMO there is one flaw at 8:08. Our target is 0 and we narrow our search range from [4,5,6,7,0,1,2] down to [0,1,2]. But then we say [0,1] is the 'left side' because [0,1,2] is sorted?
    Clearly what we care about is not left or right side, but whether our subarray is a sorted subarray or a rotated sorted subarray. The key observation is that *_when you take the mid of a rotated sorted subarray, one side will be sorted, and the other will be rotated sorted._*
    For example, with [4,5,6,7,0,1,2], if the mid value is 6, then we get a split of [4,5] and [7,0,1,2]. If the mid value is 1 then the split is [4,5,6,7,0] and [2]. Which of the left or right side is sorted and which is rotated sorted varies. And as a special case we can also have both sides sorted, like if the mid value is 0.
    I think it'll also be helpful to summarize all the cases:
    If we're in the sorted portion ('left side'):
    • If target > mid val then search right
    • Else target = left val then search left
    Else we're in the rotated sorted portion ('right side'):
    • If target < mid val then search left
    • Else target >= mid val:
    • If target > right val then search left (i.e. rotate around)
    • If target

  • @ronifintech9434
    @ronifintech9434 2 года назад +28

    Dinner on me if I pass my Google super day with your 150 question list :-)
    beautiful explanations!!

  • @mohamedantar1249
    @mohamedantar1249 9 месяцев назад +1

    thanks, you always have the best explanation

  • @amardeepsingh7528
    @amardeepsingh7528 Год назад

    The explanation with graph is really helpful

  • @ThomasCrosbie-Walsh
    @ThomasCrosbie-Walsh 7 месяцев назад +1

    I feel like this solution makes slightly more sense:
    left = 0
    right = len(nums) - 1
    while left = nums[left] and target

  • @Taeesh-nc6nv
    @Taeesh-nc6nv 5 месяцев назад

    For those struggling with this problem I suggest working on Leetcode 153 or watch his explanation for LC 153.
    That problem basically gives u an idea how to do BS on rotated arrays and then solving this problem becomes easier.
    I personally tried solving this problem by my own , failed and then changed if statements to satisfy the test cases which were failing and finally got it to pass all cases. I then watched this vid and the approach was the same. Below is my solution -
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    l = 0
    r = len(nums) - 1
    while l

  • @jameszhou20850
    @jameszhou20850 4 года назад +2

    Thanks. Your explanation is very clean

  • @andreytamelo1183
    @andreytamelo1183 2 года назад

    Thanks!

  • @deepakg9220
    @deepakg9220 Год назад +1

    class Solution:
    def search(self, nums: List[int], target: int) -> int:

    if target in nums:
    return nums.index(target)
    else:
    return -1
    #sometimes my genius is almost frightening

    • @vishwaskotegar5248
      @vishwaskotegar5248 Год назад

      who is this genius??? it works ! ... NEET!!!!!!!!!!! you need some explaining to do for spoiling us with lot of code

  • @bitlandsaga
    @bitlandsaga Месяц назад

    Thanks for such an easy explanation 👏🏻

  • @amitupadhyay6511
    @amitupadhyay6511 3 года назад +6

    Even simple explaination :
    If A[mid] >= A[l] , then A[l:mid+1] is increasing subarray, so explore this array only if target between A[l] and A[mid] , the other part is like dumb yard, throw all other conditions in dump
    IF A[mid] < A[l], then everything to left of mid is a dump yard. Use the right side of mid only if A[mid] < target

  • @positive.stories
    @positive.stories 4 года назад +4

    Thank you very much!! I learned a lot from this video.

  • @sudarshanh.v993
    @sudarshanh.v993 4 месяца назад

    So, a bit late to the party, but just repeating one of the top comments. I basically found the pivot, and then based on how large the target is, search in one of the two sorted subarrays.
    I felt that this solution is much more intuitive than the one here. Though understanding how to find the minimum was a pain.

  • @shashikantdivekar7839
    @shashikantdivekar7839 3 года назад +3

    Thank you Sir. Very nicely explained and useful.

  • @oluwatosin001
    @oluwatosin001 8 месяцев назад

    i knew the methods but so many things we were testing and it tripped me off

  • @eeshanprabhu1609
    @eeshanprabhu1609 5 месяцев назад

    This is one of the easiest problems using python, this is the solution that I used:
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    try:
    return nums.index(target)
    except:
    return -1

    • @leetcodemonkey
      @leetcodemonkey 5 месяцев назад

      Bruh the soln you used doesn't come under Binary Search algorithm. You're just using the index method 💀
      Your interviewer might need you to solve the question using Binary Search

    • @thetruth65756
      @thetruth65756 3 месяца назад

      lmao

  • @maneeshreddy3825
    @maneeshreddy3825 4 года назад +4

    Best explanation!!!

  • @DineshrajanT
    @DineshrajanT Год назад +1

    @Neetcode
    it must and condition and not or .
    Here is the correct solution :
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    # we can run a single loop to locate the target but that is not needed here,
    # we need something in O(log N)

    leftPt = 0
    rightPt = len(nums) - 1
    while leftPt nums[middlePt] and target

  • @danielsun716
    @danielsun716 2 года назад

    What I need to say is one thing easily missing, that is nums[l] may be equal to nums[m]. for instance, [3, 1] find the target 1. then you will see what is going on. I think that is the hard edge case to find out.
    So just for the more clear to see the conditions, I like to write elif condition as a beginner just in case for avoiding missing any other conditions.
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    l, r = 0, len(nums) - 1
    while l

  • @TechnoSan09
    @TechnoSan09 11 месяцев назад +1

    finding pivot using binary search in O(log n) then performing another binary search O(log n) to find the key will be little easier but little time intensive but still better than O(n)

  • @ritikajagtiani2984
    @ritikajagtiani2984 3 года назад +2

    Thanks - this is crystal clear!

  • @ahmedzedan1772
    @ahmedzedan1772 2 года назад

    Hello, your videos are awesome! it takes me to the next level. Thank you!

  • @jjayguy23
    @jjayguy23 Год назад

    God bless you man, you are incredible.

  • @bismeetsingh352
    @bismeetsingh352 4 года назад +2

    This is a dope explanation.

  • @moncefabboud2839
    @moncefabboud2839 9 месяцев назад

    I found the left/right sorted portions explanation a bit hard to digest. One other of seeing things that convinced me better is as follows:
    if nums[l] the sub array from l to mid is sorted.
    else => the other sub array from mid to r is sorted.
    Indeed, we can only have one unsorted subarray which includes the pivot.
    Once we think of subarrays as sorted or unsorted, the idea is to check if target is within the sorted subarray if it is, move the left/right to mid to seach within the sorted portion. If target is outside the sorted portion. Keep searching in the unsorted portion by splitting it into a sorted and unsorted parts and repeating the process.

  • @hackytech7494
    @hackytech7494 3 года назад +3

    Fantastic as always ❤️

  • @mostinho7
    @mostinho7 Год назад

    Good explanation from 6:30
    Todo:- take notes

  • @SankalpPrakashECE--
    @SankalpPrakashECE-- 2 года назад

    Beautifully explained

  • @akhma102
    @akhma102 2 года назад

    Great Explanation. Thank you!

  • @ivanwen8335
    @ivanwen8335 3 года назад +1

    when you check with the leftmost value, why used "target < nums[l]", not "target < nums[0]"?

    • @abhicasm9237
      @abhicasm9237 3 года назад

      because the left may not be always 0

    • @ivanwen8335
      @ivanwen8335 3 года назад

      @@abhicasm9237 I tried to change it to 0, it still passed all the tests

  • @ebrahimaliabadifarahani7755
    @ebrahimaliabadifarahani7755 Год назад

    Could also find the pivot in log(n) and then have two sorted array which could be used to do simple binary search which is still log(n)

  • @johnyha9236
    @johnyha9236 24 дня назад

    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    l, r = 0, len(nums) - 1

    while l = nums[r], then that means the diverging point is in the second half
    if ( (target = nums[mid]) or (nums[mid] >= nums[r] and (target = nums[mid] )) ):
    l = mid + 1
    else:
    r = mid - 1

    return -1

  • @karthikr7956
    @karthikr7956 Год назад

    Thanks. A very good explanation.

  • @janvidalal476
    @janvidalal476 2 года назад

    That Visualization Supremacy. 💯

  • @niteshmanem1997
    @niteshmanem1997 2 года назад +2

    This problem makes sense once I see the solution but if I had never encountered this problem before, I do not know how I would have arrived at the solution
    Is it expected in interviews to have new problems we haven't seen before and come up with the solution on the spot? That sounds very difficult

    • @nikhil_a01
      @nikhil_a01 Год назад +1

      Yes, it's completely expected to get problems you haven't seen before in interviews. Rarely you might be lucky and get a problem you've seen before, but you shouldn't expect it. A large part of why this style of interviews was introduced was to test your problem solving.
      This problem is on the hard side in my opinion though. It took me 45 minutes.

    • @bossgd100
      @bossgd100 Год назад

      @@nikhil_a01 did you practice something similar before ?

    • @nikhil_a01
      @nikhil_a01 Год назад +1

      ​@@bossgd100 Not that similar. I'd done about a dozen binary search problems before, but not anything with rotated arrays.

    • @bossgd100
      @bossgd100 Год назад

      @@nikhil_a01 thank you for your answer 👍

  • @jiahernghong3620
    @jiahernghong3620 5 месяцев назад

    You can find the pivot using a binary search, then do another usual binary search except this time with transformed index based on the rotation. It's a O(2 * log n) solution which should be evaluate to O(log n).
    class Solution {
    public int search(int[] nums, int target) {
    int l = 0, r = nums.length - 1;
    while (l < r) {
    int m = (l + r) / 2;
    if (nums[m] > nums[r]) {
    l = m + 1;
    } else {
    r = m;
    }
    }
    int k = l; // l, r, m are all equals
    l = 0;
    r = nums.length - 1;
    while (l target) {
    r = m - 1;
    } else {
    l = m + 1;
    }
    }
    return -1;
    }
    }

  • @finleyy
    @finleyy 3 года назад

    you have the best explanation! thank you

  • @KaneroKikai
    @KaneroKikai 4 месяца назад

    it's such a motivating problem for a newbie in algorithms 😅

  • @Shark_9099
    @Shark_9099 5 месяцев назад +1

    for me if target in nums:
    return nums.index(target)
    else:
    return -1 this is what i understand

  • @sergeychepurnov1328
    @sergeychepurnov1328 3 года назад +2

    Awesome explanation, thanks.

    • @NeetCode
      @NeetCode  3 года назад

      Happy it was helpful!

  • @michaelmarchese5865
    @michaelmarchese5865 11 месяцев назад +1

    This statement being true doesn't mean you are on the left part: "nums[L]

  • @nguyentuan1990
    @nguyentuan1990 7 месяцев назад

    i found this solution to be more intuitive. This problem is exact same as the Min Rotated Sorted Array pronlem.
    so for the Min Rotated Sorted Array problem, the solution is like this
    def findMinRotatingElement(nums):
    left = 0
    right = len(nums) - 1
    '''
    logic is this: do the binary search, compare the mid value with the most right value
    if mid is larger, meaning the min value is on the top half, update left = mid + 1
    else the min value is on the bottom half, update right = mid
    once l = r
    we found the solution
    '''
    while left < right:
    mid = (left + right) // 2
    midValue = nums[mid]
    if midValue > nums[right]:
    left = mid + 1
    elif midValue

  • @footballking5993
    @footballking5993 3 года назад +4

    try:
    return nums.index(target)
    except:
    return -1

    • @cesaralcantara1341
      @cesaralcantara1341 3 года назад +2

      thats not O(log(n) like the problem states, but good easy solution if you want O(n)

    • @motivewave001
      @motivewave001 3 года назад

      follow up would be if the array contains duplicate

  • @Extremesarova
    @Extremesarova Год назад

    Much easier to understand solution (updated conditions):
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    l, r = 0, len(nums) - 1
    while l

  • @今天比昨天厲害
    @今天比昨天厲害 3 года назад +1

    You are amazing!!

  • @kafychannel
    @kafychannel 2 года назад +1

    great work thanks so much!

  • @trungthucnguyen7540
    @trungthucnguyen7540 Год назад

    great solution! Thank you

  • @seizhiro
    @seizhiro 3 месяца назад +1

    I have a 5 word one line , 100% beat solution:
    return Array.IndexOf(nums, target);
    EASY.

  • @gokulnaathb2627
    @gokulnaathb2627 3 года назад +2

    Very nicely explained

  • @ximingwen2542
    @ximingwen2542 Месяц назад

    is it possible L is already in the right sorted portion? Then in this case nums[L] < nums[mid] can't be used to judge if mid is in the left sorted portion, right?

  • @freyappari
    @freyappari Год назад

    Faster solution, no need for a min:
    l, r = 0, len(nums) - 1
    while l < r:
    m = (l + r) // 2
    if nums[m] < nums[r]:
    r = m
    else:
    l = m + 1
    return nums[l]

  • @kanhashukla6067
    @kanhashukla6067 Год назад

    Solution works. Explained well. But this is a very complicated solution. Below is a slightly more readable solution.
    Note : This is not my solution. Its borrowed. I just added a few comments.
    def search(self, nums, target):
    l = 0
    r = len(nums) - 1
    while l = nums[l]: # Checks if the left part is sortted or not.
    if nums[l]

  • @zainio
    @zainio 3 года назад +2

    Line 12 of your solution: Why is it

    • @Terracraft321
      @Terracraft321 2 года назад

      I think he explained it in the video, that the pointer can be the same or smaller than the middle pointer. I could be wrong though.

  • @armand6994
    @armand6994 Год назад

    this one is more eazy to understand
    if nums[left]

  • @whitest_kyle
    @whitest_kyle 2 года назад +2

    Very clear and helpful!
    I got a time limit exceeded warning when trying to submit though :(

  • @yinglll7411
    @yinglll7411 3 года назад +1

    Thank you so much!

  • @Nikhil-Tomar
    @Nikhil-Tomar Год назад

    TO find the target belows what I did.
    First I found if list is rotated or not, If last element < first element. List is rotated an no matter what permutation you choose it will remain this way
    [4,5,6,7,0,1,2]
    If its rotated We divide the array into 2 logical sorted arrays, How.
    Take a pointer at last element and start finding minimum element from last pointer. IN above case 0 is minimum at index 4.
    So 2 lists are 0: 4 < 0 element is not included here and 4:6(len of array - 1).
    Now we first do Binary search on first, if not found second and then return -1 or answer.
    If array is not rotated at all we just apply binary search directly.

  • @RaoVenu
    @RaoVenu 2 года назад +1

    Personally, I find it more intuitive to find the index of the min value and then search the two sections of the array with ranges [0, minIdx -1] and [minIdx, end of array]
    var search = function(nums, target) {
    const minIdx = findMinIdx(nums);
    const bst = (lb, ub) => {
    let l = lb, r = ub;
    while(l < r) {
    const midIdx = l + Math.floor((r - l) / 2);
    if (nums[midIdx] >= target) {
    r = midIdx;
    } else {
    l = midIdx + 1;
    }
    }
    const v = nums[l] === target ? l : undefined;
    return v;
    }
    const left = bst(0, minIdx - 1) ;
    if (left !== undefined) {
    return left;
    }
    const right = bst(minIdx, nums.length - 1);
    return right !== undefined ? right : -1;
    };
    const findMinIdx = nums => {
    let l = 0, r = nums.length - 1;
    while(l < r) {
    const midIdx = l + Math.floor((r - l) / 2);
    if (nums[midIdx] > nums[r]) {
    l = midIdx + 1;
    } else {
    r = midIdx;
    }
    }
    return l;
    }

  • @Emorinken
    @Emorinken 5 месяцев назад

    Thank you very much

  • @SOMESHKHANDELIA
    @SOMESHKHANDELIA 7 месяцев назад

    I was able to solve this problem without looking at the solution, only by updating my code seeing which test cases were failing. If test cases weren't visible, would not have been able to solve it. It was tricky as anything.

  • @james3742
    @james3742 2 года назад

    Great vid, thank you!

  • @fullmetalsmash001
    @fullmetalsmash001 Год назад

    This is a deceptively complex problem. Normally, they say if you're writing a lot of complex nested logic, it means you're doing too much or something wrong.
    Turns out in this case, this problem is the exception. The solution isn't as intuitive or elegant as one would expect, but it's necessary and not too difficult.
    Goes to show you, any correct soluton you can achieve at first is your best answer, then you can work your way to reduce the complexity later if it's possible.

  • @elab4d140
    @elab4d140 5 месяцев назад

    To anyone who is still confused, here's a solution which is easier to understand with comments:
    class Solution:
    def search(self, nums: List[int], target: int) -> int:
    l, r = 0, len(nums) - 1
    while l

  • @khaldouneldanaf36
    @khaldouneldanaf36 2 года назад +1

    for this case, how do you know which way to go?
    [8,9,0,1,|2|,3,4,5,6,7], target = 1

    • @ianokay
      @ianokay Год назад

      This is a great question and captures the confusion of the problem

    • @ianokay
      @ianokay Год назад

      By his bizarre logic and relatively convoluted explanation here, he would find that your middle is NOT in the "left sorted portion" because M < L. Only if your middle is greater than your leftmost first value do you know you're "in a" contiguous portion, where "in a" is just a bad way of saying "the values from L until middle are contiguous/unsplit/don't contain the pivot". If this isn't the case you're in the "right sorted portion". Wtf is "right" about being a number in the middle? No freaking idea, it's upsetting at how illogical it is.
      Anyways, since that first case isn't the case in your example, we "know we're in the 'right sorted portion of the array'". Perhaps just terrible naming on his part??? Then..... by his fantastic logic at 5:03 he says "What if we know our target is less than the mid, well then we know we have to [binary] search the left portion of this array".
      So in short, you figure out if you're in the left or right portion. If you're in the left you check if your target is in the range. If you're in the right (again, wtf does this mean!?), you check if your target is less than the middle FIRST 😑 to make sure what you're about do isn't all fudged up (check the right). If it is... then you go "ooops" and go back to checking/using the left you were just about to throw away.
      Hope this helps, although it sorta doesn't since it's so stupid feeling even when you grok it.

    • @ianokay
      @ianokay Год назад

      What he's trying (and failing) to say, is that if when you're in the middle the leftmost value is not less than you, then you know that a split/pivot has occurred. If you detect that the left of middle is in split, he says that you're in "the right sorted portion", where by "right" he means ... fudge if I know honestly, it sounds stupid.
      He explains at 1:57 "Lets say the left side is exclusively greater than the right side, because that's what it means by rotating the array"
      So he's just defining left and right in what is I think mostly WRONG and arbitrary logic to where "in the left side" means THE PART AFTER THE SPLIT POINT (OF LARGER NUMBERS) (which was actually the freaking right side of the number line before the split).
      Left and right to him don't even have anything to do with left and right of center even though this whole thing revolves around a center number.
      He even erroneously says at exactly 1:49 "We took some pivot and then swap these halves around". *THEY ARE NOT HALVES*. WE'RE LITERALLY IN A PROBLEM ABOUT HALVES AND MIDDLES AND HE'S MISUSING THE WORD HALF TO NOT MEAN LITERALLY HALF. They're just parts because the split isn't always in the middle!!! This flawed and erroneous description is the entire basis of him calling everything left and right side; the entire explanation to everything and grokking his solution. At 2:42 he erroneously misuses the term halves again! 🤦‍♂
      So in your example he defines your middle number as "not being in the left side" because it's a part of the numbers that were to the left of the split/pivot position and to him the arbitrary so-called "LEFT FREAKING SIDE" just means the side on the left that begins at a split (and higher), even if left of the middle contains numbers from the other side of the split (like the 0, and 1 in your example). So for reasons that make little sense to me the number 2 is not "in the left side" (because he's not talking left of middle) and he becomes inclined to use (binary search) the right side... unless oops your target is less than the middle. It just feels like a bizarre and broken way of thinking about it all... I'll have to consider it further.

  • @seungminlee4971
    @seungminlee4971 2 года назад

    thank you finally understood

  • @Charmask_creation
    @Charmask_creation Месяц назад

    class Solution {
    public:
    int search(vector& nums, int target) {
    int size = nums.size();
    if (size == 0) {
    return -1;
    }
    // Handle the case where size == 1
    if (size == 1) {
    return (nums[0] == target) ? 0 : -1;
    }
    int res = -1;
    int l = 0;
    int r = size - 1;
    while (l nums[l] && nums[mid] > nums[r]) {
    if (target >= nums[l] && target < nums[mid]) {
    r = mid - 1;
    } else {
    l = mid + 1;
    }
    } else {
    // Prevent r from becoming negative
    if (r > 0) {
    r = r - 1;
    } else {
    break; // Exit if r is already 0
    }
    }
    }
    return res;
    }
    };

  • @khemrajghalley3206
    @khemrajghalley3206 Год назад

    well explained