post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/integer-to-roman/discuss/1184992/Python3-simple-and-easy-to-understand-solution-using-dictionary
class Solution: def intToRoman(self, num: int) -> str: d = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'} s = '' for i in d.keys(): s += (num//i)*d[i] num %= i return s
integer-to-roman
Python3 simple and easy to understand solution using dictionary
EklavyaJoshi
6
328
integer to roman
12
0.615
Medium
500
https://leetcode.com/problems/integer-to-roman/discuss/1103010/Clean-and-Correct-Python-Solution
class Solution: def intToRoman(self, num: int) -> str: vals = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] romans = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] res='' for i,v in enumerate(vals): res += (num//v) * romans[i]; num %= v return res
integer-to-roman
Clean & Correct Python Solution
lokeshsenthilkumar
6
623
integer to roman
12
0.615
Medium
501
https://leetcode.com/problems/integer-to-roman/discuss/1793052/Python-3-greater-Simple-solution-with-key-learnings
class Solution: def intToRoman(self, num: int) -> str: sym = { 1 : "I", 4 : "IV", 5 : "V", 9 : "IX", 10 : "X", 40 : "XL", 50 : "L", 90 : "XC", 100 : "C", 400 : "CD", 500 : "D", 900 : "CM", 1000 : "M" } values = sym.keys() result = [] for val in reversed(values): quo, num = divmod(num, val) if quo > 0: temp = [sym[val]] * quo result.extend(temp) return ''.join(result)
integer-to-roman
Python 3 -> Simple solution with key learnings
mybuddy29
5
282
integer to roman
12
0.615
Medium
502
https://leetcode.com/problems/integer-to-roman/discuss/2324305/Simple-greedy-solution-with-dictionary
class Solution: def intToRoman(self, num: int) -> str: values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] numerals = {1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD', 100 : 'C', 90 : 'XC', 50 : 'L', 40 : 'XL', 10 : 'X', 9 : 'IX', 5 : 'V', 4: 'IV', 1: 'I'} res = '' for value in values: while num >= value: num -= value res += numerals[value] return res
integer-to-roman
Simple greedy solution with dictionary
mfarrill
4
250
integer to roman
12
0.615
Medium
503
https://leetcode.com/problems/integer-to-roman/discuss/1801264/Python3-Straightforward-solution
class Solution: def intToRoman(self, num: int) -> str: roman = 'M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I'.split(',') integers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] d=dict(zip(integers, roman )) res='' for n in integers: res += d[n]*(num//n) num = num % n return res
integer-to-roman
[Python3] Straightforward solution
__PiYush__
4
194
integer to roman
12
0.615
Medium
504
https://leetcode.com/problems/integer-to-roman/discuss/1549409/Simple-Python-Solution-40-ms-faster-than-96.41-of-Python3-online-submissions.
class Solution: def intToRoman(self, num: int) -> str: dct = {1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L', 90: 'XC', 100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M'} stack = list(dct.keys()) ans = "" while num > 0: if stack[-1] > num: stack.pop() else: num -= stack[-1] ans += dct[stack[-1]] return ans
integer-to-roman
Simple Python Solution 40 ms, faster than 96.41% of Python3 online submissions.
kunalraut335
4
271
integer to roman
12
0.615
Medium
505
https://leetcode.com/problems/integer-to-roman/discuss/2552032/Python-runtime-O(1)-memory-O(1)
class Solution: def intToRoman(self, num: int) -> str: ans = "" digits = [(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"), (100, "C"), \ (90, "XC"), (50, "L"), (40, "XL"), (10, "X"), (9, "IX"), \ (5, "V"), (4, "IV"), (1, "I")] i = 0 while i < len(digits): decimal, roman = digits[i] if num >= decimal: num -= decimal ans += roman else: i += 1 return ans
integer-to-roman
Python, runtime O(1), memory O(1)
tsai00150
3
339
integer to roman
12
0.615
Medium
506
https://leetcode.com/problems/integer-to-roman/discuss/2290902/Python-oror-Straight-Forward
class Solution: def intToRoman(self, num: int) -> str: rome = { 1: 'I', 5: 'V', 4: 'IV', 10: 'X', 9: 'IX', 50: 'L', 40: 'XL', 100: 'C', 90: 'XC', 500: 'D', 400: 'CD', 1000: 'M', 900: 'CM' } R = '' for i in range(3,-1,-1): conv = num//(10**i) if 0 < conv < 4: for k in range(conv): R += rome[10**i] elif 5 < conv < 9: R+= rome[5*(10**i)] for k in range(conv-5): R += rome[10**i] elif conv in [9, 5, 4] : R += rome[conv*(10**i)] num -= conv*(10**i) return R
integer-to-roman
Python || Straight Forward
morpheusdurden
3
168
integer to roman
12
0.615
Medium
507
https://leetcode.com/problems/integer-to-roman/discuss/1804259/99-Time-93-Memory-easy-to-understand-python-solution
class Solution: def intToRoman(self, num: int) -> str: ans = "" ans += "M"*(num//1000) ans += "D"*(num%1000//500) ans += "C"*(num%500//100) ans += "L"*(num%100//50) ans += "X"*(num%50//10) ans += "V"*(num%10//5) ans += "I"*(num%5) ans = ans.replace("DCCCC","CM").replace("CCCC","CD").replace("LXXXX","XC").replace("XXXX","XL").replace("VIIII","IX").replace("IIII","IV") return(ans)
integer-to-roman
99% Time, 93% Memory, easy to understand python solution
user5901e
3
220
integer to roman
12
0.615
Medium
508
https://leetcode.com/problems/integer-to-roman/discuss/979027/Python-simple-solution
class Solution: def intToRoman(self, num: int) -> str: rules = ( ("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90), ("L", 50), ("XL", 40), ("XXX", 30), ("XX", 20), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1), ) res = "" for suf, val in rules: while num >= val: num -= val res += suf return res
integer-to-roman
Python simple solution
yuliy
3
354
integer to roman
12
0.615
Medium
509
https://leetcode.com/problems/integer-to-roman/discuss/264463/Simple-Solution-with-python3-76ms
class Solution: def intToRoman(self, num: int) -> str: t = '' for i in X: tmp = num // i if tmp > 0: t += X[i] * tmp num -= (tmp * i) return t X = { 1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I', }
integer-to-roman
Simple Solution with python3 76ms
Conight
3
144
integer to roman
12
0.615
Medium
510
https://leetcode.com/problems/integer-to-roman/discuss/2726348/easy-understandble-python-code-with-good-runtime
class Solution: def intToRoman(self, num: int) -> str: values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] res="" for i,v in enumerate(values): res+=(num//v)*numerals[i] print(res) num%=v return res
integer-to-roman
easy understandble python code with good runtime
V_Bhavani_Prasad
2
47
integer to roman
12
0.615
Medium
511
https://leetcode.com/problems/integer-to-roman/discuss/2366605/Python-Elegant-and-Short-or-99.89-faster
class Solution: """ Time: O(1) Memory: O(1) """ ROMAN_TO_INTEGER = { 'M': 1000, 'CM': 900, 'D': 500, 'CD': 400, 'C': 100, 'XC': 90, 'L': 50, 'XL': 40, 'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1, } def intToRoman(self, num: int) -> str: converted = '' for roman, integer in self.ROMAN_TO_INTEGER.items(): whole, num = divmod(num, integer) converted += whole * roman return converted
integer-to-roman
Python Elegant & Short | 99.89% faster
Kyrylo-Ktl
2
95
integer to roman
12
0.615
Medium
512
https://leetcode.com/problems/integer-to-roman/discuss/2238167/Basic-Python-Solution-(53-ms)
class Solution: def intToRoman(self, num: int) -> str: roman ={1 :'I', 4 : 'IV', 5 : 'V', 9 : 'IX', 10 : 'X', 40 : 'XL', 50 : 'L', 90 : 'XC', 100 : 'C', 400 : 'CD', 500 : 'D', 900 : 'CM', 1000 : 'M'} n = num roman_str="" while n > 0 : if n >=1000 : roman_str += roman[1000] n -= 1000 elif n >= 900 : roman_str += roman[900] n -= 900 elif n >= 500 : roman_str += roman[500] n -= 500 elif n >= 400 : roman_str += roman[400] n -= 400 elif n >= 100 : roman_str += roman[100] n -= 100 elif n >= 90 : roman_str += roman[90] n -= 90 elif n >= 50 : roman_str += roman[50] n -= 50 elif n >= 40 : roman_str += roman[40] n -= 40 elif n >= 10 : roman_str += roman[10] n -= 10 elif n >= 9 : roman_str += roman[9] n -= 9 elif n >= 5 : roman_str += roman[5] n -= 5 elif n >= 4: roman_str += roman[4] n -= 4 else : roman_str += roman[1] n -= 1 return roman_str
integer-to-roman
Basic Python Solution (53 ms)
mayank9
2
191
integer to roman
12
0.615
Medium
513
https://leetcode.com/problems/integer-to-roman/discuss/2165509/Python-beats-90-with-full-working-explanation
class Solution: def intToRoman(self, num: int) -> str: # Time: O(n) and Space: O(1) symbolList = [['I', 1], ['IV', 4], ['V', 5], ['IX', 9], ['X', 10], ['XL', 40], ['L', 50], ['XC', 90], ['C', 100], ['CD', 400], ['D', 500], ['CM', 900], ['M', 1000]] res = '' for symbol, val in reversed(symbolList): # we will traverse the list in reverse order if num // val: # num//val = 0 means False, else True count = num // val res += (symbol * count) num = num % val return res # After iterating through all the symbols in the list
integer-to-roman
Python beats 90% with full working explanation
DanishKhanbx
2
244
integer to roman
12
0.615
Medium
514
https://leetcode.com/problems/integer-to-roman/discuss/1949975/Python-simple-solution
class Solution: def intToRoman(self, num: int) -> str: hashmap = {1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L', 90: 'XC', 100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M'} sol = "" while num > 0: for n in reversed(hashmap): if num >= n: num -= n sol+=hashmap[n] break return sol
integer-to-roman
Python simple solution
alessiogatto
2
180
integer to roman
12
0.615
Medium
515
https://leetcode.com/problems/integer-to-roman/discuss/1731380/Python-3-Making-use-of-the-built-in-divmod-function-(56ms-13.8MB)
class Solution: def intToRoman(self, num: int) -> str: mapping = [(1000, 'M'), (900, 'CM'), (500, 'D'), \ (400, 'CD'), (100, 'C'), (90, 'XC'), \ (50, 'L'), (40, 'XL'), (10, 'X'), \ (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')] answer = '' for pair in mapping: value, roman = pair count, num = divmod(num, value) answer += (count * roman) return answer
integer-to-roman
[Python 3] Making use of the built-in `divmod` function (56ms, 13.8MB)
seankala
2
118
integer to roman
12
0.615
Medium
516
https://leetcode.com/problems/integer-to-roman/discuss/1222011/Python3-98-faster-84-memory
class Solution: def intToRoman(self, num: int) -> str: letters = {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M'} i = 1 roman="" while num != 0: digit = num % 10 if digit==1 or digit==5: roman=letters[digit*i]+roman elif digit==2 or digit==3: roman=(letters[1*i] * digit) + roman elif digit==4 or digit==9: roman=letters[1*i]+letters[(digit+1)*i] + roman elif digit==6 or digit==7 or digit==8: roman=letters[5*i]+(letters[1*i] * (digit % 5)) + roman num = num // 10 i*=10 return roman
integer-to-roman
Python3 98% faster 84% memory
nikhilchaudhary0126
2
121
integer to roman
12
0.615
Medium
517
https://leetcode.com/problems/integer-to-roman/discuss/1068027/simple-easy-to-understand-python3-solution
class Solution: def intToRoman(self, num: int) -> str: number = [1000,900,500,400,100,90,50,40,10,9,5,4,1] roman_symbol = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'] roman_string = "" for index, value in enumerate(number): quotient = num // value roman_string = roman_string + roman_symbol[index]*quotient num = num % value return roman_string
integer-to-roman
simple, easy to understand python3 solution
headoftheport1230
2
128
integer to roman
12
0.615
Medium
518
https://leetcode.com/problems/integer-to-roman/discuss/2724560/C%2B%2B-Go-Python-TypeScript-JavaScript-simple-approach
class Solution: def intToRoman(self, n: int) -> str: r = '' while True: print(n) if n - 1000 > -1: r += 'M' n -= 1000 elif n - 900 > -1: r += 'CM' n -= 900 elif n - 500 > -1: r += 'D' n -= 500 elif n - 400 > -1: r += 'CD' n -= 400 elif n - 100 > -1: r += 'C' n -= 100 elif n - 90 > -1: r += 'XC'; n -= 90 elif n - 50 > -1: r += 'L' n -= 50 elif n - 40 > -1: r += 'XL' n -= 40 elif n - 10 > -1: r += 'X' n -= 10 elif n - 9 > -1: r += 'IX' n -= 9 elif n - 5 > -1: r += 'V' n -= 5 elif n - 4 > -1: r += 'IV' n -= 4 elif n - 1 > -1: r += 'I' n -= 1 else: break return r
integer-to-roman
C++ / Go / Python / TypeScript / JavaScript simple approach
nuoxoxo
1
89
integer to roman
12
0.615
Medium
519
https://leetcode.com/problems/integer-to-roman/discuss/2724164/FASTEST-AND-EASIEST-oror-FASTER-THAN-95-SUBMISSIONS-oror-MAPPING
class Solution: def intToRoman(self, num: int) -> str: mp=[(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')] res =[] for val,sym in mp: if val > num: continue res.append((num // val) * sym) num %= val return ''.join(res)
integer-to-roman
FASTEST AND EASIEST || FASTER THAN 95% SUBMISSIONS || MAPPING
Pritz10
1
11
integer to roman
12
0.615
Medium
520
https://leetcode.com/problems/integer-to-roman/discuss/2305037/Python-Effective-Solution
class Solution: def intToRoman(self, num: int) -> str: dic_roman = { 1:['I','V'], 2:['X','L'], 3:['C','D'], 4:['M'] } list1 = [int(i) for i in str(num)] num_lenl = len(list1) res = '' for i in list1: if i < 4: res += i*dic_roman[num_lenl][0] elif i == 4: res += dic_roman[num_lenl][0] + dic_roman[num_lenl][1] elif i == 9: res += dic_roman[num_lenl][0] + dic_roman[num_lenl+1][0] else: res += dic_roman[num_lenl][1] + dic_roman[num_lenl][0]*(i-5) num_lenl -= 1 return res
integer-to-roman
Python Effective Solution
zip_demons
1
99
integer to roman
12
0.615
Medium
521
https://leetcode.com/problems/integer-to-roman/discuss/2208701/Easy-solution-to-understand-using-a-Dictionary
class Solution: def intToRoman(self, num: int) -> str: # Make a dictionry for all possible integers including exceptions dic={1:'I',4:'IV',5:'V',9:'IX',10:'X',40:'XL',50:'L',90:'XC',100:'C',400:'CD',500:'D',900:'CM',1000:'M'} # create a sorted array to store the keys just to iterate in list arr=[1,4,5,9,10,40,50,90,100,400,500,900,1000] n=len(arr) i,st=n-1,"" while i>=0 and num!=0: # moving arr list from last(maximum to minimum) cond=math.floor(num/arr[i]) # checking num is greater or smaller to arr[i] if cond<=0: # if num is smaller to arr[i] then move back to list i=i-1 elif cond>0: # if num is larger to arr[i] then check how many char we need to add in string count=num//arr[i] j=0 st+=(dic[arr[i]]*count) num=num%arr[i] # reducing the num size by doing mod return st
integer-to-roman
Easy solution to understand using a Dictionary
shubham_mishra204
1
168
integer to roman
12
0.615
Medium
522
https://leetcode.com/problems/integer-to-roman/discuss/2180150/Simple-Python-Solution-(Beats-100-python3-submission)-(16-ms)
class Solution: def intToRoman(self, nums: int) -> str: s="" thousands=nums//1000 if(thousands>0): s+="M"*thousands nums-=1000*thousands hundreds=nums//100 if(hundreds>0): if(hundreds==5): s+="D" elif(hundreds==9): s+="CM" elif(hundreds==4): s+="CD" elif(hundreds>5): s+="D" s+="C"*(hundreds-5) else: s+="C"*hundreds nums-=100*hundreds tens=nums//10 if(tens>0): if(tens==5): s+="L" elif(tens==9): s+="XC" elif(tens==4): s+="XL" elif(tens>5): s+="L" s+="X"*(tens-5) else: s+="X"*tens nums-=10*tens ones=nums if(ones>0): if(ones==5): s+="V" elif(ones==9): s+="IX" elif(ones==4): s+="IV" elif(ones>5): s+="V" s+="I"*(ones-5) else: s+="I"*ones return s
integer-to-roman
Simple Python Solution (Beats 100% python3 submission) (16 ms)
ravishk17
1
130
integer to roman
12
0.615
Medium
523
https://leetcode.com/problems/integer-to-roman/discuss/2066622/Simple-solution-Python
class Solution: def intToRoman(self, num: int) -> str: def san(n, x1, x2, x3): #print('san') san = '' if n == 9: san = x1 + x3 elif n == 4: san = x1 + x2 else: if n >= 5: san += x2 n = n - 5 for i in range(n): san += x1 return san ans = '' if (num // 1000) > 0: ans += san(num // 1000, 'M', '', '') num %= 1000 if (num // 100) > 0: ans += san(num // 100, 'C', 'D', 'M') num %= 100 if (num // 10) > 0: ans += san(num // 10, 'X', 'L', 'C') num %= 10 if (num ) > 0: ans += san(num, 'I', 'V', 'X') return ans
integer-to-roman
Simple solution Python
Adaisky
1
179
integer to roman
12
0.615
Medium
524
https://leetcode.com/problems/integer-to-roman/discuss/1947770/Python3-Simple-While-Loops-Solution-(faster-than-94)
class Solution: def intToRoman(self, num: int) -> str: # Using While Loops to Add to a String :) string = "" while num >= 1000: string += "M" num -= 1000 while num >= 900: string += "CM" num -= 900 while num >= 500: string += "D" num -= 500 while num >= 400: string += "CD" num -= 400 while num >= 100: string += "C" num -= 100 while num >= 90: string += "XC" num -= 90 while num >= 50: string += "L" num -= 50 while num >= 40: string += "XL" num -= 40 while num >= 10: string += "X" num -= 10 while num >= 9: string += "IX" num -= 9 while num >= 5: string += "V" num -= 5 while num >= 4: string += "IV" num -= 4 while num >= 1: string += "I" num -= 1 return string
integer-to-roman
[Python3] Simple While Loops Solution (faster than 94%)
Rustizx
1
74
integer to roman
12
0.615
Medium
525
https://leetcode.com/problems/integer-to-roman/discuss/1899198/Python-Simple-solution-explained
class Solution: def intToRoman(self, num: int) -> str: # we'll maintain the usual mapping but # also the "one-less" value mapping. why? # because the way we've written our solution # if we didn't have the mapping the way we do # the output for num=900, would actually be # additive i.e. "DCD" rather than "CM" rom = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] dec = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] i, res = 0, "" # loop until we've exhausted nums while num != 0: # now for every curr value # of num, check if the current # value from the dec array # is smaller, if it is, we # subtract that value till it's # possible # # for e.g. num = 247, we'll # run this loop twice for i=4 (100) # and at the same time we'll add # the corresponding roman literal # to result i.e "CC" and so on and so forth while num >= dec[i]: num -= dec[i] res += rom[i] i += 1 return res
integer-to-roman
[Python] Simple solution explained
buccatini
1
225
integer to roman
12
0.615
Medium
526
https://leetcode.com/problems/integer-to-roman/discuss/1698363/Super-simple-Python-solution-with-runtime%3A-44-ms-faster-than-90.64-submissions
class Solution: def intToRoman(self, num: int) -> str: weights = [ (1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I') ] result = "" for weight, weight_symbol in weights: if num >= weight: qty = (num // weight) result += weight_symbol * qty num -= qty * weight return result
integer-to-roman
Super simple Python solution with runtime: 44 ms, faster than 90.64% submissions
bodanish
1
140
integer to roman
12
0.615
Medium
527
https://leetcode.com/problems/integer-to-roman/discuss/1670455/Simple-and-short-python-solution-(40ms)
class Solution: def intToRoman(self, num: int) -> str: rom_dig = '' pre = {0: '', 1: 'I', 5:'V', 10:'X', 50:'L', 100:'C', 500:'D', 1000:'M'} place = 1 while num: dig = num%10 if (dig+1)%5==0: rom_dig = pre[place]+pre[(dig+1)*place] + rom_dig else: rom_dig = pre[dig//5*5*place] + dig%5*pre[place] + rom_dig num = num//10 place = place*10 return rom_dig
integer-to-roman
Simple and short python solution (40ms)
Charging-to-100
1
107
integer to roman
12
0.615
Medium
528
https://leetcode.com/problems/integer-to-roman/discuss/1662193/Python-Greedy-solution-easy-to-understand
class Solution: def intToRoman(self, num: int) -> str: arr1 = [1,4,5,9,10,40,50,90,100,400,500,900,1000] arr2 = ['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM','M'] n = num i = len(arr1)-1 st = '' while n != 0 and i>=0: if n - arr1[i] >= 0: n = n-arr1[i] st += arr2[i] else: i -= 1 return st
integer-to-roman
Python Greedy solution, easy to understand
myp001
1
265
integer to roman
12
0.615
Medium
529
https://leetcode.com/problems/integer-to-roman/discuss/1411410/Python-Simple-O(1)O(1)-Faster-Than-87
class Solution: def intToRoman(self, num: int) -> str: translator = {1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M', 4: 'IV', 9: 'IX', 40: 'XL', 90: 'XC', 400: 'CD', 900: 'CM'} roman = '' for i in sorted(translator.keys(), reverse=True): if num - i >= 0: quotient, remainder = divmod(num, i) num = remainder roman += translator[i] * quotient return roman
integer-to-roman
Python Simple O(1)/O(1) Faster Than 87%
Medium_Conversation
1
371
integer to roman
12
0.615
Medium
530
https://leetcode.com/problems/integer-to-roman/discuss/1344349/Faster-than-99.64-python-solution
class Solution: def intToRoman(self, num: int) -> str: def romanHelper(upper, middle, lower, factor): if factor == 9: return lower + upper elif factor >= 5: return middle + lower*(factor%5) elif factor == 4: return lower + middle else: return lower*factor roman = "" if num >= 1000: roman += 'M'*(num // 1000) num %= 1000 if num >= 100: factor = num // 100 roman += romanHelper('M', 'D', 'C', factor) num %= 100 if num >= 10: factor = num // 10 roman += romanHelper('C', 'L', 'X', factor) num %= 10 roman += romanHelper('X', 'V', 'I', num) return roman
integer-to-roman
Faster than 99.64% python solution
MrAlpha786
1
408
integer to roman
12
0.615
Medium
531
https://leetcode.com/problems/integer-to-roman/discuss/653558/Python3-modulo-operation
class Solution: def intToRoman(self, num: int) -> str: mp = {1000:"M", 900:"CM", 500:"D", 400:"CD", 100:"C", 90:"XC", 50:"L", 40:"XL", 10:"X", 9:"IX", 5:"V", 4:"IV", 1:"I"} ans = [] for k, v in mp.items(): ans.append(num//k * v) num %= k return "".join(ans)
integer-to-roman
[Python3] modulo operation
ye15
1
127
integer to roman
12
0.615
Medium
532
https://leetcode.com/problems/integer-to-roman/discuss/653558/Python3-modulo-operation
class Solution: def intToRoman(self, num: int) -> str: mp = {1:"I", 5:"V", 10:"X", 50:"L", 100:"C", 500:"D", 1000:"M"} ans = [] m = 1 while num: num, x = divmod(num, 10) if x == 9: ans.append(mp[m] + mp[10*m]) elif x == 4: ans.append(mp[m] + mp[5*m]) else: val = "" if x >= 5: val += mp[5*m] x -= 5 while x: val += mp[m] x -= 1 ans.append(val) m *= 10 return "".join(reversed(ans))
integer-to-roman
[Python3] modulo operation
ye15
1
127
integer to roman
12
0.615
Medium
533
https://leetcode.com/problems/integer-to-roman/discuss/2847421/Python-best-solution
class Solution: def intToRoman(self, num: int) -> str: final_str = "" roman_map = { 1: "I", 4: "IV", 5: "V", 9: "IX", 10: "X", 40: "XL", 50: "L", 90: "XC", 100: "C", 400: "CD", 500: "D", 900: "CM", 1000: "M" } for key in sorted(roman_map.keys(), reverse=True): rem = int(num / key) if rem: final_str += rem * roman_map[key] num = num % key return final_str
integer-to-roman
Python best solution
Nihshreyas
0
2
integer to roman
12
0.615
Medium
534
https://leetcode.com/problems/integer-to-roman/discuss/2844956/Only-5-cases-easy-to-understand-whit-explanation
class Solution: def intToRoman(self, num: int) -> str: # Saving the digists in the list, but we must note here, # that this digits are saving as string. num = list(str(num)) # Dictionary: roman_number = { 1000 : "M", 500 : "D", 100 : "C", 50 : "L", 10 : "X", 5: "V", 1 : "I", } s = "" # The Roman numeral i = len(num) - 1 # We need that to know # what key of dictionary # we can choose. for n in num: # Looping throut the list of strings 'num'. n = int(n) a = 10**i b = 5 * a # For "V", "L", "D" c = roman_number[a] if n < 4: s = s + c * n elif n == 4: s = s + c + roman_number[b] elif n == 5: s = s + roman_number[b] elif n < 9: s = s + roman_number[b] + c * (n - 5) elif n == 9: s = s + c + roman_number[10*a] i = i - 1 return s
integer-to-roman
Only 5 cases, easy to understand whit explanation
hafid-hub
0
1
integer to roman
12
0.615
Medium
535
https://leetcode.com/problems/integer-to-roman/discuss/2833911/Python-simple-and-easy-to-understand-solution
class Solution: def intToRoman(self, num: int) -> str: d = { 1 : 'I', 4: 'IV', 5 : 'V', 9: 'IX', 10 : 'X', 40: 'XL', 50 : 'L', 90: 'XC', 100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M' } roman = '' for n in [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]: while n <= num: roman += d[n] num -= n return roman
integer-to-roman
Python simple and easy to understand solution
Sudhanshu344
0
2
integer to roman
12
0.615
Medium
536
https://leetcode.com/problems/integer-to-roman/discuss/2828597/Low-Memory-Solution-(Python3)
class Solution: def intToRoman(self, num: int) -> str: roman = [ [1000,'M'], [900,'CM'], [500,'D'], [400,'CD'], [100,'C'], [90,'XC'], [50,'L'], [40,'XL'], [10,'X'], [9,'IX'], [5,'V'], [4,'IV'], [1,'I'] ] ans = "" temp = num for r in roman: ans += r[1]*math.trunc(temp/r[0]) temp = temp%r[0] return ans
integer-to-roman
Low Memory Solution (Python3)
smithshannonf
0
3
integer to roman
12
0.615
Medium
537
https://leetcode.com/problems/integer-to-roman/discuss/2827225/Reduce-based-Solution-The-Best
class Solution: def intToRoman(self, num: int) -> str: SYMBOLS = 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' VALUES = 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 rom = '' for numeral, divisor in zip(SYMBOLS, VALUES): quot, num = divmod(num, divisor) rom += numeral * quot return rom
integer-to-roman
Reduce-based Solution, The Best
Triquetra
0
3
integer to roman
12
0.615
Medium
538
https://leetcode.com/problems/integer-to-roman/discuss/2827225/Reduce-based-Solution-The-Best
class Solution: def intToRoman(self, num: int) -> str: SYMBOLS = 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' VALUES = 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 return reduce(self.produceSymbols, zip(SYMBOLS, VALUES), (num, ''))[1] def produceSymbols(self, num_rom: tuple[int, str], sym_div: tuple[str, int]) -> tuple[int, str]: num, rom = num_rom sym, div = sym_div return num % div, rom + sym * (num // div)
integer-to-roman
Reduce-based Solution, The Best
Triquetra
0
3
integer to roman
12
0.615
Medium
539
https://leetcode.com/problems/integer-to-roman/discuss/2801102/Easy-Understanding
class Solution: def intToRoman(self, num: int) -> str: result = "" if num // 1000 != 0: result += "M" * (num // 1000) num %= 1000 if num // 900 == 1: result += "CM" num %= 900 if num // 500 == 1: result += "D" num %= 500 if num // 400 == 1: result += "CD" num %= 400 if num // 100 != 0: result += "C" * (num // 100) num %= 100 if num // 90 == 1: result += "XC" num %= 90 if num // 50 == 1: result += "L" num %= 50 if num // 40 == 1: result += "XL" num %= 40 if num // 10 != 0: result += "X" * (num // 10) num %= 10 if num // 9 == 1: result += "IX" num %= 9 if num // 5 == 1: result += "V" num %= 5 if num // 4 == 1: result += "IV" num %= 4 if num <= 3: result += "I" * (num // 1) return result
integer-to-roman
Easy Understanding
eugen3ee
0
6
integer to roman
12
0.615
Medium
540
https://leetcode.com/problems/integer-to-roman/discuss/2799652/Python3-98.38-runtime-and-80.24-memory-beats-solution!!!!
class Solution: def intToRoman(self, num: int) -> str: a = num // 1000 b = (num - 1000 * a) // 100 c = (num - 1000 * a - 100 * b) // 10 d = num - 1000 * a - 100 * b - 10 * c """ N is int number from 0 to 9. X coresppond to I. Y coresppond to V. Z coresppond to X. """ def local_intToRoman(N: int, X: str, Y: str, Z: str) -> str: if N == 0: return '' if N == 4: return X + Y if N == 9: return X + Z else: if N >= 5: return Y + X * (N - 5) if N < 5: return X * N ans = a * 'M' + local_intRoman(b, 'C', 'D', 'M') + local_intRoman(c, 'X', 'L', 'C') + local_intRoman(d, 'I', 'V', 'X') return ans
integer-to-roman
[Python3] 98.38% runtime & 80.24 % memory beats solution!!!!
jungledowmtown55
0
10
integer to roman
12
0.615
Medium
541
https://leetcode.com/problems/integer-to-roman/discuss/2798606/Python-code-using-recursive-function
class Solution: def intToRoman(self, num: int) -> str: if num >= 1000: return "M" + self.intToRoman(num-1000) elif num >= 900: return "CM" + self.intToRoman(num-900) elif num >= 500: return "D" + self.intToRoman(num-500) elif num >= 400: return "CD" + self.intToRoman(num-400) elif num >= 100: return "C" + self.intToRoman(num-100) elif num >= 90: return "XC" + self.intToRoman(num-90) elif num >= 50: return "L" + self.intToRoman(num-50) elif num >= 40: return "XL" + self.intToRoman(num-40) elif num >= 10: return "X" + self.intToRoman(num-10) elif num >= 9: return "IX" + self.intToRoman(num-9) elif num >= 5: return "V" + self.intToRoman(num-5) elif num >= 4: return "IV" + self.intToRoman(num-4) elif num >= 1: return "I" + self.intToRoman(num-1) else: return ""
integer-to-roman
Python code using recursive function
derbuihan
0
2
integer to roman
12
0.615
Medium
542
https://leetcode.com/problems/integer-to-roman/discuss/2792586/Easiest-Python-Solution
class Solution: def intToRoman(self, num: int) -> str: roman = "" values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ] numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ] for index, value in enumerate(values): roman += (num//value) * numerals[index] num %= value return roman
integer-to-roman
Easiest Python Solution
sachinsingh31
0
4
integer to roman
12
0.615
Medium
543
https://leetcode.com/problems/integer-to-roman/discuss/2779448/Integer-To-Roman
class Solution: def intToRoman(self, num: int) -> str: roman = {"I" : 1, "IV":4, "V": 5, "IX":9, "X": 10, "XL":40, "L": 50, "XC":90, "C": 100, "CD":400, "D": 500, "CM":900, "M": 1000} result = "" for key, val in reversed(roman.items()): if num // val: count = num // val result += (key*count) num = num % val else: continue return result
integer-to-roman
Integer To Roman
Hamideddie
0
7
integer to roman
12
0.615
Medium
544
https://leetcode.com/problems/integer-to-roman/discuss/2776887/Simplest-Approach-Space-Saving-Easy-To-Understand-Brute-Force-%3A-Python
class Solution: def intToRoman(self, num: int) -> str: out="" while(num>0): if num>=1 and num<5: if num>=4: num=num-4 out=out+"IV" else: num=num-1 out=out+"I" if num>=5 and num<10: if num>=9: num=num-9 out=out+"IX" else: num=num-5 out=out+"V" if num>=10 and num<50: if num>=40: num=num-40 out=out+"XL" else: num=num-10 out=out+"X" if num>=50 and num<100: if num>=90: num=num-90 out=out+"XC" else: num=num-50 out=out+"L" if num>=100 and num<500: if num>=400: num=num-400 out=out+"CD" else: num=num-100 out=out+"C" if num>=500 and num<1000: if num>=900: num=num-900 out=out+"CM" else: num=num-500 out=out+"D" if num>=1000: num=num-1000 out=out+"M" return out
integer-to-roman
Simplest Approach Space Saving Easy To Understand Brute Force : Python
hritikbatra
0
2
integer to roman
12
0.615
Medium
545
https://leetcode.com/problems/integer-to-roman/discuss/2764367/python-easy-solution
class Solution: def intToRoman(self, num: int) -> str: if num==0:return '' if num>=1000: return 'M' + self.intToRoman(num-1000) if num>=900: return 'CM'+self.intToRoman(num-900) if num>=500: return 'D' + self.intToRoman(num-500) if num>=400: return 'CD'+self.intToRoman(num-400) if num>=100: return 'C' + self.intToRoman(num-100) if num>=90: return 'XC'+self.intToRoman(num-90) if num>=50: return 'L' + self.intToRoman(num-50) if num>=40: return 'XL' +self.intToRoman(num-40) if num>=10: return 'X' + self.intToRoman(num-10) if num>=9: return 'IX'+self.intToRoman(num-9) if num>=5: return 'V' + self.intToRoman(num-5) if num>=4: return 'IV' + self.intToRoman(num-4) if num>=1: return 'I' + self.intToRoman(num-1)
integer-to-roman
python easy solution
tush18
0
9
integer to roman
12
0.615
Medium
546
https://leetcode.com/problems/integer-to-roman/discuss/2757693/Python-Deque
class Solution: def intToRoman(self, num: int) -> str: """ Symbol Value I 1 ones V 5 ones X 10 tens L 50 tens C 100 hundreds D 500 hundreds M 1000 thousands """ # mode the number to get digits # based on the digits, append the letter to the ans # reverse the ans or use dequeue() letters = [["I", "IV", "V", "IX"], ["X", "XL", "L", "XC"], ["C", "CD", "D", "CM"], ["M","","",""]] place = 0 # value place. 0-> ones, 1->tens ans = deque() while num > 0: digit = num % 10 num //= 10 lt = letters[place] if digit < 4: ans.appendleft(lt[0]*digit) elif digit == 4: ans.appendleft(lt[1]) elif digit < 9: ans.appendleft(lt[2]+lt[0]*(digit-5)) elif digit == 9: ans.appendleft(lt[3]) place += 1 return "".join(ans)
integer-to-roman
Python Deque
ckayfok
0
6
integer to roman
12
0.615
Medium
547
https://leetcode.com/problems/roman-to-integer/discuss/264743/Clean-Python-beats-99.78.
class Solution: def romanToInt(self, s: str) -> int: translations = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000 } number = 0 s = s.replace("IV", "IIII").replace("IX", "VIIII") s = s.replace("XL", "XXXX").replace("XC", "LXXXX") s = s.replace("CD", "CCCC").replace("CM", "DCCCC") for char in s: number += translations[char] return number
roman-to-integer
Clean Python, beats 99.78%.
hgrsd
1,200
60,900
roman to integer
13
0.582
Easy
548
https://leetcode.com/problems/roman-to-integer/discuss/2428756/Python-oror-Easily-Understood-oror-Faster-than-98-oror-Less-than-76-oror-O(n)
class Solution: def romanToInt(self, s: str) -> int: roman_to_integer = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000, } s = s.replace("IV", "IIII").replace("IX", "VIIII").replace("XL", "XXXX").replace("XC", "LXXXX").replace("CD", "CCCC").replace("CM", "DCCCC") return sum(map(lambda x: roman_to_integer[x], s))
roman-to-integer
🔥 Python || Easily Understood ✅ || Faster than 98% || Less than 76% || O(n)
wingskh
146
15,300
roman to integer
13
0.582
Easy
549
https://leetcode.com/problems/roman-to-integer/discuss/1791875/Python-3-greater-Simple-and-detailed-explanation
class Solution: def romanToInt(self, s: str) -> int: sym = { "I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000 } result = 0 prev = 0 for c in reversed(s): if sym[c] >= prev: result += sym[c] else: result -= sym[c] prev = sym[c] return result
roman-to-integer
Python 3 -> Simple and detailed explanation
mybuddy29
44
1,500
roman to integer
13
0.582
Easy
550
https://leetcode.com/problems/roman-to-integer/discuss/1941583/Python-3-solution-less-than-98-Memory-Usage-with-explanation.
class Solution: def romanToInt(self, s: str) -> int: mapping = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, "IV": -2, "IX": -2, "XL": -20, "XC": -20, "CD": -200, "CM": -200, } sum = 0 for symbol, val in mapping.items(): sum += s.count(symbol) * val return sum
roman-to-integer
Python 3 solution less than 98% Memory Usage with explanation.
wabesasa
33
1,500
roman to integer
13
0.582
Easy
551
https://leetcode.com/problems/roman-to-integer/discuss/357734/Python-Solution-with-explanantion
class Solution: def romanToInt(self, s: str) -> int: roman_to_int = { 'I' : 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000 } result = 0 prev_value = 0 for letter in s: value = roman_to_int[letter] result += value if value > prev_value: # preceding roman nummber is smaller # we need to undo the previous addition # and substract the preceding roman char # from the current one, i.e. we need to # substract twice the previous roman char result -= 2 * prev_value prev_value = value return result
roman-to-integer
Python Solution with explanantion
amchoukir
29
3,700
roman to integer
13
0.582
Easy
552
https://leetcode.com/problems/roman-to-integer/discuss/2272944/PYTHON-3-BEATS-99.13-or-EASY-TO-UNDERSTAND
class Solution: def romanToInt(self, s: str) -> int: a, r = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, "IV": -2, "IX": -2, "XL": -20, "XC": -20, "CD": -200, "CM": -200}, 0 for d, e in a.items(): r += s.count(d) * e return r
roman-to-integer
[PYTHON 3] BEATS 99.13% | EASY TO UNDERSTAND
omkarxpatel
20
1,600
roman to integer
13
0.582
Easy
553
https://leetcode.com/problems/roman-to-integer/discuss/2372626/Clean-efficient-and-easy-to-read-Python-beats-99.5.
class Solution: def romanToInt(self, s: str) -> int: ''' faster solution which doesn't require iterating through every element of s (currently faster than 99.49% of submissions''' # store all possible conversions roman_conversion = {"IV": 4, "IX": 9, "XL": 40, "XC": 90, "CD": 400, "CM": 900, "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} ret = 0 # dictionary is ordered, so we find if string contains special cases first for k, v in roman_conversion.items(): if k in s: ret += s.count(k) * v s = s.replace(k, "") return ret
roman-to-integer
Clean, efficient, and easy-to-read Python, beats 99.5%.
romejj
14
937
roman to integer
13
0.582
Easy
554
https://leetcode.com/problems/roman-to-integer/discuss/2021425/Python-Solution
class Solution: def romanToInt(self, s: str) -> int: roman = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} res = 0 for i in range(len(s)): if i+1 < len(s) and roman[s[i]] < roman[s[i+1]]: res -= roman[s[i]] else: res += roman[s[i]] return res
roman-to-integer
Python Solution
afreenansari25
13
915
roman to integer
13
0.582
Easy
555
https://leetcode.com/problems/roman-to-integer/discuss/1140243/Python-solution-(48ms-14.3MB)-using-a-reversed-string.
class Solution: def romanToInt(self, s: str) -> int: dic = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} res, tmp = 0, 0 for i in reversed(s): if dic[i]>=tmp: res=res+dic[i] else: res=res-dic[i] tmp=dic[i] return res
roman-to-integer
Python solution (48ms 14.3MB) - using a reversed string.
kritikaasri
13
983
roman to integer
13
0.582
Easy
556
https://leetcode.com/problems/roman-to-integer/discuss/2669258/Python-solution-with-hashmap
class Solution: def romanToInt(self, s: str) -> int: alphabet = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000, } dec_number = 0 last_add = 0 for ch in s[::-1]: if last_add > alphabet[ch]: dec_number -= alphabet[ch] else: dec_number += alphabet[ch] last_add = alphabet[ch] return dec_number
roman-to-integer
✔️ Python solution with hashmap
QuiShimo
12
1,100
roman to integer
13
0.582
Easy
557
https://leetcode.com/problems/roman-to-integer/discuss/1298123/Python3-Super-fast-than-99
class Solution: def romanToInt(self, s: str) -> int: letters = list(s) romans = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} sum_let = 0 prev = None for lets in letters: if prev is not None and romans[prev] < romans[lets]: sum_let+=(romans[lets]-(romans[prev]*2)) else: sum_let+=romans[lets] prev = lets return sum_let
roman-to-integer
Python3 Super fast than 99%
mishra_g
12
1,600
roman to integer
13
0.582
Easy
558
https://leetcode.com/problems/roman-to-integer/discuss/484638/Python-3-(one-line)
class Solution: def romanToInt(self, S: str) -> int: return sum(S.count(r)*v for r,v in [('I',1),('V',5),('X',10),('L',50),('C',100),('D',500),('M',1000),('IV',-2),('IX',-2),('XL',-20),('XC',-20),('CD',-200),('CM',-200)]) - Junaid Mansuri - Chicago, IL
roman-to-integer
Python 3 (one line)
junaidmansuri
9
1,400
roman to integer
13
0.582
Easy
559
https://leetcode.com/problems/roman-to-integer/discuss/2479970/python3-or-fast-or-space-O(1)-or-time-O(n)
class Solution: def romanToInt(self, s: str) -> int: a={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000} sum=0 x=0 while x<len(s): if x<len(s)-1 and a[s[x]]<a[s[x+1]]: sum += (a[s[x+1]] - a[s[x]]) x += 2 else: sum += a[s[x]] x += 1 return sum
roman-to-integer
python3 | fast | space-O(1) | time - O(n)
I_am_SOURAV
8
705
roman to integer
13
0.582
Easy
560
https://leetcode.com/problems/roman-to-integer/discuss/2689932/EASY-PYTHON-SOLUTION
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ roman_dic = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, } skip_next_n = False int_sum = 0 for idx, n in enumerate(s): if skip_next_n == True: skip_next_n = False continue try: if roman_dic[n] < roman_dic[s[idx + 1]]: int_sum += roman_dic[s[idx + 1]] - roman_dic[n] skip_next_n = True continue else: int_sum += roman_dic[n] except: int_sum += roman_dic[n] return int_sum
roman-to-integer
EASY PYTHON SOLUTION
raghavdabra
7
2,600
roman to integer
13
0.582
Easy
561
https://leetcode.com/problems/roman-to-integer/discuss/1628386/Faster-than-99-and-with-hash-table.
class Solution: def romanToInt(self, s: str) -> int: map_ = {"I":1 ,"V":5, "X":10, "L":50, "C":100, "D":500, "M":1000} val = 0 i = len(s)-1 while i >= 0: if map_[s[i]] > map_[s[i-1]] and i != 0: val += (map_[s[i]]-map_[s[i-1]]) i -= 1 else: val += map_[s[i]] i -= 1 return val
roman-to-integer
Faster than 99% and with hash table.
AmrinderKaur1
7
811
roman to integer
13
0.582
Easy
562
https://leetcode.com/problems/roman-to-integer/discuss/2595959/Roman-to-Integer-oror-Python3-oror-Easy
class Solution: def romanToInt(self, s: str) -> int: roman_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} value = 0 for i in range(0, len(s)-1): # If we have low value char before higher value then it should be subtracted # Like 'IV' or 'IX' or 'CM' if(roman_val[s[i]] < roman_val[s[i+1]]): value -= roman_val[s[i]] # Else add up the value of character like 'XII or 'MX' else: value += roman_val[s[i]] value += roman_val[s[-1]] return value
roman-to-integer
Roman to Integer || Python3 || Easy
vanshika_2507
5
370
roman to integer
13
0.582
Easy
563
https://leetcode.com/problems/roman-to-integer/discuss/2427959/Python-Elegant-and-Short-or-93.51-faster-or-Constant-time-and-memory
class Solution: """ Time: O(1) Memory: O(1) """ ROMAN_TO_INTEGER = { 'I': 1, 'IV': 4, 'V': 5, 'IX': 9, 'X': 10, 'XL': 40, 'L': 50, 'XC': 90, 'C': 100, 'CD': 400, 'D': 500, 'CM': 900, 'M': 1000, } def romanToInt(self, s: str) -> int: converted = 0 for roman, integer in self.ROMAN_TO_INTEGER.items(): while s.endswith(roman): s = s.removesuffix(roman) converted += integer return converted
roman-to-integer
Python Elegant & Short | 93.51% faster | Constant time and memory
Kyrylo-Ktl
5
404
roman to integer
13
0.582
Easy
564
https://leetcode.com/problems/roman-to-integer/discuss/1524835/Python-Solution-with-'subtraction'-idea
class Solution: def romanToInt(self, s: str) -> int: dict_normal = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} dict_subtract = {'IV': 1, 'IX': 1, 'XL': 10, 'XC': 10, 'CD': 100, 'CM': 100} sum_all = sum([dict_normal[s_letter] for s_letter in list(s)]) sum_deduct = sum([dict_subtract[subtract_key] for subtract_key in dict_subtract.keys() if subtract_key in s]) return sum_all - sum_deduct * 2
roman-to-integer
Python Solution with 'subtraction' idea
xxhowchanxx
5
445
roman to integer
13
0.582
Easy
565
https://leetcode.com/problems/roman-to-integer/discuss/1409296/Python-3-Simple-code-using-dictionary
class Solution: def romanToInt(self, roman: str) -> int: convert = {'IV':'IIII', 'IX':'VIIII', 'XL':'XXXX', 'XC':'LXXXX', 'CD':'CCCC', 'CM':'DCCCC'} roman_units = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1} for key in convert.keys(): if key in roman: roman = roman.replace(key, convert[key]) return sum([roman_units[i] for i in roman])
roman-to-integer
[Python 3] Simple code using dictionary
shreyamittal117
5
281
roman to integer
13
0.582
Easy
566
https://leetcode.com/problems/roman-to-integer/discuss/246482/Creative-no-dictionary-Python-Solution
class Solution: def romanToInt(self, s: str) -> int: sym = 'IVXLCDM' val = [1,5,10,50,100,500,1000] highest, res = 0, 0 for i in s[::-1]: ix = sym.index(i) if ix >= highest: res += val[ix] highest = ix else: res -= val[ix] return res
roman-to-integer
Creative no-dictionary Python Solution
leetro
5
794
roman to integer
13
0.582
Easy
567
https://leetcode.com/problems/roman-to-integer/discuss/2690026/Python-Simple-O(n)-Solution-with-full-working-explanation
class Solution: # Time: O(n) and Space: O(1) def romanToInt(self, s: str) -> int: # From Smallest Roman value to Biggest. roman = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } # Smaller roman after Bigger: add it to the Bigger one i.e. VIII = 8 # Smaller roman before Bigger: subtract it to the Bigger one i.e. IV = 5 - 1 = 4 res = 0 for i in range(len(s)): if i + 1 < len(s) and roman[s[i]] < roman[s[i + 1]]: # if there exist i+1 index and Smaller Roman is before Bigger res -= roman[s[i]] else: # if there exist only one index or Smaller Roman is after Bigger res += roman[s[i]] return res
roman-to-integer
Python Simple O(n) Solution with full working explanation
DanishKhanbx
4
672
roman to integer
13
0.582
Easy
568
https://leetcode.com/problems/roman-to-integer/discuss/2304864/Easy-Python-Solution-Using-Dictionary
class Solution: def romanToInt(self, s: str) -> int: roman_dict = { "I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 } ans = roman_dict[s[-1]] for i in range(len(s)-1): if roman_dict[s[i]] < roman_dict[s[i+1]]: ans -= roman_dict[s[i]] else: ans += roman_dict[s[i]] return ans
roman-to-integer
Easy Python Solution Using Dictionary
zip_demons
4
483
roman to integer
13
0.582
Easy
569
https://leetcode.com/problems/roman-to-integer/discuss/2006278/Simple-Python-solution
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} num = 0 for i in range(len(s)-1): # If the next letter has larger value than the current, the current letter subtracts if d[s[i]] < d[s[i+1]]: num -= d[s[i]] # If not, it adds else: num += d[s[i]] # Add the last number num += d[s[-1]] return num
roman-to-integer
Simple Python solution
rafaelcuperman
4
307
roman to integer
13
0.582
Easy
570
https://leetcode.com/problems/roman-to-integer/discuss/1865456/Easy-to-understand-Python-solution
class Solution: def romanToInt(self, s: str) -> int: convertedInt = 0 romanHashmap = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, } prevChar = None for i in range(0, len(s)): currChar = s[i] # Only do comparison once something has been stored in variable 'prevChar' if (i > 0) and (romanHashmap[currChar] > romanHashmap[prevChar]): convertedInt = convertedInt - 2*romanHashmap[prevChar] + romanHashmap[currChar] else: convertedInt += romanHashmap[currChar] # Store current character in variable 'prevChar' for comparison in next iteration prevChar = currChar return convertedInt
roman-to-integer
Easy to understand Python solution
deepjaisia
4
598
roman to integer
13
0.582
Easy
571
https://leetcode.com/problems/roman-to-integer/discuss/1781818/2-dictionaries-2-loops.-logical.-O(N)
class Solution: def romanToInt(self, s: str) -> int: map={ "I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000, } total = 0 ex_map = { "IV":4, "IX":9, "XL":40, "XC":90, "CD":400, "CM":900 } for ex, val in ex_map.items(): if ex in s: s = s.replace(ex, "") total += val for c in s: total += map[c] return total
roman-to-integer
2 dictionaries 2 loops. logical. O(N)
_rn_
4
333
roman to integer
13
0.582
Easy
572
https://leetcode.com/problems/roman-to-integer/discuss/1670229/Logic-explained-in-detail.
class Solution: def romanToInt(self, s: str) -> int: lookup = { 'I':1, 'V': 5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000, } integer = 0 prev = cur = lookup[s[0]] for i in s[1:]: if lookup[i] <= prev: cur += lookup[i] else: cur += lookup[i] - 2*prev prev = lookup[i] return cur
roman-to-integer
Logic explained in detail.
souravrane_
4
222
roman to integer
13
0.582
Easy
573
https://leetcode.com/problems/roman-to-integer/discuss/1591120/Python3-Solution
class Solution: def romanToInt(self, s: str) -> int: d = {'I':1, 'V':5, 'X':10, 'L':50,'C':100,'D':500,'M':1000} number = 0 p = 0 for i in range(len(s)-1,-1,-1): if d[s[i]]>= p: number += d[s[i]] else: number -= d[s[i]] p = d[s[i]] return number
roman-to-integer
Python3 Solution
light_1
4
152
roman to integer
13
0.582
Easy
574
https://leetcode.com/problems/roman-to-integer/discuss/1591120/Python3-Solution
class Solution: def romanToInt(self, s: str) -> int: number = 0 s = s.replace("IV", "IIII") s = s.replace("IX", "VIIII") s = s.replace("XL", "XXXX") s = s.replace("XC", "LXXXX") s = s.replace("CD", "CCCC") s = s.replace("CM", "DCCCC") for char in s: number += d[char] print(number) return number
roman-to-integer
Python3 Solution
light_1
4
152
roman to integer
13
0.582
Easy
575
https://leetcode.com/problems/roman-to-integer/discuss/1548697/EZ-Solution-or-PYTHON-or-97-97
class Solution: def romanToInt(self, s: str) -> int: symbolsValue = {'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900, 'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} res = 0 while s: for key, val in symbolsValue.items(): if key in s: res += val * s.count(key) s = s.replace(key,'') return res
roman-to-integer
EZ Solution | PYTHON | 97% 97%
zoharfran
4
566
roman to integer
13
0.582
Easy
576
https://leetcode.com/problems/roman-to-integer/discuss/672937/Python-line-by-line-explanation
class Solution: def romanToInt(self, s: str) -> int: # use a dictionary to map the letters to values digits = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000} # set the year as the value of the last letter year = digits[s[len(s)-1]] # i is the index of the string, starting from the last letter for i in range(len(s)-1, 0, -1): # get the value of the last letter cur = digits[s[i]] # prev is the one to the left prev = digits[s[i-1]] # if value of previous letter is larger than the current one, add to year if prev >= cur: year += prev # otherwise, subtract from year # e.g. "IX" where X = 10 and I = 1, we subtract 1 from 10 else: year -= prev return year
roman-to-integer
Python line by line explanation
derekchia
4
346
roman to integer
13
0.582
Easy
577
https://leetcode.com/problems/roman-to-integer/discuss/652705/Python3-linear-scan
class Solution: def romanToInt(self, s: str) -> int: mp = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} ans = 0 for i in range(len(s)): if i+1 < len(s) and mp[s[i]] < mp[s[i+1]]: ans -= mp[s[i]] else: ans += mp[s[i]] return ans
roman-to-integer
[Python3] linear scan
ye15
4
247
roman to integer
13
0.582
Easy
578
https://leetcode.com/problems/roman-to-integer/discuss/652705/Python3-linear-scan
class Solution: def romanToInt(self, s: str) -> int: mp = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000, "IV":4, "IX":9, "XL":40, "XC":90, "CD":400, "CM":900} ans = i = 0 while i < len(s): if s[i:i+2] in mp: ans += mp[s[i:i+2]] i += 2 else: ans += mp[s[i:i+1]] i += 1 return ans
roman-to-integer
[Python3] linear scan
ye15
4
247
roman to integer
13
0.582
Easy
579
https://leetcode.com/problems/roman-to-integer/discuss/2161575/python-solution
class Solution: def romanToInt(self, s: str) -> int: d={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000} x=0 i=0 while i<len(s): if i+1<len(s): if d[s[i]]<d[s[i+1]]: x=x+d[s[i+1]]-d[s[i]] i=i+2 else: x=x+d[s[i]] i=i+1 else: x=x+d[s[i]] i=i+1 return x
roman-to-integer
python solution
komin521
3
448
roman to integer
13
0.582
Easy
580
https://leetcode.com/problems/roman-to-integer/discuss/2028400/Python3%3A-easy-looped-over
class Solution: def romanToInt(self, s: str) -> int: r = 0 m = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} for i, c in enumerate(s): if (i <= len(s)-2 and m[c] < m[s[i+1]]): r-=m[c] else: r+=m[c] return r
roman-to-integer
Python3: easy looped over
doesnotcompile
3
418
roman to integer
13
0.582
Easy
581
https://leetcode.com/problems/roman-to-integer/discuss/1811219/Python3-Sol-Faster-than-89.84
class Solution: def romanToInt(self, s: str) -> int: d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} res = 0 for i in range(len(s)-1): if d[s[i]] < d[s[i+1]]: res = res - d[s[i]] else: res = res + d[s[i]] return res + d[s[len(s)-1]]
roman-to-integer
Python3 Sol - Faster than 89.84%
shivamm0296
3
318
roman to integer
13
0.582
Easy
582
https://leetcode.com/problems/roman-to-integer/discuss/1760354/Simple-Python-Solution
class Solution: def romanToInt(self, s: str) -> int: d={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000, 'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900} i,su=0,0 while i<len(s): if i+1<len(s) and s[i:i+2] in d: su+=d[s[i:i+2]] # print(su) i+=2 else: # print(s[i]) su+=d[s[i]] i+=1 return su
roman-to-integer
Simple Python Solution
adityabaner
3
449
roman to integer
13
0.582
Easy
583
https://leetcode.com/problems/roman-to-integer/discuss/1371160/36-ms
class Solution: def romanToInt(self, s: str) -> int: hashmap = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} total, prev = 0, 0 for i in s: cur = hashmap[i] if cur > prev: total -= prev total += cur - prev else: total += cur prev = cur return total
roman-to-integer
36 ms
Ojaas
3
168
roman to integer
13
0.582
Easy
584
https://leetcode.com/problems/roman-to-integer/discuss/2781564/python-solution-using-list-and-list-methods
class Solution: def romanToInt(self, s: str) -> int: sym=["I","V","X","L","C","D","M"] val=[1,5,10,50,100,500,1000] sum=0 l=len(s) flag=0 i=0 while i <l-1: fp=sym.index(s[i]) sp=sym.index(s[i+1]) if sp>fp: sum=sum+val[sp]-val[fp] i+=2 flag=1 if i==l-1: flag =0 else: ind=sym.index(s[i]) sum=sum+val[ind] flag=0 i+=1 if flag ==0 : ff=sym.index(s[l-1]) sum=sum+val[ff] return sum
roman-to-integer
python solution using list and list methods🔥
Yadunandan_1
2
607
roman to integer
13
0.582
Easy
585
https://leetcode.com/problems/roman-to-integer/discuss/2686088/Smooth-Python-3-solution-(greater98)
class Solution: def romanToInt(self, s: str) -> int: translation = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} l1 = [translation[i] for i in s] l1.reverse() num = 0 for idx, i in enumerate(l1): if idx == 0: num += i elif i < l1[idx - 1]: num = num - i else: num += i return num
roman-to-integer
Smooth Python 3 solution (>98%)
sskabarin
2
1,100
roman to integer
13
0.582
Easy
586
https://leetcode.com/problems/roman-to-integer/discuss/2426734/Python-Solution-with-Dictionary
class Solution: def romanToInt(self, s: str) -> int: d={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000,"IV":4,"IX":9,"XL":40,"XC":90,"CD":400,"CM":900} ans=0 i=0 while i<len(s): if s[i:i+2] in d: ans+=d[s[i:i+2]] i+=2 else: ans+=d[s[i]] i+=1 return ans
roman-to-integer
Python Solution with Dictionary
a_dityamishra
2
272
roman to integer
13
0.582
Easy
587
https://leetcode.com/problems/roman-to-integer/discuss/2420141/Python-Simple-and-Easy-to-Understand-oror-O(N)-Time-Complexity
class Solution: def romanToInt(self, s: str) -> int: roman = {'I': 1, 'V': 5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} res = 0 for i in range(len(s)): if i+1 < len(s) and roman[s[i]] < roman[s[i+1]]: res -= roman[s[i]] else: res += roman[s[i]] return res
roman-to-integer
Python - Simple and Easy to Understand || O(N) Time Complexity
dayaniravi123
2
263
roman to integer
13
0.582
Easy
588
https://leetcode.com/problems/roman-to-integer/discuss/2071631/For-beginners-Python
class Solution: def romanToInt(self, s: str) -> int: # str -> list # group list by digits value_list = [] for symbol in s: if symbol == "M": value_list.append(1000) elif symbol == "D": value_list.append(500) elif symbol == "C": value_list.append(100) elif symbol == "L": value_list.append(50) elif symbol == "X": value_list.append(10) elif symbol == "V": value_list.append(5) elif symbol == "I": value_list.append(1) # check the +- for value in range(len(value_list)-1): if value_list[value] < value_list[value+1]: value_list[value] = -value_list[value] # sum the groups number = sum(value_list) # output->int return number
roman-to-integer
For beginners, Python
a19941006
2
231
roman to integer
13
0.582
Easy
589
https://leetcode.com/problems/roman-to-integer/discuss/1881601/Easy-Python-Solution
class Solution: def romanToInt(self, s: str) -> int: ls = list(s) romD = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} stadD = {'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900} val = 0 pos = 0 while(True): temp = "" if(((len(s)-pos) >= 2) and (s[pos]+s[pos+1]) in stadD): val += stadD[(s[pos]+s[pos+1])] pos += 2 elif(pos < len(s)): val += romD[s[pos]] pos += 1 else: break return val
roman-to-integer
Easy Python Solution
EvilGod_
2
712
roman to integer
13
0.582
Easy
590
https://leetcode.com/problems/roman-to-integer/discuss/1840914/Generalised-Python-3-Solution
class Solution: def romanToInt(self, s: str) -> int: convert = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } out = 0 prev = 0 # store last added to compare to current for i in range(len(s) - 1, -1, -1): # Case: numbers like 4, written as IV, where smaller number precedes larger one if(prev > convert[s[i]]): out -= convert[s[i]] else: # Default case: next digit is the same as or larger than previous one out += convert[s[i]] # Update prev digit prev = convert[s[i]] return out
roman-to-integer
Generalised Python 3 Solution
rhinnawi95
2
256
roman to integer
13
0.582
Easy
591
https://leetcode.com/problems/roman-to-integer/discuss/1816357/Simple-Python-Solution-oror-92-Faster
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ roman_symobols = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, } total = 0 previous_rim = s[-1] for i in s[::-1]: roman_int = roman_symobols[i] if roman_symobols[previous_rim] > roman_int: total -= roman_int continue previous_rim = i total += roman_int return total
roman-to-integer
Simple Python Solution || 92% Faster
depocoder
2
333
roman to integer
13
0.582
Easy
592
https://leetcode.com/problems/roman-to-integer/discuss/1743461/Python-3-or-Replacing-or-99.79-Faster-97.91-less-memory-usage-or
class Solution: def romanToInt(self, s: str) -> int: result = 0 values = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, # Substraction letters "Q": 4, "W": 9, "E": 40, "R": 90, "T": 400, "Y": 900, } # Replacing substractions replacements = { "IV": "Q", "IX": "W", "XL": "E", "XC": "R", "CD": "T", "CM": "Y", } # Check for substractions and replacing for pair in replacements: s = s.replace(pair, replacements[pair]) # Calculating for letter in s: result += values[letter] return result
roman-to-integer
Python 3 | Replacing | 99.79% Faster, 97.91% less memory usage |
anotherprogramer
2
271
roman to integer
13
0.582
Easy
593
https://leetcode.com/problems/roman-to-integer/discuss/1447129/Simple-40-ms-Pure-Python-Solution-7-lines
class Solution: def romanToInt(self, s: str) -> int: dic={"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 } m,total=dic[s[0]],0 for i in s: n=dic[i] total = (total- 2*m + n) if(m<n) else (total+ n) m=n return total
roman-to-integer
Simple 40 ms Pure Python Solution , 7 lines
vineetkrgupta
2
339
roman to integer
13
0.582
Easy
594
https://leetcode.com/problems/longest-common-prefix/discuss/1351149/Python-and-startswith
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: pre = strs[0] for i in strs: while not i.startswith(pre): pre = pre[:-1] return pre
longest-common-prefix
Python & startswith
lokeshsenthilkumar
72
4,500
longest common prefix
14
0.408
Easy
595
https://leetcode.com/problems/longest-common-prefix/discuss/484683/Python-3-(beats-~97)-(six-lines)
class Solution: def longestCommonPrefix(self, S: List[str]) -> str: if not S: return '' m, M, i = min(S), max(S), 0 for i in range(min(len(m),len(M))): if m[i] != M[i]: break else: i += 1 return m[:i] - Junaid Mansuri - Chicago, IL
longest-common-prefix
Python 3 (beats ~97%) (six lines)
junaidmansuri
65
12,000
longest common prefix
14
0.408
Easy
596
https://leetcode.com/problems/longest-common-prefix/discuss/1621297/python-runtimegreater94.46-and-memoryless81.95
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: str1, str2 = min(strs), max(strs) i = 0 while i < len(str1): if str1[i] != str2[i]: str1 = str1[:i] i +=1 return str1
longest-common-prefix
python runtime>94.46% and memory<81.95%
fatmakahveci
29
2,700
longest common prefix
14
0.408
Easy
597
https://leetcode.com/problems/longest-common-prefix/discuss/1422342/Python-oror-Easy-Solution
class Solution: def longestCommonPrefix(self, lst: List[str]) -> str: ans = "" for i in zip(*lst): p = "".join(i) if len(set(p)) != 1: return (ans) else: ans += p[0] return (ans)
longest-common-prefix
Python || Easy Solution
naveenrathore
24
2,500
longest common prefix
14
0.408
Easy
598
https://leetcode.com/problems/longest-common-prefix/discuss/783976/Python-Simple-Solution-beats-100-runtime-12ms
class Solution(object): def longestCommonPrefix(self, strs): if len(strs) == 0: return "" s1, s2 = min(strs), max(strs) i = 0 while i < len(s1) and i < len(s2) and s1[i] == s2[i]: i += 1 return s1[:i]
longest-common-prefix
Python - Simple Solution, beats 100%, runtime 12ms
shaggy_x
13
1,600
longest common prefix
14
0.408
Easy
599