Sqrt(x) - Leetcode 69 - Java

preview_player
Показать описание
Sqrt(x) - Leetcode 69 - Java

--------------------------------ABOUT--------------------------------
🧑🏻 My name is David and I am a software engineer at Meta. My passion is teaching software devs how to pass the grueling technical interviews to help them land their 6-figure dream tech job.

I have received 3 six-figure offers from Google, Meta, and Amazon.

🔬I provide content that will allow you to understand the thought process, pseudocode, time complexity, and code when approaching coding problems.

--------------------------------SOCIAL--------------------------------

💬 If you have any topic or questions you want me to cover, let me know in the comment section below ~ ฅʕ•ᴥ•`ʔ ฅʕ•ᴥ•`ʔ ฅʕ•ᴥ•`ʔ

#technicalinterview #techcareer #softwareengineer
Рекомендации по теме
Комментарии
Автор

class Solution {
public int mySqrt(int x) {
long left = 0, right = (x/2) + 1;

while(left < right) {
long mid = left + (right - left)/2 + 1;
long square = mid * mid;

if( square == x ) {
return (int) mid;
}
else if ( square < x) {
left = mid;
}
else {
right = mid-1;
}
}
return (int) left;
}
}

rryann
Автор

You can avoid the use of a Long by checking if the middle value is greater than (x / middle) instead of checking middle * middle.
(middle * middle) > x is equivalent to
middle > (x / middle)

BBand
Автор

Hi, just an advise,

1. It would be great if you will use bigger font size especially in code editor,

the benefit of large font size is, it will be legible even in low video quality, as compared to current font size, you have to watch in at least 720p, not everyone has good internet connection :)

2. Once you have read the question from browser, you can minimise it, and make code editor full screen, you can go back and forth to the question, if you need to explain something, it's just sitting there occupying space

SanjeevYadavIT
Автор

why do we include left = mid and not left = mid +1 ? while right = mid -1 and not right = mid

heartdokidoki