Best explanations out there. Your videos are so succinct and well explained that I can understand any problem in about ten minutes! Got a TH-cam account just so I could subscribe and support the channel as a thank you! So thank you!!
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.
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.
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.
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!
Here's how I did it if anyone wants an alternate solution: class Solution(object): def intToRoman(self, num): integers=[1000,900,500,400,100,90,50,40,10,9,5,4,1] roman=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'] rom_val=[]
while(num!=0): for i in range(len(integers)): while(num>=integers[i]): rom_val.append(roman[i]) num=num-integers[i] return ''.join(rom_val) or alternatively: class Solution(object): def intToRoman(self, num): integers=[1000,900,500,400,100,90,50,40,10,9,5,4,1] roman=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'] rom_val=[] i=0 while(num!=0): for i in range(len(integers)): count=num//integers[i] rom_val.append(roman[i]*count) num=num%integers[i] return ''.join(rom_val)
I'm pretty sure that line 10 of your code "if num // val:" is unnecessary. In fact, the following few lines don't need to be nested into any conditionals at all; The line is redundant with the following line, since "count" is only going to be altered in cases where "num" is larger than "val" anyway. If you take out the conditional, the code still passes all of LeetCode's tests and accepts the submission as correct. That said, I'm still pretty new to this, so maybe there's another reason here that I'm missing. Would adding that conditional allow the program to run faster, as the processor wouldn't need to run through the following lines for "value" values that aren't going to change the result? LeetCode doesn't give noticeably different CPU usages or speeds for the two different versions of the code, but I can imagine that maybe this is something that would only be an issue in longer and more complex software.
Remember that we iterate through the table in reverse order. 8 would first be divided by 5, which would be divisible once (integer division), so count would be 1 for 5 and we would add 'V' and then mod by 5 to get 3, which would then be divided by 1, producing a count of 3 and adding 'I' 3 times
Great explanation!
I don't even go through the discuss section if NeetCode's video is available.
Thanks for your work! I am trying to develop a habit of watching your video everyday before bed.
after bed will give you better result
Best explanations out there. Your videos are so succinct and well explained that I can understand any problem in about ten minutes! Got a TH-cam account just so I could subscribe and support the channel as a thank you! So thank you!!
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.
Cool explanation!
Another possible solution:
def intToRoman(self, num: int) -> str:
symList = [...] # (Your symbols list)
# Roman zero does not exist
if num
it works fine bro
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.
Because you need to iterate in order. A dictionary key-value pair has no order.
@@jpkeys6000they do in python …
Best in youtube. To the point, short but complete 👍
As of Python 3.7 hashmap (dict) preserves insertion order so you don't need to use a list.
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.
It's already in the list of our conditions
like+subscribed! thx for the help! I m learning a lot with your channel bro!
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!
Thanks dude. Your simple explanation is superb.
You explained it like a peice of a cake
Simple and brilliant.
Woah! That was an outstanding explanation. Extremely clear and concise. Thanks a ton!
Thank you a lot, from France !
u made it so easy man
thanks
Stop the iteration if num==0:
if num==0:
return res
The only thing I can say is: Thank you, thank you, thank you!!!
Is it not mandatory to give an (!= 0) in the if statement?
Hermosa solución. Simple y efectiva.
Such a clear explanation!
Can we use LinkedHashMap and iterate through it? Complexity to retrieve an element would be reduced
Yes you can and as of Python 3.7 the hashmap preserves insertion order.
I have a different solución, but tours is good to me too.
Good video sir.
ok how do you run it with no main?
Good Explanation
U a Roman God
Here's how I did it if anyone wants an alternate solution:
class Solution(object):
def intToRoman(self, num):
integers=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
roman=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
rom_val=[]
while(num!=0):
for i in range(len(integers)):
while(num>=integers[i]):
rom_val.append(roman[i])
num=num-integers[i]
return ''.join(rom_val)
or alternatively:
class Solution(object):
def intToRoman(self, num):
integers=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
roman=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
rom_val=[]
i=0
while(num!=0):
for i in range(len(integers)):
count=num//integers[i]
rom_val.append(roman[i]*count)
num=num%integers[i]
return ''.join(rom_val)
What about the number 1000 , it shows CDCDXCXCXX instead of M which is already in the array
1000 / 1000 gives count 1. which then multiplied with M gives you M alone. Then num becomes 0 due to mod operation. loop breaks
I'm pretty sure that line 10 of your code "if num // val:" is unnecessary. In fact, the following few lines don't need to be nested into any conditionals at all; The line is redundant with the following line, since "count" is only going to be altered in cases where "num" is larger than "val" anyway. If you take out the conditional, the code still passes all of LeetCode's tests and accepts the submission as correct.
That said, I'm still pretty new to this, so maybe there's another reason here that I'm missing. Would adding that conditional allow the program to run faster, as the processor wouldn't need to run through the following lines for "value" values that aren't going to change the result? LeetCode doesn't give noticeably different CPU usages or speeds for the two different versions of the code, but I can imagine that maybe this is something that would only be an issue in longer and more complex software.
thanks for your help for beginners like me 😂
thank you for your help😀
absolutely amazing
such an elegant solution
You could have avoided the complexity of reversing the list , simply making the list reverse from the start
for 1994 it's giving wrong answer
No, it gives the right answer i.e "MCMXCIV". However I did in Swift, but the concept is same
Yes, for 1994, it's not working
it works perfectly , just now submitted it with the same logic
Awesome!!
What about the number 8? In roman numerals its VIII, but if I'm not mistaken according to this code it'd be IVIV
Remember that we iterate through the table in reverse order. 8 would first be divided by 5, which would be divisible once (integer division), so count would be 1 for 5 and we would add 'V' and then mod by 5 to get 3, which would then be divided by 1, producing a count of 3 and adding 'I' 3 times
@@OGRfilms oh got it, thank you
thank you so much
how wiukd anyone know that ,he has to make a list for this problem?
From Roman to Integer , it was pretty clear , only some cases are not considered , and those could be added here
This is more math than programming
🎉
Why nest these solutions inside a class? Why not just make the scope the py script?
Because u have to submit this code in leetcode by nesting it inside a class. That's how leetcode works
Can someone please type that list of lists for me, I'll Gpay 20 Rs.
Bruh 😂
Best 👍
Am i the only one in 30s and still struggling with such problems :(
This one was hard
im too dumb to code
LOL
sexjhvhj
I hate python bcz i am C++ coder
Agreed