Search in rotated sorted array - Leetcode 33 - Python

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🐦 Twitter: / neetcode1
    🥷 Discord: / discord
    🐮 Support the channel: / neetcode
    Coding Solutions: • Coding Interview Solut...
    Solve it yourself: neetcode.io/problems/find-tar...
    0:00 - Conceptual
    8:55 - Coding solution
    #Coding #Programming #CodingInterview
    Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
  • Věda a technologie

Komentáře • 289

  • @NeetCode
    @NeetCode  Před 3 lety +20

    🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    Search in Rotated Sorted Array II - czcams.com/video/oUnF7o88_Xc/video.html

  • @expansivegymnast1020
    @expansivegymnast1020 Před rokem +177

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

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

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

  • @wonderfulsnow
    @wonderfulsnow Před 3 lety +257

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

    • @erikm9768
      @erikm9768 Před 2 lety +31

      This is a really good idea, less confusing

    • @aadil4236
      @aadil4236 Před 2 lety +7

      How?

    • @wonderfulsnow
      @wonderfulsnow Před 2 lety +104

      @@aadil4236 your array looks something like
      7 8 9 1 2 3 4 5 6
      Save the first element for reference: a0 = 7.
      Take the middle element of the array (2). If it’s smaller than a0, then your pivot element is to the left. Else, it’s to the right. Repeat, and you will find the index of your pivot element in O(log n) time.
      Then, if the value you’re searching for is larger than a0, use binary search in the left part of the list (from 0 to pivot index). Otherwise, the right side (from pivot index to end).

    • @import_numpy_as_pd2330
      @import_numpy_as_pd2330 Před 2 lety +33

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

    • @dk20can86
      @dk20can86 Před 2 lety +19

      @@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.

  • @symbol767
    @symbol767 Před 2 lety +27

    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 Před 5 měsíci +1

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

  • @kevindebruyne17
    @kevindebruyne17 Před 2 lety +109

    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!

  • @danielgareev8877
    @danielgareev8877 Před 2 lety +148

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

  • @ThePacemaker45
    @ThePacemaker45 Před rokem +16

    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 😅

  • @legendarygaming7790
    @legendarygaming7790 Před 3 lety +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.

  • @tanvirahmed7993
    @tanvirahmed7993 Před 4 měsíci +1

    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.

  • @krypton7792
    @krypton7792 Před 2 lety +3

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

  • @user-wf3bp5zu3u
    @user-wf3bp5zu3u Před rokem +5

    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 Před rokem +5

      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

  • @iamsmitthakkar
    @iamsmitthakkar Před rokem +7

    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 Před rokem

      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 Před 7 měsíci

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

  • @vuminhnhat4061
    @vuminhnhat4061 Před 2 lety +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.

  • @free-palestine000
    @free-palestine000 Před rokem +4

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

  • @yoonsw12
    @yoonsw12 Před rokem

    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.

  • @algorithmimplementer415
    @algorithmimplementer415 Před 2 lety +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!

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

    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 Před 10 měsíci +1

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

  • @tanaykamath1415
    @tanaykamath1415 Před rokem

    Man idk how you manage to explain so elegantly!

  • @priyavardhanpatel6155

    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!!!

  • @positive.stories
    @positive.stories Před 3 lety +4

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

  • @sidazhong2019
    @sidazhong2019 Před 2 lety +15

    if nums[l]

  • @TheKapei22
    @TheKapei22 Před 7 měsíci

    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!

  • @rextlfung
    @rextlfung Před 4 lety +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 Před 2 lety +7

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

    • @carloscarrillo201
      @carloscarrillo201 Před 2 lety +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 Před 2 lety +9

    Amazing explanation, will donate when I get the job.

  • @jameszhou20850
    @jameszhou20850 Před 3 lety +2

    Thanks. Your explanation is very clean

  • @ritikajagtiani2984
    @ritikajagtiani2984 Před 2 lety +2

    Thanks - this is crystal clear!

  • @shashikantdivekar7839
    @shashikantdivekar7839 Před 2 lety +3

    Thank you Sir. Very nicely explained and useful.

  • @gregp83
    @gregp83 Před rokem +1

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

  • @Andrew-dd2vf
    @Andrew-dd2vf Před 2 měsíci

    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!

  • @azamatik3
    @azamatik3 Před rokem

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

  • @ahmedzedan1772
    @ahmedzedan1772 Před rokem

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

  • @finleyy
    @finleyy Před 2 lety

    you have the best explanation! thank you

  • @hackytech7494
    @hackytech7494 Před 3 lety +3

    Fantastic as always ❤️

  • @akhma102
    @akhma102 Před rokem

    Great Explanation. Thank you!

  • @zenyujin_
    @zenyujin_ Před rokem +2

    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

  • @bismeetsingh352
    @bismeetsingh352 Před 3 lety +2

    This is a dope explanation.

  • @legspinismylife
    @legspinismylife Před 2 lety +18

    array problems make me feel like i have IQ below 80

  • @amardeepsingh7528
    @amardeepsingh7528 Před rokem

    The explanation with graph is really helpful

  • @marinbeslo7841
    @marinbeslo7841 Před 2 lety

    Great explanation, thanks! :D

  • @gokulnaathb2627
    @gokulnaathb2627 Před 2 lety +2

    Very nicely explained

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

    Best explanation!!!

  • @james3742
    @james3742 Před 2 lety

    Great vid, thank you!

  • @SankalpPrakashECE--
    @SankalpPrakashECE-- Před rokem

    Beautifully explained

  • @karthikr7956
    @karthikr7956 Před rokem

    Thanks. A very good explanation.

  • @mohamedantar1249
    @mohamedantar1249 Před měsícem

    thanks, you always have the best explanation

  • @今天比昨天厲害
    @今天比昨天厲害 Před 3 lety +1

    You are amazing!!

  • @trungthucnguyen7540
    @trungthucnguyen7540 Před rokem

    great solution! Thank you

  • @yinglll7411
    @yinglll7411 Před 2 lety +1

    Thank you so much!

  • @ronifintech9434
    @ronifintech9434 Před 2 lety +28

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

  • @ThomasCrosbie-Walsh
    @ThomasCrosbie-Walsh Před 20 dny +1

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

  • @TechnoSan09
    @TechnoSan09 Před 4 měsíci +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)

  • @kafychannel
    @kafychannel Před rokem +1

    great work thanks so much!

  • @OluwatosinOseni260
    @OluwatosinOseni260 Před měsícem

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

  • @zr60
    @zr60 Před 2 lety +4

    target < nums[l] and target > nums[r] make it hard to visualize. target >= nums[pivot] and target

  • @sergeychepurnov1328
    @sergeychepurnov1328 Před 2 lety +2

    Awesome explanation, thanks.

  • @jjayguy23
    @jjayguy23 Před rokem

    God bless you man, you are incredible.

  • @seungminlee4971
    @seungminlee4971 Před 2 lety

    thank you finally understood

  • @danielsun716
    @danielsun716 Před rokem

    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

  • @mahbubulhaque3137
    @mahbubulhaque3137 Před 2 lety +1

    Really awesome

  • @thecuriousprogrammer5857

    This was awesome

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

    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)

  • @janvidalal476
    @janvidalal476 Před rokem

    That Visualization Supremacy. 💯

  • @nikhil_a01
    @nikhil_a01 Před rokem +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

  • @andreytamelo1183
    @andreytamelo1183 Před 2 lety

    Thanks!

  • @moncefabboud2839
    @moncefabboud2839 Před 2 měsíci

    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.

  • @user-de4lk6pk8k
    @user-de4lk6pk8k Před 6 měsíci +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

  • @therelaxinggamer3448
    @therelaxinggamer3448 Před 14 dny

    what is that software you used to explain the algorithm ?

  • @nguyentuan1990
    @nguyentuan1990 Před 2 dny

    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

  • @khemrajghalley3206
    @khemrajghalley3206 Před 8 měsíci

    well explained

  • @erassylzh5658
    @erassylzh5658 Před rokem

    awesome!

  • @whitest_kyle
    @whitest_kyle Před 2 lety +2

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

  • @ivanwen8335
    @ivanwen8335 Před 2 lety +1

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

    • @abhicasm9237
      @abhicasm9237 Před 2 lety

      because the left may not be always 0

    • @ivanwen8335
      @ivanwen8335 Před 2 lety

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

  • @amitupadhyay6511
    @amitupadhyay6511 Před 2 lety +5

    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

  • @SOMESHKHANDELIA
    @SOMESHKHANDELIA Před 21 dnem

    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.

  • @PankajKumar-mz6mp
    @PankajKumar-mz6mp Před 2 měsíci

    this was asked to me in the first round of an interview

  • @maamounhajnajeeb209
    @maamounhajnajeeb209 Před rokem

    thanks

  • @aumrudhlalkumartj1948
    @aumrudhlalkumartj1948 Před 2 lety

    Thanks

  • @ax5344
    @ax5344 Před 3 lety +2

    why do we need to use "while left

    • @mastermind5421
      @mastermind5421 Před 3 lety

      says it at 9:20

    • @fishoil5310
      @fishoil5310 Před 3 lety +2

      you use left < right when you want "eager termination", that means after while loop exited, left==right.

  • @kaungkyaw5632
    @kaungkyaw5632 Před rokem

    Hoping that one day I can visualize problems by drawing pictures like this

  • @deepakg9220
    @deepakg9220 Před 9 měsíci +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 Před 9 měsíci

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

  • @michaelmarchese5865
    @michaelmarchese5865 Před 4 měsíci +1

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

  • @emilyhuang2759
    @emilyhuang2759 Před 4 lety +2

    Python is not my strongest language. Is //2 is dividing by 2 and truncating the decimal part? TY

    • @NeetCode
      @NeetCode  Před 4 lety

      Yeah that's exactly correct.

  • @noextrasugar
    @noextrasugar Před 5 dny

    My head hurts🤯How this seemingly easy problem becomes so complex, I hate leet code mannnnnnn. TY for making it easier, I kept failing test cases because I didn't identify I needed to be looking at whether I was at left sorted or right sorted portion ugggghhhhhh

  • @eugbyte1822
    @eugbyte1822 Před 2 lety

    When comparing whether the target element or mid element is in the left half or right half, why must we compare against the left element instead of comparing against the first element>, e.g . `nums[mid] > nums[left]`, but not `nums[mid[ > nums[0]`;

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

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

    • @cesaralcantara1341
      @cesaralcantara1341 Před 3 lety +2

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

    • @motivewave001
      @motivewave001 Před 2 lety

      follow up would be if the array contains duplicate

  • @mostinho7
    @mostinho7 Před rokem

    Good explanation from 6:30
    Todo:- take notes

  • @fullmetalsmash001
    @fullmetalsmash001 Před 6 měsíci

    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.

  • @lbvenkatesh
    @lbvenkatesh Před 2 lety

    I had changed the if conditions this way for better understanding:
    if (nums[s] = nums[s] && target < nums[m]) {
    e = m -1;
    } else {
    s = m +1;
    }
    } else {
    if (target > nums[m] && target

  • @longtruong3059
    @longtruong3059 Před rokem +1

    I wonder why in the coding sol: if nums[m] >= nums[l] then nums[m] is in the the left sorted portion. From my finding, it should be nums[m] >= nums[0]

  • @skyjoe3655
    @skyjoe3655 Před 24 dny

    this is easy to understand but hard to write running code without bugs

  • @freyappari
    @freyappari Před 10 měsíci

    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]

  • @zainio
    @zainio Před 2 lety +2

    Line 12 of your solution: Why is it

    • @Terracraft321
      @Terracraft321 Před rokem

      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.

  • @kimstuart7989
    @kimstuart7989 Před 2 lety +4

    I have a very curious question for anybody who works at a large scale competitive FAANG type company -- what is the expectation here for an interview process with a question like this? I am not asking this as in i am upset or disagree, but moreso to gauge my level of focus and help develop a better gameplan The interview process (to my understanding) is to test your level of understanding on algorithm and design, data structures and implementation. So when you see a problem like this, You can understand every function of an array data structure and the time and space complexity of every function of an array. You can know when to use it over other data structures that can solve the same question and can even come to the conclusion that an array (since we are talking about contiguous memory) is in fact the best data structure to use here. So you spend your time focusing on that data structure. However, that doesn't help you understand the actual methodology and pattern recognition. To me, that seems like something a mathematician can easily come up with that as a result, I would look to hire a strong mathematician with some software design background over someone with a strong computer science background and a good mathematical background. So for the people with much more experience, have gone through FAANG level interviews or even have been part of an interview committee, what is the expectation of a candidate when presented a problem like this during an interview and also what tools helps you gain that understanding of pattern recognition and more or less mathematical proofing and what is the expectation in terms of a FAANG interview as a entry level SWE? Again, let me say I am committed to studying and getting my skill set to where it needs to be. Me asking this question is more or less to put things in perspective and help revamp my study habits.

    • @nikhil_a01
      @nikhil_a01 Před rokem +2

      This is a good question so I'll do my best to answer it. Coding interviews _aren't_ just to test algorithms and data structures. They're designed to test your communication skills, problem-solving, ability to quickly write clear and bug-free code, _and_ your knowledge of algorithms and data structures.
      The reason you get problems like this is because they have a twist to them, so you have to problem solve rather than just apply a basic algorithm. They want to see that you can break down the problem and find a way to approach it.
      There are a number of techniques you can use. You can draw diagrams, walk through examples, simplify the problem by ignoring some constraints, break the problem into sub-problems etc. Polya's book "How to Solve It" was written for mathematicians but you can adapt some of the techniques listed in the first 20-30 pages to use here. There's a short summary at math.berkeley.edu/~gmelvin/polya.pdf. The book "Cracking the Coding Interview" (pg 60 to pg 81) also has a section on technical problem solving that I like. That's more focused on coding interviews obviously.
      In this problem for example, you can make a number of observations. A rotated sorted array has two sorted parts. For example, [4,5,6,7 | 0,1,2] If we could use binary search to find where the boundary is between the two parts, then we could do a basic binary search on each of those sorted parts. We also note that the boundary is just the smallest element in the array. Or we could note that the boundary is the only place where we have a larger element before a smaller element, like [...,7, 0,...]. These observations are like puzzle pieces that you can put together into a complete solution. You could also make other observations which lead you towards the solution that NeetCode did.
      You're going to have to sit down with pen and paper and really think about the problem. Initially you'll be slow but you'll get better with practice. Many people give up if they can't solve a problem in 15-20 minutes so they don't develop their problem solving skills. That's one reason why you see people grinding 500 or 800 LeetCode problems, trying to learn all the "patterns", but still not doing well at interviews. It's far more efficient to learn a small number of patterns and apply them in creative ways, than to grind hundreds of problems. There isn't really a pattern in NeetCode's solution other than knowing binary search well.

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

      @@nikhil_a01 ok i will try pen and paper next time thx

  • @armand6994
    @armand6994 Před rokem

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

  • @indhumathi5846
    @indhumathi5846 Před rokem

    understood

  • @Extremesarova
    @Extremesarova Před 7 měsíci

    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

  • @niteshmanem1997
    @niteshmanem1997 Před rokem +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 Před rokem +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 Před 9 měsíci

      @@nikhil_a01 did you practice something similar before ?

    • @nikhil_a01
      @nikhil_a01 Před 9 měsíci +1

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

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

      @@nikhil_a01 thank you for your answer 👍

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

    This question should be marked Double Hard

  • @Nikhil-Tomar
    @Nikhil-Tomar Před 6 měsíci

    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.

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

    This doesnt seem to be working for non-distinct values