Тёмный

Happy number | Leetcode 

Techdose
Подписаться 174 тыс.
Просмотров 49 тыс.
50% 1

This is the day-2 problem of the april 30 days coding challenge on leetcode. This is a very interesting problem. I have shown 2 methods for solving this problem. One is by using set and the second is just by using trial and error. CODE LINK will be present below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: techdose.co.in...

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

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 77   
@life_of_anjali
@life_of_anjali 3 года назад
We can solve this using Floyd's cycle detection algorithm too, without using extra space. int next(int n) { int res=0; while(n) { res+=pow(n%10,2); n/=10; } return res; } bool isHappy(int n) { int slow=next(n); int fast=next(next(n)); while(slow!=fast) { slow=next(slow); fast=next(next(fast)); } return(slow==1); }
@ashutoshthite
@ashutoshthite 2 года назад
thanks
@salimzhulkhrni1610
@salimzhulkhrni1610 4 года назад
nice explanation :) what about the case when a number does not reach 1 and does not have a cycle?
@mixupthings
@mixupthings 3 года назад
Simple Python Code: class Solution: def isHappy(self, n: int) -> bool: s=set() while n!=1: n=sum(int(i) ** 2 for i in str(n)) if n in s: return False else: s.add(n) else: return True
@nutankumari9977
@nutankumari9977 3 года назад
we can minimize the time complexity by checking if n!=4 ...I did via this. Time complexity 1ms The unhappy number will result in a cycle of 4, 16, 37, 58, 89, 145, 42, 20, 4, .... class Solution { public boolean isHappy(int n) { int index=0,val=0; while(n>0){ index=n%10; val+=index*index; n=n/10; } if(val==1) return true; else if(val==4) return false; else return isHappy(val); } }. Time Complexity : 1ms
@Star_Bawa9
@Star_Bawa9 2 года назад
Bhai woh last wali approach main 6 hi kyu liya??
@supernova7870
@supernova7870 4 года назад
Sir, Iam unable to find logic for this problem -: "SUM OF MAXIMUM OF ALL SUBARRAYS OF A GIVEN ARRAY" , please tell the approach to solve this.
@techdose4u
@techdose4u 4 года назад
I have posted in community. Follow my video on kadane's algorithm. You will understand it. That's why I did not post Day-3 video. Today I will post Day-4 video.
@supernova7870
@supernova7870 4 года назад
@@techdose4u okk sir
@suryaajha2142
@suryaajha2142 3 года назад
slow fast pointer technique will give better result since it will take O(n) space
@kishan2148
@kishan2148 4 года назад
I am getting confuse that why we are taking counter =6 only
@techdose4u
@techdose4u 4 года назад
That is trial n error. It can vary with test cases. Best method is to use SET. But that was just to improve time complexity. It was just a hunch. If your number is happy then it should not require a lot of operations. Try to solve some using pen n paper. You can also create a code using a random generator as input and take a variable to count how if a number is happy then just return the no of steps done. In that way you can store the Max value and figure out how many steps you need. It will be a simple code. Use SET to verify the Max step count.
@pavanilla4374
@pavanilla4374 4 года назад
I tried counter with 20 it gets accepted
@jasmeenkaur6001
@jasmeenkaur6001 3 года назад
Why u take counter value as 6 ???? We can take any number up to 100 or only 6 we can take
@techdose4u
@techdose4u 3 года назад
You need to take a number which will be an upper bound for all the test cases. I have not explained you the proof but if you think a little then you will get it.
@abhi34776
@abhi34776 2 года назад
WOW! 😮 i found a gem programmer.
@najimali32
@najimali32 4 года назад
Nice explanation. Please upload everyday. Sir i have better approach which is in O(n) time & O(1) space.You can use detect cycle in loop concept.
@techdose4u
@techdose4u 4 года назад
Yea correct. Actually set is also O(1) space because your cycle will be short.
@najimali32
@najimali32 4 года назад
@@techdose4u how do we know cycle will be short ??
@crimsoncad3230
@crimsoncad3230 4 года назад
Floyd hare and tortoise algorithm right ???
@najimali32
@najimali32 4 года назад
@@crimsoncad3230 yes.
@techdose4u
@techdose4u 4 года назад
Just a hunch. I have no mathematical proof. See my 2nd method code. You might get it.
@SR-we1vl
@SR-we1vl 4 года назад
What is the time complexity Mr. @Tech Dose !?
@beinghappy9223
@beinghappy9223 Год назад
Thanks for the amazing explanation.
@MagicalCreationAviCreation
@MagicalCreationAviCreation 4 года назад
sir imagine if my happy number will be find after the 7th step then what sir?
@techdose4u
@techdose4u 4 года назад
You can find out the Max steps needed but writting a function and passing all possible inputs.
@MagicalCreationAviCreation
@MagicalCreationAviCreation 4 года назад
@@techdose4u OK thanks sir U r voice is great sir Yours presentation ,usage of words both are good sir
@anitaroy1347
@anitaroy1347 4 года назад
Sir can u pls make a video ,solving this using hare and tortoise algorithm
@ShivamSharma-uw1uo
@ShivamSharma-uw1uo 4 года назад
i didnt get why the counter is 6, please explain this(counter) approach as well
@techdose4u
@techdose4u 4 года назад
You can find the value of counter. I did it by trial and error but you can see the range and test it to find proper value of counter.
@gokulnaathb2627
@gokulnaathb2627 3 года назад
Great explanation, Sir
@cyruspassi457
@cyruspassi457 4 года назад
Keep up the good work!Thanks.
@techdose4u
@techdose4u 4 года назад
Yea... :)
@shakil-the-coding-monster
@shakil-the-coding-monster Год назад
Excellent explanation!
@zsoltfehervari625
@zsoltfehervari625 4 года назад
What if you have chain longer than 6 elements during reduction?
@techdose4u
@techdose4u 4 года назад
Since all test cases were available, I did trial and error. If they are not available. Then you can just return the length instead of true or false. In that way, you can keep track of length of largest reduction chain encountered. Take +1 length for your loop and that's it. This is not trial and error. I hope you got it.
@Ali-kf6jk
@Ali-kf6jk 4 года назад
gud work dude
@techdose4u
@techdose4u 4 года назад
Thanks :)
@pabloarkadiusz4687
@pabloarkadiusz4687 3 года назад
Why using set and not unordered_map which have O(1) vs O(log N) lookup ?
@riteshpatil1422
@riteshpatil1422 3 месяца назад
6:18 58 and 85 are going to give same results
@PradeepKumar-ue2ct
@PradeepKumar-ue2ct 3 года назад
What is the time complexity of the approach?
@crimsoncad3230
@crimsoncad3230 4 года назад
Leet code doesn't give extra time for Python codes. I tried a similar method, but it gives TLE. While other coding platform allow Python codes to take more time.
@techdose4u
@techdose4u 4 года назад
True. Some platforms do not allow java and python any extra time. So, for competitive programming, C++ is preferable.
@crimsoncad3230
@crimsoncad3230 4 года назад
@@techdose4u I was fortunate in the 1st Problem i.e Single Number I wrote the solution in Python and it did accept it.
@crimsoncad3230
@crimsoncad3230 4 года назад
@@techdose4u When I see HackerRank and HackerEarth have adapted to the changes but LeetCode being the top coding platform in the world is yet to adapt/make changes, this thing amazes me.
@najimali32
@najimali32 4 года назад
@@crimsoncad3230 It means you have to optimized your code. I submitted my code in python.
@techdose4u
@techdose4u 4 года назад
Share your DS and technique bro. He will also get it.
@dhruvagarwal882
@dhruvagarwal882 3 года назад
Clear explanation !
@techdose4u
@techdose4u 3 года назад
Thanks :)
@sahilsaxena8374
@sahilsaxena8374 3 года назад
We can you hashMap instead of set O(1) me solve hojyga
@techdose4u
@techdose4u 3 года назад
👍
@inactive5848
@inactive5848 Год назад
can someone please explain what is the purpose of *myset.end()* in line number 20? i understood that myset.find() is being used to find the value but what is myset.end?
@harshpatel1385
@harshpatel1385 4 года назад
nice explanation
@techdose4u
@techdose4u 4 года назад
Thanks :)
@supernova7870
@supernova7870 4 года назад
Sir can we extract digits from a number without using while loop , is there any other way to do it ??
@techdose4u
@techdose4u 4 года назад
You need to use loop because you don't know the length of any number.
@supernova7870
@supernova7870 4 года назад
@@techdose4u okk sir.
@muktadirkhan6674
@muktadirkhan6674 4 года назад
Yes you can by converting it to a string.
@ashokjat555
@ashokjat555 4 года назад
Please make a video on Partition Problem DP
@techdose4u
@techdose4u 4 года назад
Which problem? I have recently made painters partition problem. Check it out.
@ashokjat555
@ashokjat555 4 года назад
@@techdose4u www.geeksforgeeks.org/partition-problem-dp-18/amp/
@ashokjat555
@ashokjat555 4 года назад
Sir the above problem problem along with the partitoned parts.
@techdose4u
@techdose4u 4 года назад
Sure :)
@musicalworld3277
@musicalworld3277 2 года назад
Why u use counter as 6 only not 10
@preetiipriya
@preetiipriya Год назад
why is counter taken as 6?
@abheetsingh9938
@abheetsingh9938 4 года назад
What is the time complexity for the 1st approach ? and how it can be improved ?
@pabloarkadiusz4687
@pabloarkadiusz4687 3 года назад
It can be improved using hash table instead of red black tree (unordered_map instead of set) Because lookup is O(1) vs O(log N)
@balajiyadakuppam4505
@balajiyadakuppam4505 4 года назад
why counter is set to 6
@payalsagar1808
@payalsagar1808 3 года назад
Great❤️
@techdose4u
@techdose4u 3 года назад
Welcome 😊
@AnonymousAspirantLikeYou
@AnonymousAspirantLikeYou 3 года назад
thank you
@techdose4u
@techdose4u 3 года назад
Welcome
@theoneinyou2233
@theoneinyou2233 Год назад
public static void main(String[] args) { int n = 29,s=0; Set l= new HashSet(); System.out.println(isHappy(n,l,s)); } public static boolean isHappy(int n, Set l, int s) { if(n==1) return true; for(;n>0;n/=10) s+=(n%10)*(n%10); if (!l.add(s)) return false; else return isHappy(s,l,0); }
Далее
Remove K digits | Build lowest number | Leetcode #402
15:30
Вопрос Ребром - Булкин
59:32
Просмотров 998 тыс.
Cursor Is Beating VS Code (...by forking it)
18:00
Просмотров 72 тыс.
Product of array except self | Leetcode #238
15:00
Просмотров 95 тыс.
Group anagrams | Leetcode #49
13:50
Просмотров 83 тыс.
Move zeroes | Leetcode
6:03
Просмотров 44 тыс.
Jump game | Leetcode #55 | Valley peak approach
12:28
Просмотров 189 тыс.