LeetCode #69: Sqrt(x) | Binary Search

preview_player
Показать описание
0:00 Problem overview
0:35 O(sqrt x) solution
1:10 Binary Search O(log n) solution
2:10 Explanation of O(log n) runtime
3:00 Code walkthrough

#leetcode #programming #coding #algorithms
Рекомендации по теме
Комментарии
Автор

thanks for sharing intution and approach nicely

singhamita
Автор

Keep it up man! You’re really good with these

dnm
Автор

My man, you're by far the best one explaining Leetcode questions. Please keep up the good work, i struggle when i don't find a question in your channel. Please do all 2k+ :)

mohamedelmahy
Автор

Bro your work is so good, pleace concentrate on completing the blind 75 playlist, Which will be more helpful
-Thanks for your work

Sai-epon
Автор

4:03 it is showing run-time error
for 0

vikrant_bhatt
Автор

Is it possible to solve by:

mid = left + right) / 2;
if ((mid * mid <= x) {
left = mid;
} else if (mid * mid >= x) {
right = mid;
left = 0;
}

This yields results like x=9, left = right = 3.00001 but there is no way to decide if the final return is 2 or 3 because it infinitely closes to the actual value but how far left to the exact answer VS right to the exact answer cannot be decided. Is it possible to build a solution based on above logic? Below is the return condition I have:

if (left * left < x && right * right > x && right - left <= 0.01) {
return (int) Math.floor(right);
}

which does not work no matter how small the epsilon value is.

weiwang