Integer to Roman - Leetcode 12 - Python

  Рет қаралды 81,477

NeetCode

NeetCode

Күн бұрын

Пікірлер: 72
@bigboyshassen59
@bigboyshassen59 Ай бұрын
Here you go kings and queens, save yourself some time: symList = [["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]]
@aryanyadav3926
@aryanyadav3926 2 жыл бұрын
Great explanation! I don't even go through the discuss section if NeetCode's video is available.
@mengjiasings1278
@mengjiasings1278 3 жыл бұрын
Thanks for your work! I am trying to develop a habit of watching your video everyday before bed.
@priyanshu715
@priyanshu715 3 жыл бұрын
after bed will give you better result
@katrinak3469
@katrinak3469 2 жыл бұрын
Best explanations out there. Your videos are so succinct and well explained that I can understand any problem in about ten minutes! Got a KZbin account just so I could subscribe and support the channel as a thank you! So thank you!!
@arunks4918
@arunks4918 3 жыл бұрын
Best in youtube. To the point, short but complete 👍
@parkboy
@parkboy 11 ай бұрын
As of Python 3.7 hashmap (dict) preserves insertion order so you don't need to use a list.
@self-learning1824
@self-learning1824 2 жыл бұрын
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.
@jpkeys6000
@jpkeys6000 2 жыл бұрын
Because you need to iterate in order. A dictionary key-value pair has no order.
@Mis_guidedz
@Mis_guidedz Жыл бұрын
@@jpkeys6000they do in python …
@andresbonelli
@andresbonelli 8 ай бұрын
Cool explanation! Another possible solution: def intToRoman(self, num: int) -> str: symList = [...] # (Your symbols list) # Roman zero does not exist if num
@balu45653
@balu45653 6 ай бұрын
it works fine bro
@RichardSmith-os2hk
@RichardSmith-os2hk 2 ай бұрын
STOP ITTTTTTTTTTTTTTTTTTTTTTTTTT I WANTED TO SOLVE IT NUMBNUTS
@nehascorpion
@nehascorpion 2 жыл бұрын
Woah! That was an outstanding explanation. Extremely clear and concise. Thanks a ton!
@LordKing13
@LordKing13 3 жыл бұрын
like+subscribed! thx for the help! I m learning a lot with your channel bro!
@adventurer2395
@adventurer2395 Жыл бұрын
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!
@jpkeys6000
@jpkeys6000 2 жыл бұрын
The only thing I can say is: Thank you, thank you, thank you!!!
@rpgiri2002
@rpgiri2002 4 ай бұрын
Thanks dude. Your simple explanation is superb.
@travelnlearn
@travelnlearn Жыл бұрын
You explained it like a peice of a cake
@safwan1340
@safwan1340 11 ай бұрын
Is it not mandatory to give an (!= 0) in the if statement?
@MideStudio
@MideStudio 2 жыл бұрын
Thank you a lot, from France !
@maxrivera3275
@maxrivera3275 Жыл бұрын
Hermosa solución. Simple y efectiva.
@gary-williams
@gary-williams 10 ай бұрын
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.
@imomuganda
@imomuganda 2 жыл бұрын
Simple and brilliant.
@johnniegilkerson4724
@johnniegilkerson4724 6 ай бұрын
ok how do you run it with no main?
@ayonnnnnnn
@ayonnnnnnn 5 ай бұрын
u made it so easy man thanks
@Technical-zero
@Technical-zero 7 ай бұрын
thank you for your help😀
@DatascienceConcepts
@DatascienceConcepts 3 жыл бұрын
Such a clear explanation!
@athangkulkarni8245
@athangkulkarni8245 Жыл бұрын
Can we use LinkedHashMap and iterate through it? Complexity to retrieve an element would be reduced
@parkboy
@parkboy 11 ай бұрын
Yes you can and as of Python 3.7 the hashmap preserves insertion order.
@69savage75
@69savage75 3 жыл бұрын
Good video sir.
@aaryandubey6906
@aaryandubey6906 Жыл бұрын
Good Explanation
@oyeshetty
@oyeshetty 3 жыл бұрын
What about the number 1000 , it shows CDCDXCXCXX instead of M which is already in the array
@Dhanushh
@Dhanushh 2 жыл бұрын
1000 / 1000 gives count 1. which then multiplied with M gives you M alone. Then num becomes 0 due to mod operation. loop breaks
@dannyhd8301
@dannyhd8301 2 жыл бұрын
absolutely amazing
@KTBWorld
@KTBWorld 2 жыл бұрын
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.
@איתיבןחיים-ט6מ
@איתיבןחיים-ט6מ 3 жыл бұрын
What about the number 8? In roman numerals its VIII, but if I'm not mistaken according to this code it'd be IVIV
@OGRfilms
@OGRfilms 2 жыл бұрын
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
@איתיבןחיים-ט6מ
@איתיבןחיים-ט6מ 2 жыл бұрын
@@OGRfilms oh got it, thank you
@data_science_uzbekistan
@data_science_uzbekistan 10 ай бұрын
thanks for your help for beginners like me 😂
@MidoValo
@MidoValo 2 жыл бұрын
thank you so much
@ivanleon6164
@ivanleon6164 Жыл бұрын
I have a different solución, but tours is good to me too.
@chenhaibin2010
@chenhaibin2010 2 жыл бұрын
such an elegant solution
@052dhirojkumarsahu9
@052dhirojkumarsahu9 Жыл бұрын
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.
@animeshsingh4290
@animeshsingh4290 Жыл бұрын
It's already in the list of our conditions
@ISHSACHDEVA-i9r
@ISHSACHDEVA-i9r 4 ай бұрын
Stop the iteration if num==0: if num==0: return res
@deadpoet4427
@deadpoet4427 3 жыл бұрын
Best 👍
@dingus2332
@dingus2332 7 ай бұрын
You could have avoided the complexity of reversing the list , simply making the list reverse from the start
@JOHNSMITH-ve3rq
@JOHNSMITH-ve3rq Жыл бұрын
Why nest these solutions inside a class? Why not just make the scope the py script?
@KeepCoding69
@KeepCoding69 Жыл бұрын
Because u have to submit this code in leetcode by nesting it inside a class. That's how leetcode works
@owenwu7995
@owenwu7995 Жыл бұрын
This is more math than programming
@uqu200
@uqu200 2 жыл бұрын
for 1994 it's giving wrong answer
@rajanmaheshwari
@rajanmaheshwari 2 жыл бұрын
No, it gives the right answer i.e "MCMXCIV". However I did in Swift, but the concept is same
@sutriptaroy
@sutriptaroy 2 жыл бұрын
Yes, for 1994, it's not working
@abhineetsingh6720
@abhineetsingh6720 2 жыл бұрын
it works perfectly , just now submitted it with the same logic
@FaberLSH
@FaberLSH 5 ай бұрын
Awesome!!
@_Who_u_are
@_Who_u_are 9 ай бұрын
how wiukd anyone know that ,he has to make a list for this problem?
@dingus2332
@dingus2332 7 ай бұрын
From Roman to Integer , it was pretty clear , only some cases are not considered , and those could be added here
@lukaszplachecki8723
@lukaszplachecki8723 5 ай бұрын
🎉
@finwwwfinwww4669
@finwwwfinwww4669 2 жыл бұрын
Am i the only one in 30s and still struggling with such problems :(
@edwardteach2
@edwardteach2 3 жыл бұрын
U a Roman God
@quandingleberry445
@quandingleberry445 2 ай бұрын
If num is 8000 wouldn't this give 8 M's?
@Yo-sb9st
@Yo-sb9st 2 жыл бұрын
This one was hard
@HimanshuKumar_24
@HimanshuKumar_24 9 ай бұрын
Can someone please type that list of lists for me, I'll Gpay 20 Rs.
@raghavendras8866
@raghavendras8866 7 ай бұрын
Bruh 😂
@JashSingh-bv5ge
@JashSingh-bv5ge Жыл бұрын
im too dumb to code
@Ausbals
@Ausbals 9 ай бұрын
LOL
@karthick...
@karthick... Жыл бұрын
I hate python bcz i am C++ coder
@firozkamdar6735
@firozkamdar6735 10 ай бұрын
Agreed
@tsunningwah3471
@tsunningwah3471 Жыл бұрын
sexjhvhj
@arsenearteta
@arsenearteta 5 ай бұрын
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)
Game of Life - Leetcode 289 - Python
17:36
NeetCode
Рет қаралды 44 М.
Integer to Roman | Leetcode #12
17:34
Techdose
Рет қаралды 46 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 196 М.
Roman to Integer - Leetcode 13 - Arrays & Strings (Python)
6:15
Palindrome Number - Leetcode 9 - Python
12:00
NeetCode
Рет қаралды 82 М.
Integer to English Words - Leetcode 273 - Python
20:59
NeetCodeIO
Рет қаралды 16 М.
Implement Trie (Prefix Tree) - Leetcode 208
18:56
NeetCode
Рет қаралды 223 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 490 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 238 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН