Ugly Number - Leetcode 263 - Python
Ғылым және технология
🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
💡 BINARY SEARCH PLAYLIST: • Binary Search
📚 STACK PLAYLIST: • Stack Problems
Problem Link: leetcode.com/problems/ugly-nu...
0:00 - Read the problem
1:40 - Drawing Explanation
8:44 - Coding Explanation
leetcode 263
This question was identified as a facebook interview question from here: github.com/xizhengszhang/Leet...
#prime #number #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Пікірлер: 30
there are times that i wonder if im being punished for skipping all my math classes during undergrad
@NeetCode
2 жыл бұрын
I feel the same sometimes lol
@user-us7rg4cd6p
2 ай бұрын
There are times I wonder if I am being punished for not studying Computer Science Major but trying to be a software programmer.. lol
I watch entire videos! Because I'm a beginner so the coding section is really helpful! Thank you for the great explanation :)
You just made it quick and easy.. Thanks!!
@NeetCode
2 жыл бұрын
Glad it helped!
you can also add one more condition at while loop that n!= 0 otherwise the loop will be infinite.
i think the worse case time complexity is O(log(2)n) that's if n is only divisible by 2.
My Favorite YT chanel, don't ask me why
@ambujhakhu7531
2 жыл бұрын
why
@playonce4186
2 жыл бұрын
@@ambujhakhu7531 dont worry why he said
@shavvasatya7131
Жыл бұрын
But how
hi - is there a leetcode playlist for this as well?
Thanks
i think u r from mars ur every video is educative
the GOAT uploaded
@BB8_CA
2 жыл бұрын
As aways!
Helpfull
What happens if n is 1. This code seems like it return it as True even though the prime number of 1 is not 2 3 or 5. shouldn't it be if n
@spaceface2288
2 жыл бұрын
if n == 0: return False while n % 2 == 0: n = n / 2 while n % 3 == 0: n = n / 3 while n % 5 == 0: n = n / 5 return n == 1
@sahanar4805
2 жыл бұрын
+1
@harshaggarwal1967
2 жыл бұрын
1 has no prime factors, therefore all of its prime factors are limited to 2,3 and 5..so we can take it as ugly number.. Hence return true for this case..
but the test case says 20 is an ugly number.
20 is not considered an ugly number. An ugly number must only be divisible by 2, 3, or 5, but 20 is also divisible by 4, which is not among the allowed prime factors. -source chatgpt
@user-qg7lb1jx8b
Жыл бұрын
4 is not prime
@ChrisCox-wv7oo
10 ай бұрын
Being divisible by 4 is the same as being divisible by 2 two times... I'm not sure if Chad GPT is going to do mentally cripple an entire generation of engineers, or help them learn quicker through failure
++subscribers
Actually that shows "Time Limit Exceeded" A better solution can be: if (n return false; while (n > 1) { if (n % 2 == 0) n /= 2; else if (n % 3 == 0) n /= 3; else if (n % 5 == 0) n /= 5; else return false; } return n ==1;
@NegiRohit
Жыл бұрын
I came up with this exact solution without seeing this code.