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
Well explained. Would be glad if you can take up the same problem with duplicates.
Thanks! Sure, no problem at all! Is it also on leetcode? What's the name of the problem or it doesn't exist there?
I don't think it is available on LC. This question is a kind of follow up one could expect for the above problem.
@@nikhilm4290 Ok, I see. Sure, I will think about something, thanks!
great explaination!
Thank you!
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?
Sir if you can explain why we return l, instead of r at the very end.
@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
8 минут смотрел, как англичанин говорит по-русски с сильным британским акцентом
хаха, подкол засчитан ;DD