Wow! Thank you for the detailed explanation and code example!😍

I implemented:

ROMAN_TO_DECIMAL={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}len_s=len(s)number=0foriinrange(len_s-1):value=ROMAN_TO_DECIMAL[s[i]]ifROMAN_TO_DECIMAL[s[i]]<ROMAN_TO_DECIMAL[s[i+1]]:number-=valueelse:number+=value# last
number+=ROMAN_TO_DECIMAL[s[len_s-1]]returnnumber

I could not come up with the idea of ROMAN_TO_DECIMAL.😭

When thinking about this problem, did you first think about ROMAN_TO_DECIMAL and then the logic?
Or is the logic first?

If it's ok with you, could you tell me about your thought process?

I am sorry to disappoint you but I don't really know how I came up with this or in what order. I have solved this question a while ago.

ROMAN_TO_DECIMAL makes sense because for each roman numeral there is one corresponding number. There is a one to one mapping between them, and dict is python's map.

I think that the bottom line is experience. The more questions you solve on leetcode, see how other people's solutions look like, you develop more intuition as to what are the data-structures and algorithms suitable for each problem.

Also, you might find these books helpful, depends on what your goals are, and how much spare time you have:

I thought that the key solved the problem is awareness of "each roman numeral there is one corresponding number".
I will continue to solve problems and gain experiences, with other people's solutions.

And thanks for introducing books!😍

We're a place where coders share, stay up-to-date and grow their careers.

Wow! Thank you for the detailed explanation and code example!😍

I implemented:

I could not come up with the idea of

`ROMAN_TO_DECIMAL`

.😭When thinking about this problem, did you first think about

`ROMAN_TO_DECIMAL`

and then the logic?Or is the logic first?

If it's ok with you, could you tell me about your thought process?

I am sorry to disappoint you but I don't really know how I came up with this or in what order. I have solved this question a while ago.

`ROMAN_TO_DECIMAL`

makes sense becausefor eachroman numeral there isonecorresponding number. There is a one to onemappingbetween them, and`dict`

is python'smap.I think that the bottom line is experience. The more questions you solve on leetcode, see how other people's solutions look like, you develop more intuition as to what are the data-structures and algorithms suitable for each problem.

Also, you might find these books helpful, depends on what your goals are, and how much spare time you have:

Thank you for your helpful reply.

I thought that the key solved the problem is awareness of "each roman numeral there is one corresponding number".

I will continue to solve problems and gain experiences, with other people's solutions.

And thanks for introducing books!😍