Integer to English Words - Leetcode 273 - Python

preview_player
Показать описание


0:00 - Read the problem
1:43 - Drawing Explanation
9:53 - Coding Explanation

leetcode 273

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

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"
}

davidorbang
Автор

Problem's name : 😇
Problem's solution:😈

invalidfusion
Автор

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" .

finemechanic
Автор

u know its gonna be a pain when the solution video is 20+ mins long

sundew_ii
Автор

can someone remind me, what's the meaning of life?

NeetCodeIO
Автор

switch(num) {
case 1: return "One";
case 2: return "Two";
case 3: return "Three";


Harshsahu.
Автор

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.

asagiai
Автор

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

bag_of_pixels
Автор

it took me 3 hours to do it with recursion, the meaning of life is to suffer

AhmedAbdelbaset-zb
Автор

Very nice explanation and it was very helpful

maryannegichohi
Автор

3rd time requesting- please please solve this- '815 bus routes' on leetcode.

Notezl
Автор

Opening line had me thinking... hmm theres gotta be something useful in this problem... and then you cracked me up

abhimanyuambastha
Автор

did it in 2 and a half hours, went the if else bell route

manutebol
Автор

There's a chrome extension called Leetcode Fix that shows the dislikes. If you're into that kind of thing

leeroymlg
Автор

imagine having this problem but for french digit representation

returnreturnnn
Автор

Really love to start my day with 'pain in the ass' daily problem, thank you, NeetCode!

АльбусДамблодр
Автор

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)

qulinxao
Автор

Introduction & Problem Overview:

The problem "Integer to English Words" is tedious but useful for learning problem-solving and breaking down tasks.
The creator emphasizes the importance of making observations and notes that the problem is disliked due to its tedious nature.
They solved the problem in 38 minutes, including missed edge cases, and optimized the solution afterward.
Observations lead to recognizing patterns and repeated work, especially around handling numbers like 20 and beyond.
Breaking Down Numbers:

Numbers 1-19 have unique strings, while numbers starting from 20 use a repeated pattern (e.g., 21, 30, etc.).
Strings for numbers 1-19 and multiples of ten (20, 30, ..., 90) need to be hardcoded.
For numbers beyond 99, the task involves breaking them down into subproblems like 100s and thousands.
Numbers up to the billions are handled by breaking them down three digits at a time (e.g., thousands, millions, billions).
Solution Strategy:

Use a helper function to convert three-digit chunks of the number into a string.
For smaller numbers (below 20), a simple lookup suffices. For larger numbers (20+), both tens and ones need to be handled.
Edge cases like zeros are handled by skipping over them in string conversion.
The solution involves looping through the number, processing three digits at a time, and concatenating results with appropriate postfixes (e.g., "thousand", "million").
Key Edge Cases & Efficiency:

The creator highlights edge cases like numbers with trailing zeros (e.g., 1, 000, 000).
The solution involves avoiding empty strings in the result and handling spacing issues correctly.
Time complexity is constant, but for arbitrarily large integers, it would be O(n), where n is the number of digits.
They conclude by emphasizing that while the problem is tedious, it has valuable problem-solving lessons, and efficiency varied in their submissions.

yashshukla
Автор

The meanint of life is to tolerate what life opposes on us RT

saranshthukral
Автор

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

jonathanrodriquez
welcome to shbcf.ru