Leetcode 35. Search Insert Position [Java] | Binary search explained

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • This is the leetcode Search Insert Position problem explanation in Java. This question has been asked by companies such as Amazon, Google, Microsoft and Facebook during the coding interviews.
    Question URL: leetcode.com/p...
    Please write in the comments below which leetcode problem you want me to solve next.
    And remember, a leetcode a day keeps unemployment away!
    Thanks for watching!
    #leetcode35

ความคิดเห็น • 11

  • @nikhilm4290
    @nikhilm4290 3 ปีที่แล้ว +2

    Well explained. Would be glad if you can take up the same problem with duplicates.

    • @ifelsestatement7803
      @ifelsestatement7803  3 ปีที่แล้ว

      Thanks! Sure, no problem at all! Is it also on leetcode? What's the name of the problem or it doesn't exist there?

    • @nikhilm4290
      @nikhilm4290 3 ปีที่แล้ว

      I don't think it is available on LC. This question is a kind of follow up one could expect for the above problem.

    • @ifelsestatement7803
      @ifelsestatement7803  3 ปีที่แล้ว

      @@nikhilm4290 Ok, I see. Sure, I will think about something, thanks!

  • @ginasomara267
    @ginasomara267 2 ปีที่แล้ว

    great explaination!

  • @michaelcosta7235
    @michaelcosta7235 ปีที่แล้ว

    So uh, I came here to find the big brain method to solve this thinking my solution would be at the bottom of the list, but I got 100% with a simple if statement check and for loop. I am not sure I did it right, lol.
    Here is my logic:
    I check if the target number is less than the element at index 0, if yes, return 0;
    I check if the target number is greater than the last element of the index, if yes, I return the array length as that would be highest index number plus one.
    Goes into a for loop if none of those conditions are met, checks each element against the target, if it matches the target return i.
    If during the loop if the condition met is i > 0 and nums[i-1] is less than target and nums[i] > target return i;
    Is this not acceptable?

  • @bradjohnson8116
    @bradjohnson8116 3 ปีที่แล้ว +1

    Sir if you can explain why we return l, instead of r at the very end.

    • @ifelsestatement7803
      @ifelsestatement7803  3 ปีที่แล้ว

      @Brad Johnson, think about this example: [1,3,5,6], 2. If you return r, the return value would be equal to 0 which is wrong. The return value of l would be 1 which is correct. So, only "l" value carries the right result in this code

  • @kirillprosviryakov6206
    @kirillprosviryakov6206 ปีที่แล้ว

    8 минут смотрел, как англичанин говорит по-русски с сильным британским акцентом