Тёмный

Valid Perfect Square | LeetCode 367 | C++, Java, Python | May LeetCoding Day 9 | Binary Search 

Knowledge Center
Подписаться 57 тыс.
Просмотров 3,6 тыс.
50% 1

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 32   
@mohammadyahya78
@mohammadyahya78 2 года назад
#1 leetcode teacher on youtube so far. Thank you.
@KnowledgeCenter
@KnowledgeCenter 2 года назад
Glad to hear. Thanks.
@yoreneu1294
@yoreneu1294 4 года назад
Your explanation is really good and some RU-vidrs' code cant pass the judgment but magically their code passed...
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Thanks.
@adarshmv261
@adarshmv261 4 года назад
Super simple explanation and great effort from you sir.... keep this going..
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Thanks a lot.
@satyamgupta6030
@satyamgupta6030 Год назад
sir thank you very much for such a wonderful explaination and thanks again for sharing that awesome 40% test cases trick with us.
@charismatic1516
@charismatic1516 4 года назад
@8: 33 Can you explain where we can use while (lo
@KnowledgeCenter
@KnowledgeCenter 4 года назад
When we update hi = mid or lo = mid, they will never cross each other. They will ultimately become equal, i.e., hi = lo. And stop. So, in this case while(lo < hi) can be used. If you are updating like: lo = mid + 1, and hi = mid - 1, then lo & hi will cross each other, and you can use while(lo
@charismatic1516
@charismatic1516 4 года назад
​@@KnowledgeCenter Thanks! This is great explanation! I saw LC 69 solution and it said return (lo-1) and did not fully understand its significance until you pointed out: hi = lo-1 (the first instance they cross) in both of your replies. Also, when you are checking in duplicates (get range of a repeating elem), then eventually, a[lo] == a[mid] (or, a[mid] == a[hi[), so we have to use while (lo
@vrindakakkar8752
@vrindakakkar8752 4 года назад
Thank you sir for great explanation ! Sir, as only square can overflow, why should we write long long for mid as well ? Mid shall always lie within the range so, writing int for mid should work, But is showing run time error for: bool isPerfectSquare(int num) { int last_dig = num % 10; if(last_dig == 2 || last_dig == 3 || last_dig == 7 || last_dig == 8) return false; int left = 1; int right = num; while(left num) right = mid-1; if(sq < num) left = mid + 1; } return false; }
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Your code has 2 mistakes: (1). never calculate mid as mid = (left + right)/2; // As left + right can be more than INT_MAX. Division is performed after adding (left+right). (2.) If you multiply 2 ints or take square of a int and assign it to a long long, first it will be calculated as int only then moved to long long. Example: C++: ------- int num = 10; long long x = num * num; * Assembly: ------------------- mov DWORD PTR [rbp-4], 10 mov eax, DWORD PTR [rbp-4] imul eax, eax mov QWORD PTR [rbp-16], rax Here when I write int num = 10; Move the 32-bit integer representation of 10 into the 4 bytes starting at the address in rbp. (DWORD PTR represents 4 bytes) Then this value is moved to 'eax'. eax is holding 4 bytes representing 10. Then you see -- imul eax, eax It means multiply contents of eax with eax ... So, eax was holding 10. Now eax is holding 10*10 i.e., 100. So, same register is holding the square. Then, the result is assigned to long long x. mov QWORD PTR [rbp-16], rax Here QWORD PTR denotes 8 bytes. So, result is moved to 8 bytes starting at rax. In Short, before assigning square of int to long long variable, first its calculated as int only. Then moved. So, you will get the error. So, make correction to you your code based on both the points I have mentioned.
@vrindakakkar8752
@vrindakakkar8752 4 года назад
@@KnowledgeCenter Okay Sir, thanks a lot !
@TechnicalFeelOut
@TechnicalFeelOut 4 года назад
@@KnowledgeCenter As an alternative we can solve as mid = lo +(hi-lo) /2
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Yes that is my point 1. But, that will not solve the problem completely, thats why there is point 2.(Although if point 2 is followed, in this this case point 1 is not required.). But, still good to stick to it.
@charismatic1516
@charismatic1516 4 года назад
Thanks for a great explanation with diagrams! A similar problem is LC 69 (Squaare root of non-negative num). While we are on the topic of binary search, you can also do LC 668 (kth smallest in multiplication table)? I would like to hear your explanation of count of numbers in a mul table up to a certain target num. Thanks!
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Thanks. Yes prob 69 looks almost exact problem like this one. Even code would be almost identical. Just 1 extra thing would be that sq = mid*mid may not exactly match num. So, at the end of the loop return hi. 668 looks a bit tougher than this problem, but Binary search applies there as well. I'll create a video on that, most likely after May challenge. immediately after that.
@charismatic1516
@charismatic1516 4 года назад
@@KnowledgeCenter Thanks!
@umapathybabu8397
@umapathybabu8397 4 года назад
`class Solution { public boolean isPerfectSquare(int num) { long low =0; long high = num; while(lownum/mid) high=mid-1; else low=mid+1; } return false; } }`
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Please share other approaches which works for you.
@adarshmv261
@adarshmv261 4 года назад
Can this solution be modified for leetcode prob no. 69 - sqrt(x). Or whats the approach for prob 69 sqrt(x)??
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Yes problem 69 looks almost same as this problem.
@kiranyadav4721
@kiranyadav4721 4 года назад
sir i have written exactly the same code as u have written ,still it is showing time limit exceeded after clicking on submit button.
@kiranyadav4721
@kiranyadav4721 4 года назад
bool isPerfectSquare(int num) { int ld=num%10; if(ld==2||ld==3||ld==7||ld==8) return false; int l=1; int r=num; while(l
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Please check your while loop, may be its not getting terminated. Are you setting l = mid instead of lo = mid + 1, or r = mid instead of r = mid - 1, because our while loop check is while(l
@karansachdeva8516
@karansachdeva8516 4 года назад
I think when u are calculating mid: you are subtracting 1 instead of L(lowercase) from right i.e -> long mid = l + (r-1)/2 instead of long mid = l + (r-l)/2 because of which you are getting TLE (time limit exceeded)
@karansachdeva8516
@karansachdeva8516 4 года назад
@@kiranyadav4721 You are mistakenly subtracting ONE (1) while calculating mid, you need to subtract L(lowercase which is left). Write -> long long int mid = l + (r - l)/2. Hope this resolves TLE.
@KnowledgeCenter
@KnowledgeCenter 4 года назад
Nice catch.
@sahilkaushik6856
@sahilkaushik6856 4 года назад
Hello, I am Sahil from India. I am a pre final year student in NSIT , New Delhi , India. These days i am a bit stressed because of stuck at home and now not able to focus on Competitive Programming. I had already solved about 80-90 problems on Leetcode but still unable to many of problems in April Long challenges , many other problems on leetcode and mostly able to solve 1 or 2 problems on codechef or codeforces challenges . The question solved on leetcode by me are mostly medium level and very few of them are hard also. So could you please help me . Will you be my mentor
Далее
Valid Perfect Square - Leetcode 367 - Python
8:44
Просмотров 35 тыс.
Holding Bigger And Bigger Dogs
00:18
Просмотров 19 млн
BS-10. Finding Sqrt of a number using Binary Search
17:11
Design HashSet | LeetCode 705 | C++, Java, Python
28:27
Valid Perfect Square | Leetcode #367
9:00
Просмотров 15 тыс.
Arranging Coins | LeetCode 441 | C++, Java, Python
16:10
Holding Bigger And Bigger Dogs
00:18
Просмотров 19 млн