Roman to Integer - Leetcode 13 - Arrays & Strings (Python)

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


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

WOW, you made it look very easy, I love your teaching method!

harshgamer
Автор

I think the key learning here for me was that only ever 1 character to the left of a larger value can be a "Subtract" value. E.g. you can never get IIV or XXL
I was in a loop trying to figure out how to see if I need to check next 1 or 2 characters, when it can only ever be the next one. Great video, helped a lot!

kishc
Автор

This would take me some time to figure out. Then you realise you need to do about 150-200 problems to be comfortable at some level in level...phew....good luck everyone

vishwakjayakanthan
Автор

Great Video! but if a map is needed, why is this problem on 'array & Strings' Section?

shyguy
Автор

This code is bit more complicated than it needs to be. If you have a subtraction as in the number "XL", when you get to 'X', check that it's less than L, and subtract 10. Continue your algorithm with the next character 'L' and add 50 for that one.
In my code, I initialize SUMM with the value of the last character since we know it will never be subtracted. Then you can iterate through s[:-1], or better yet use zip(s, s[1:]) to check each character against its neighbor and you don't have to worry about keeping track of an index at all.

dm_
Автор

Cool! this is very explanatory and you made look easy thanks!. Only one feedback It will be good to use a better variable names instead of d, n, i. This just to help new people.

CesBear
Автор

i think using stacks is more intuitive, because when we manually calculate by hand we build up stacks until we hit a element whose value is less than the stack[-1]

jmgpqcx
Автор

Is this really an easy question? It takes time for beginners like me to notice that

viswanathvuppala
Автор

Same logic, but different implementation:
"
def romanToInt(self, s: str) -> int:
symbol_table = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
converted = 0
n = len(s)
for index in range(0, n):
if index<n-1 and
converted -= symbol_table[s[index]]
else:
converted += symbol_table[s[index]]
return converted
"

ayushman-tech
visit shbcf.ru