Integer to Roman - Leetcode 12 - Python

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


0:00 - Read the problem
1:40 - Drawing Explanation
6:25 - Coding Explanation

leetcode 12

#amazon #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

Great explanation!
I don't even go through the discuss section if NeetCode's video is available.

aryanyadav
Автор

The division and modulus can be replaced with subtraction:

for sym, val in reversed(symList):
while num >= val:
res += sym
num -= val


This won't perform any different (although there will be 3x as many string concatenations in the worst case (e.g., num=3333) ), but it's conceptually slightly simpler.

gary-williams
Автор

Cool explanation!
Another possible solution:

def intToRoman(self, num: int) -> str:
symList = [...] # (Your symbols list)

# Roman zero does not exist
if num <= 0:
return None
result = ""
for symbol, value in reversed(symList):
while value <= num:
result += symbol
num -= value
return result

andresbonelli
Автор

Thanks for your work! I am trying to develop a habit of watching your video everyday before bed.

mengjiasings
Автор

Best explanations out there. Your videos are so succinct and well explained that I can understand any problem in about ten minutes! Got a Youtube account just so I could subscribe and support the channel as a thank you! So thank you!!

katrinak
Автор

As of Python 3.7 hashmap (dict) preserves insertion order so you don't need to use a list.

parkboy
Автор

Thanks for the great explaination! Just a quick ques: Why did you use list instead if hashmap? I have an idea still wanted some clarification if what I thinking is right.

self-learning
Автор

Best in youtube. To the point, short but complete 👍

arunks
Автор

Your explanations are excellent! After watching many of your videos, one suggestions I would make is better variable naming for clarity. Your variable names tend too be too concise and generic/unclear. In this video for example, "sym, val" would be clearer as "roman, decimal". I see you also use "res" a lot in many of your videos. Sometimes, I have to re-read the code a few times to keep track of what the variables mean. Other than that, awesome, and thank you so much for helping us!

adventurer
Автор

how to handle 400, the output is "CCCC" but expected is "CD". I think To handle the case of 400, we need to add an additional condition to check if the num is equal to the current val. If that condition is true, we can directly append the symbol and subtract the value from num. This will ensure the code correctly handles cases requiring subtractive notation.

dhirojkumarsahu
Автор

Thanks dude. Your simple explanation is superb.

rpgiri
Автор

You explained it like a peice of a cake

travelnlearn
Автор

The only thing I can say is: Thank you, thank you, thank you!!!

jpkeys
Автор

Woah! That was an outstanding explanation. Extremely clear and concise. Thanks a ton!

nehascorpion
Автор

Is it not mandatory to give an (!= 0) in the if statement?

safwan
Автор

like+subscribed! thx for the help! I m learning a lot with your channel bro!

LordKing
Автор

Can we use LinkedHashMap and iterate through it? Complexity to retrieve an element would be reduced

athangkulkarni
Автор

I have a different solución, but tours is good to me too.

ivanleon
Автор

Stop the iteration if num==0:
if num==0:
return res

ISHSACHDEVA-ir
Автор

What about the number 1000, it shows CDCDXCXCXX instead of M which is already in the array

oyeshetty