Roman to Integer - Leetcode 13 - Python

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


0:00 - Read the problem
1:40 - Drawing Explanation
5:34 - Coding Explanation

leetcode 13

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

I love how the negative numbers is applied here(when small numbers are being put before the largers), it is very inspiring

zexisun
Автор

Great solution, NeetCode. Your videos help a lot.

Here is another solution: trick is to always add roman values,

but if previous roman value is smaller than next one,
subtract it twice. (because we already added it on the previous step). here is the code:

roman = {}

roman["I"] = 1
roman["V"] = 5
roman["X"] = 10
roman["L"] = 50
roman["C"] = 100
roman["D"] = 500
roman["M"] = 1000

int_sum = roman[s[0]]
for i in range(1, len(s)):
if roman[s[i-1]] < roman[s[i]]:
int_sum -= 2*roman[s[i-1]]
int_sum += roman[s[i]]

for example, "XC" which is 90 will be computed as
step 1: int_sum = roman[s[0]] (10)
step 2: since roman[s[1]] > roman[s[0]], int_sum = -2*roman[s[0]] + int_sum + roman[s[1]] (- 2*10 + 10 + 100)

Phi_AI
Автор

Nobody highlights this so clearly that there can only be 1 symbol on the left for substraction case, you just made it clear !

abhishekr
Автор

Did this yesterday. My solution was too complex, but faster. This is way easier to grasp.

abuchikingsley
Автор

You're amazing, thank you for the lucid explanation, breaking down how to reach the problem. 😊

riyatiwari
Автор

Bro I just wanted to take a look how others solved this and Im so happy my solution its close to yours, literally one/two lines differently. Happy to see that I was able to come with a simple solution like you, good minds think alike 😁

xReisk
Автор

I never thought I would be able to understand how this problem worked, thanks so much.

DarklordMcsweetTeaSipper
Автор

You really know how to clear the confusion. THANK YOU!

zerosandones
Автор

I watch this channel while eating. Keep it going mate :)

jideabdqudus
Автор

this is the best explanation of this problem. clean and simple.

cometomatt
Автор

If you keep a prev variable you can simply add the value of the numeral each iteration and substract previous twice if it is smaller than the current numeral

alf
Автор

hey, i understand this concept but what about the very last number in the index? how does the code account for that since there's no subsequent number to compare it with? been stuck in leetcode for a while

alifeoflaurels
Автор

I understood the logic of the Roman numerals but didn't really understand the execution. Thanks for the vid.

adityavardhanjain
Автор

to understand better you have to understand the outputs of each instruction, check the outputs of
str="XVIII"
for i in range(len(str)):
#print(i) #print first
#print(str[i]) #2nd

str = "XVIII"
for i in range(len(str)):
print(f"i: {i}, c: {str[i]}")


and you will get it sooner, basically is the explanation of comparing 2 chars and compare the value with the map and check that is >1 letter, cant be 0 that's why the line 11

koearnn
Автор

I am not getting output for the folllowing string i.e
MCMXCIV
If i use your method then the answer is 1990
But the answer is 1994

Please reply

ShaikSharmila-tqbc
Автор

this is more intuitive i feel
class Solution:
def romanToInt(self, s: str) -> int:
romans = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
prev, running_total = 0, 0
for i in range(len(s)-1, -1, -1):
curr = romans[s[i]]
if prev > curr:
running_total -= curr
else:
running_total += curr
prev = curr
return running_total

geekydanish
Автор

Lol, I did it kinda the same. Basically I had a dictionary but then i converted the string given to a list and then created a new list of the values of that given string so instead of XIII i had [10 1 1 1] and then did the addition for this particular list :) proud i made it by myself

sepia
Автор

I had a similar solution to yours but I iterated the loop backwards. Here's my solution:
class Solution:
def romanToInt(self, s: str) -> int:
roman = s.strip("")
convert = {"I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000}
num = convert[roman[len(roman) - 1]]
for letter in range(len(roman) - 2, -1, -1):
if convert[roman[letter]] < convert[roman[letter + 1]]:
num -=convert[roman[letter]]
else:
num += convert[roman[letter]]

return num

chaitanyakhot
Автор

You are awesome👍👏😊 please continue like this, this was really helpful for us

suraj-ejoq
Автор

As a beginner, this is what I tried. I am wondering, is there a way to do it my way? With indexes in if statements?
Roman = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
I = str(1)
V = str(5)
X = str(10)
L = str(50)
C = str(100)
D = str(500)
M = str(1000)

if D[ ] < M[ ]:
D = str(-500)
else:
D = str(500)

print(M, D)
print(D, M)

atlasatlantis