class Solution: def numberToWords(self, n: int) -> str: if not n: return 'Zero' o,h,sfx,d=[],'Hundred',['','Thousand','Million','Billion'],{1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen', 14:'Fourteen',15:'Fifteen', 16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Nineteen', 20:'Twenty', 30:'Thirty', 40:'Forty',50:'Fifty', 60:'Sixty',70:'Seventy',80:'Eighty', 90:'Ninety'} def mk(n,p): if n: a,n=divmod(n,1000) mk(a,p+1) if n: a,n=divmod(n,100) if a:o.append(d[a]);o.append(h) if n>19: a,n=divmod(n,10) o.append(d[a*10]) if n:o.append(d[n]) if p:o.append(sfx[p]) mk(n,0) return ' '.join(o)
I'm surprised the problem is so much disliked. I liked it. It's clear and practical, in contrast to all those "count the number of solutions and because nobody cares return it by modulo" .
I encountered this problem in one of my interviews. The thing about this question is It is one of the hardest questions yet one of the most useful. You can literally judge an applicant with this kind of question.
Yep, solved it in about an hour. It took 4 submission attempts and then I posted 2 successful submissions, so it lowers success rate quite a lot From now on I'll first setup sandbox locally, debug everything, search discussion section for test cases and only then submit the code
we should use if condition inside the get_string() to check if the tens in null or not. It will not change the output but will improve code clarity and maintainability.
got this on an Amazon Phone Screen had less than 30 mins to solve with the additional requirement to add the "and" in the corresponding spots... I failed lol
@@michaelroditis1952 giving better runtime than append-reverse one Though can't compare based on leetcode runtime As inserting at pos 0 everytime isn't it 0(1) like append?
class Solution: def numberToWords(self, n: int) -> str: if not n: return 'Zero' o,h,sfx,d=[],'Hundred',['','Thousand','Million','Billion'],{1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen', 14:'Fourteen',15:'Fifteen', 16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Nineteen', 20:'Twenty', 30:'Thirty', 40:'Forty',50:'Fifty', 60:'Sixty',70:'Seventy',80:'Eighty', 90:'Ninety'} def mk(n,p): if n: a,n=divmod(n,1000) mk(a,p+1) if n: a,n=divmod(n,100) if a:o.append(d[a]);o.append(h) if n>19: a,n=divmod(n,10) o.append(d[a*10]) if n:o.append(d[n]) if p:o.append(sfx[p]) mk(n,0) return ' '.join(o)
I solved it with arrays instead and using indexes divided and moded by 1000: below_20 = [ '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' ] tens = [ '', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety' ] thousands = ['', 'Thousand', 'Million', 'Billion'] def num_to_string(n): if n == 0: return '' ans = "" if n >= 100: ans += below_20[n//100] + " Hundred " n = n%100 if n < 20: return ans + below_20[n] if n < 100: return ans + tens[n//10] + " " + below_20[n%10] res = "" for i, s in enumerate(thousands): digit = num % 1000 nn = num_to_string(digit).strip() res = nn + (( " " + s ) if nn else "") + (" " + res.strip() if res else "") num = num // 1000 return res.strip()
For anyone implementing this here are the 2 hashmaps:
ones_map = {
1: "One",
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Nine",
10: "Ten",
11: "Eleven",
12: "Twelve",
13: "Thirteen",
14: "Fourteen",
15: "Fifteen",
16: "Sixteen",
17: "Seventeen",
18: "Eighteen",
19: "Nineteen",
}
tens_map = {
20: "Twenty",
30: "Thirty",
40: "Forty",
50: "Fifty",
60: "Sixty",
70: "Seventy",
80: "Eighty",
90: "Ninety"
}
my hero
Our lord saviour is here
🐐
class Solution:
def numberToWords(self, n: int) -> str:
if not n: return 'Zero'
o,h,sfx,d=[],'Hundred',['','Thousand','Million','Billion'],{1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen',
14:'Fourteen',15:'Fifteen', 16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Nineteen', 20:'Twenty', 30:'Thirty', 40:'Forty',50:'Fifty', 60:'Sixty',70:'Seventy',80:'Eighty', 90:'Ninety'}
def mk(n,p):
if n:
a,n=divmod(n,1000)
mk(a,p+1)
if n:
a,n=divmod(n,100)
if a:o.append(d[a]);o.append(h)
if n>19:
a,n=divmod(n,10)
o.append(d[a*10])
if n:o.append(d[n])
if p:o.append(sfx[p])
mk(n,0)
return ' '.join(o)
u know its gonna be a pain when the solution video is 20+ mins long
Before opening lc daily i get notification of neetcode video and from the length of the video i understand the level of today's question. 😂
@@shubham6523 🤣🤣
going through all the dynamic programming videos, they're all 20-30 mins long
Problem's name : 😇
Problem's solution:😈
can someone remind me, what's the meaning of life?
Maybe solving a hard problem on first try 😢
To Enjoy and have fun 🎉
42
For devs it’s to suffer.
Solve Edge Cases 😅 thanks for the solution
The problem with a million edge cases
I'm surprised the problem is so much disliked. I liked it. It's clear and practical, in contrast to all those "count the number of solutions and because nobody cares return it by modulo" .
I encountered this problem in one of my interviews.
The thing about this question is
It is one of the hardest questions yet one of the most useful.
You can literally judge an applicant with this kind of question.
it took me 3 hours to do it with recursion , the meaning of life is to suffer
1:47 I think I know how he got to know that it could be zero.😂
Dude solved it in 38 mins😧💀☠
3rd time requesting- please please solve this- '815 bus routes' on leetcode.
switch(num) {
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
.....
.....
Opening line had me thinking... hmm theres gotta be something useful in this problem... and then you cracked me up
Yep, solved it in about an hour. It took 4 submission attempts and then I posted 2 successful submissions, so it lowers success rate quite a lot
From now on I'll first setup sandbox locally, debug everything, search discussion section for test cases and only then submit the code
Nice to see your initial thought process & previous codes
we should use if condition inside the get_string() to check if the tens in null or not. It will not change the output but will improve code clarity and maintainability.
Really love to start my day with 'pain in the ass' daily problem, thank you, NeetCode!
Very nice explanation and it was very helpful
Easy but tedious problem.
There's a chrome extension called Leetcode Fix that shows the dislikes. If you're into that kind of thing
did it in 2 and a half hours, went the if else bell route
imagine having this problem but for french digit representation
if i see this , i am walking out of the interview room.
Jokes aside, thanks for the explanations.
im gonna cry
Using arrays instead of hash maps is way less tedious to code up.
The meanint of life is to tolerate what life opposes on us RT
got this on an Amazon Phone Screen had less than 30 mins to solve with the additional requirement to add the "and" in the corresponding spots... I failed lol
00:08 🤣😅😆
Am I the only one who enjoyed this? Not exactly hard, but a bunch of little parts to figure out and put together.
If it took you 38 minutes, for common plebs like me will be impossible in an interview setting...
Imagine this being the first hard problem you solve🙂
And yes i m this unlucky soul.....
res.insert(0,s+postfix[i])
can use this instead of reverse
Yes but it's slower
@@michaelroditis1952 giving better runtime than append-reverse one
Though can't compare based on leetcode runtime
As inserting at pos 0 everytime isn't it 0(1) like append?
@@sankhadip_roy Inserting at position 0 is the worst because it must move n elements. Better to use deque
@@michaelroditis1952 understood
@@michaelroditis1952 Understood
opening line im dying lol
Anyone - What do you want to be when you grow up?
Me - Not a Programmer 😪
class Solution:
def numberToWords(self, n: int) -> str:
if not n: return 'Zero'
o,h,sfx,d=[],'Hundred',['','Thousand','Million','Billion'],{1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen',
14:'Fourteen',15:'Fifteen', 16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Nineteen', 20:'Twenty', 30:'Thirty', 40:'Forty',50:'Fifty', 60:'Sixty',70:'Seventy',80:'Eighty', 90:'Ninety'}
def mk(n,p):
if n:
a,n=divmod(n,1000)
mk(a,p+1)
if n:
a,n=divmod(n,100)
if a:o.append(d[a]);o.append(h)
if n>19:
a,n=divmod(n,10)
o.append(d[a*10])
if n:o.append(d[n])
if p:o.append(sfx[p])
mk(n,0)
return ' '.join(o)
For the real DSA heads who do this shit for fun 😢😂
tedious indeed
thank you for reminding me to put dislike after solving this problem 👍
Only stupid people from HR will ask this problem in Interview.
spain
i wish u can hear me clappingg !
How is this a hard problem? There is not even a proper algorithm here. literally a bruteforce solution is the correct one!
Nah tbh i enjoyed this problem
i too wanted to dislike just bcoz of i am not good at spellings i dont 11-19 and some more
you care about me?
i care
I solved it with arrays instead and using indexes divided and moded by 1000:
below_20 = [
'', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'
]
tens = [
'', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'
]
thousands = ['', 'Thousand', 'Million', 'Billion']
def num_to_string(n):
if n == 0:
return ''
ans = ""
if n >= 100:
ans += below_20[n//100] + " Hundred "
n = n%100
if n < 20:
return ans + below_20[n]
if n < 100:
return ans + tens[n//10] + " " + below_20[n%10]
res = ""
for i, s in enumerate(thousands):
digit = num % 1000
nn = num_to_string(digit).strip()
res = nn + (( " " + s ) if nn else "") + (" " + res.strip() if res else "")
num = num // 1000
return res.strip()