Microsoft Coding Interview Question - Find Minimum in Rotated Sorted Array - Leetcode 153

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

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

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

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @rids437
    @rids437 6 месяцев назад +3

    i love the why how sorts on you tube help me with some idea when i can do all problems by my self, Thank you!! keep posting more .. also if you can do on system design

  • @seasong7655
    @seasong7655 7 месяцев назад +92

    Python programmers be like min()

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

      Real

    • @CoolExcite
      @CoolExcite 7 месяцев назад +23

      In theory that is not the best solution since it would be O(n) while this video's is O(log(n)). In reality there's a decent chance it'd actually be faster (at least without massive array sizes) because library functions are magical sometimes

    • @virno69420
      @virno69420 7 месяцев назад +4

      *std::min_element(nums.begin(), nums.end());

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

      ​@@CoolExcite exactly

  • @mohamedfarid7742
    @mohamedfarid7742 7 месяцев назад +30

    Wouldn't the answer always be arr[n/ 2 + 1] ?

    • @StevenTohme
      @StevenTohme 7 месяцев назад +34

      pivot isnt always in the middle

    • @Masteraffan
      @Masteraffan 7 месяцев назад +5

      Not always depends on how it's rotated

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

      what if it rotates fully and starts the same, then the smallest is arr[0]. Never guaranteed to rotate half way.

  • @axhraf7712
    @axhraf7712 3 месяца назад +7

    hmm this solution seems a lot more simple than Neetcode’s 🤔

  • @pradyumnachakraborty3262
    @pradyumnachakraborty3262 6 месяцев назад +2

    The solution is pretty simple actually. Lets say in the array 4,5,6,1,2,3 you have to search 2. Now, you find the middle value which is 6. Now 6 is greater than 3,however, 2 is less than 3. So, you search in the right side only, ie from 1 to 3. And the next pivot is 2.
    Now what happens if the array is something like
    3,4,5,6,1,2 and you have to find 3?
    Here, 5 is the initial pivot. Now, we see that 2 is less than 5, and 3 is less than 5, however, 3 is greater than 2 as well. So, we check in the left interval.

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

      Glad you've got it figured out! :)

  • @vasylshkrum8007
    @vasylshkrum8007 7 месяцев назад +6

    Why not just iterate till n>n+1? And if halfs rotated, we can start iterating from (n/2)-1. Will it work?

    • @lOmaine777
      @lOmaine777 7 месяцев назад +2

      It doesn't have to be half rotated.

    • @Masteraffan
      @Masteraffan 7 месяцев назад +6

      We also need to take time complexity in account

    • @elhermanodejiren183
      @elhermanodejiren183 7 месяцев назад +4

      That will work, but the time complexity of that is O(n). When this question is asked, it generally requires the solution to be in O(log(n)) time.

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

      You know leetcode would have some gigantic input at the end and it would timeout. Gotta get that bigO down, but conceptually yeah you can just iterate through linearly.

  • @ashishkumarsingh6618
    @ashishkumarsingh6618 7 месяцев назад +5

    What if elemnets in array have duplicates. How the approach will change

    • @LuisDiaz-np8dz
      @LuisDiaz-np8dz 6 месяцев назад

      well if that was the case, the exercise would tell you. In this time all the arrays in the 67 test don't have any duplicate

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

      If not mentioned in the question that it doesn't have any duplicates then use set data structure

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

    If it’s just rotated at one point, why can’t we do this:
    First, check if nums[0] < nums[-1] then return nums[0] as solution and end, else;
    keep checking until nums[i] > nums[i+1] n whenever this condition is met the solution is nums[i+1].

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

      Yes I think that's right :)

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

    Is this guy the programmer from Ex Machina?

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

      He is not lol

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

    You took a sorted array in which both the sides of mid are sorted .what if they aren't?

    • @GregHogg
      @GregHogg  6 месяцев назад +2

      They will be, because the original array was sorted and shifted :)

  • @tylerp.8352
    @tylerp.8352 7 месяцев назад +1

    I don’t understand why wouldn’t you just iterate thru the array and return lowest value?

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

      Binary search has better time complexity O(log n) as compared to iterating the array and returning the minimum O(n).

    • @tylerp.8352
      @tylerp.8352 7 месяцев назад +1

      @@lakshyakwatra3430 interesting, thanks!

    • @CodeWithGajanan
      @CodeWithGajanan 6 месяцев назад +2

      It is okay if we have a small array, But binary search solution really helps if we have millions of values in array

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

    I can't think of any real world applications for this?

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

      No? Job interview, i have been asked for that every time

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

      @@Niceguy54444as a programmer for 10+ years, I have never needed to do anything like this in real world programming. I think many people in the business would say the same. This sort of problem is only really relevant in these sorts of predefined puzzle questions.

  • @AyanSohail-oq7vk
    @AyanSohail-oq7vk Месяц назад

    Binary search be like i am coming

  • @oreokapoor577
    @oreokapoor577 7 месяцев назад +2

    Hey greg sir i m first year btech undergrade plz can u tell me what to skills to learn except the dsa and web dev plzz🎉

    • @CoolExcite
      @CoolExcite 7 месяцев назад +3

      In my experience, Cloud Computing was the one skill that wasn't part of the curriculum that would've been really useful to have while job hunting

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

      English (not joking even)

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

      Systems Design, API Design, Object Oriented design, DB modeling

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

    Am i dumb, you have to find the minimum right? If i find the starting point where the array got rotated i don't need to do binary search then. And Binary search is only possible in a sorted array, so i can't improve finding the pivot point 😂🤔

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

    Damn

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

    sort((a,b)=>a-b)[0]