We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!! Questions you might like: ✅✅✅[ Tree Data Structure ] : th-cam.com/play/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls.html ✅✅✅[ Graphs Data Structure ] : th-cam.com/play/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C.html ✅✅✅[ January Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn.html ✅✅✅[ December Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA.html ✅✅✅[ November Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e.html ✅✅✅[ August Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe.html ✅✅✅July Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC-.html ✅✅✅June Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG.html ✅✅✅May Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e.html ✅✅✅Cracking the Coding Interview - Unique String: th-cam.com/play/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE.html Struggling in a question?? Leave in a comment and we will make a video!!!🙂🙂🙂
I think it is almost the same. const s = 'LVIII' let result = 0 const dict = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }; let arr = s.split(''); for (var i = 0; i < arr.length; i++) { if (dict[arr[i]] < dict[arr[i + 1]]) { result += (dict[arr[i]]) * (-1) } else { result += dict[arr[i]] } } return(result)
Explanation and everything was good. But next time please dont cover up your mistakes. We all are humans (Proof: Check 3rd line of the code at 4:36 and 4:38)
thanks for this!! One query , how can we handle -ve cases like below? ("IV", "IIII") ("IX", "VIIII") ("XL", "XXXX") ("XC", "LXXXX") ("CD", "CCCC") ("CM", "DCCCC")
I have a question, in LeetCode all code should be inside the function? Even the const Map that will be use multiple times? Can I put this map in the Solution class instead?
Explanation is good 👍but help me for program not working showing this Line 4: Char 13: error: use of undeclared identifier 'character' Mapmap=new HashMap();
Hi mam, i am facing this issue while programming the roman to integer (Syntax Error: invalid syntax ^ Class Solution { Line 1 (Solution.py)) full program I was did:- Class Solution { public int romanToInt(string, s){ Map map = new Hashmap(); map.put("I" , 1); map.put("V" , 5); map.put("X" , 10); map.put("L" , 50); map.put("C" , 10); map.put("D" , 500); map.put("M" , 1000);
int res = map.get(s.charAt(s.length()-1)); for (int i = s.length()-2; i >=0; i--) { if (map.get(s.chartAt(i)) < map.get (s.charAt(i+1))){ res-=map.get(s.charAt(i)); } else{ res+=map.get(s.charAt(i)); } } return res; } }
int res = map.get(s.charAt(s.length()-1)); for (int i = s.length()-2; i >=0; i--) { if (map.get(s.charAt(i)) < map.get (s.charAt(i+1))){ res-=map.get(s.charAt(i)); } else{ res+=map.get(s.charAt(i)); } } return res; Use this code, This is edited version of your code.
May anyone please tell me why does my code always ignore the last element? HashMap map = new HashMap(); map.put('I', 1); map.put('V', 5); map.put('X', 10); map.put('L', 50); map.put('C', 100); map.put('D', 500); map.put('M', 1000); int total =0; for(int i=0; i
Your solution is 5 ms. This solution is 4 ms. Which one is better to choose? public int romanToInt(String s) { int sum = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'I') sum += 1; if (s.charAt(i) == 'V') sum += 5; if (s.charAt(i) == 'X') sum += 10; if (s.charAt(i) == 'L') sum += 50; if (s.charAt(i) == 'C') sum += 100; if (s.charAt(i) == 'D') sum += 500; if (s.charAt(i) == 'M') sum += 1000; if ( i != 0) { if (s.charAt(i) == 'V' && s.charAt(i - 1) == 'I') sum -=2; if (s.charAt(i) == 'X' && s.charAt(i - 1) == 'I') sum -=2; if (s.charAt(i) == 'L' && s.charAt(i - 1) == 'X') sum -=20; if (s.charAt(i) == 'C' && s.charAt(i - 1) == 'X') sum -=20; if (s.charAt(i) == 'D' && s.charAt(i - 1) == 'C') sum -=200; if (s.charAt(i) == 'M' && s.charAt(i - 1) == 'C') sum -=200; } } return sum; }
We hope you all are enjoying our videos!!! Don't forget to leave a comment!!! Please like the video to support us!!!
Questions you might like:
✅✅✅[ Tree Data Structure ] : th-cam.com/play/PLJtzaiEpVo2zx-rCqLMmcFEpZw1UpGWls.html
✅✅✅[ Graphs Data Structure ] : th-cam.com/play/PLJtzaiEpVo2xg89cZzZCHqX03a1Vb6w7C.html
✅✅✅[ January Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2wCalBcRcNjXQ0C6ku3dRkn.html
✅✅✅[ December Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xo8OdPZxrpybGR8FmzZpCA.html
✅✅✅[ November Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2yMYz5RPH6pfB0wNnwWsK7e.html
✅✅✅[ August Leetcoding Challenge ] : th-cam.com/play/PLJtzaiEpVo2xu4h0gYQzvOMboclK_pZMe.html
✅✅✅July Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wrUwkvexbC-vbUqVIy7qC-.html
✅✅✅June Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2xIfpptnCvUtKrUcod2zAKG.html
✅✅✅May Leetcoding challenges: th-cam.com/play/PLJtzaiEpVo2wRmUCq96zsUwOVD6p66K9e.html
✅✅✅Cracking the Coding Interview - Unique String: th-cam.com/play/PLJtzaiEpVo2xXf4LZb3y_BopOnLC1L4mE.html
Struggling in a question??
Leave in a comment and we will make a video!!!🙂🙂🙂
Please make a series od dsa problem and solution explaination video.
None of my code run in my leetcode
Best explanation for this problem on TH-cam. Thanks.
wow! I never thought of going reverse. I like your explanation a lot! I was using a switch case with repetitive code.
Yeah me neither, I would always start from left -> right and add the numbers.
@@aaqibjavedz2569 you could do it in that way too
thanks! I have been stuck on this problem for a while now. you made it make a lot of sense!
This channel provides the best solutions. Keep up the good work.
Thanks Shubhankar!!! 🙂
Just wow. I coded this solution from left to right, but it becomes complicated and lots of conditional statements. Right to left is awesome. Thanks.
I think it is almost the same.
const s = 'LVIII'
let result = 0
const dict = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
};
let arr = s.split('');
for (var i = 0; i < arr.length; i++) {
if (dict[arr[i]] < dict[arr[i + 1]]) {
result += (dict[arr[i]]) * (-1)
} else {
result += dict[arr[i]]
}
}
return(result)
Good Editing, Clear Voice and clean solution.
I've subscribed
Thanks !!
Explanation and everything was good. But next time please dont cover up your mistakes. We all are humans (Proof: Check 3rd line of the code at 4:36 and 4:38)
Yeh Smart 🤓🤣
the perfect explanation and in just 5 minutes!! Thank you.
Really a great video, we need more such Leetcode video solutions! Thank you very much for this one.
Glad you enjoyed it!
Your presentation visuals are amazing. Thanks for the content
Thank you!!
AMAZING CONTENT!!
SIMPLE AND PERFECT!!!!
Thank you!!
Very easy and simple approach and well explained crystal clear i used to watch this channel for more videos .Thank you 🌟🌟
Just watched the video. This is the kind of explanation I was looking for. Subscribed.
Glad you enjoyed it!
we can go forward to it also takes O(n) time complexity
Ping me if u need the code or a Hint
Clean, Crisp, to the point code
Appreciate that
The perfect Channel name for the perfect Video. Algorithms made easy.....❤❤❤❤❤
Better than other explanation videos :) Thank you
Thanks for creating this presentation and explanation.
Thank you so much!! Do checkout our latest videos too!! 👍👍
right to left approach makes it very easy ,thanks
you made it easier to understand..really great
Glad it helped!
you just gave me the new perespective to the question
Thank you
it shows error like this :
Line 4: Char 13: error: use of undeclared identifier 'Character'
Map map = new HashMap();
Same
Perfect explanation mam👏
Mam u kept the solution clean and easy.
Bravo!!! Am clapping.
Thanks !!
I don't know why but your code is giving wrong output on geeksforgeeks ide
In 3rd line, it is not Map and character C shld be capital ..It is HashMap=new HashMap();
thanks for this!! One query , how can we handle -ve cases like below?
("IV", "IIII") ("IX", "VIIII")
("XL", "XXXX") ("XC", "LXXXX")
("CD", "CCCC") ("CM", "DCCCC")
what if i pass IIX as there is no validation can you please explain
What's better to use in this case, a hashmap or a switch statement?
hashmap
@@sudipta_pradhan Why? Arent we using extra space if we use hashmap?
@@gautamjh the hashmap doesn't grow, it's constant. It could even be static, therefore, space complexity is still O(1), isn't it?
Great explanation. Thankyou so much.😊
I have a question, in LeetCode all code should be inside the function? Even the const Map that will be use multiple times? Can I put this map in the Solution class instead?
very well explained!
Thanks !
easy to understand explanation. Thank you
nicely explained
Thanks a lot mam, this really helped for me !
Happy to help!!
great simple and perfect
very nice and easy solution thank you
Thanks !!
it is really hard to cope pls how do you guys get so good at understanding stuff like this, how does one even learn programming to understand better.
Solid Explanation!
Explanation is good 👍but help me for program not working showing this Line 4: Char 13: error: use of undeclared identifier 'character'
Mapmap=new HashMap();
Use capital C in the word Character
Thanks !
how can we solve it, without using auxillary space?
Super explained mam.
Very useful 🙂
Glad you liked it !!
best explanation
Thank you so much
Happy to help!!
I don't know why but your code is giving wrong output on geeksforgeeks ide. And one more thing why you are taking s.length()-1 why not s.length() ?
Thanks di!!
most welcome!!
why we traverse the string from the end???
thank you.....
nice explanation
mam y we have to perform only friom right to left why nit from startin g index
thank you
how that mcmxciv come?
Is it possible ? without using Map ?
why should we go from right to left?plzz
why we are applying a map .why can't put the values in a single line? like (I=1,V=5,C=100,M=1000) LIKE THIS
Thanks❤🌹🙏 a lot😊
how can we convert roman numbers into numbers(counting 1,2,3) in word document, I tried but not found solution if u have plz share
C++ code:
class Solution {
public:
int romanToInt(string s) {
map map;
map['I']=1;
map['V']=5;
map['X']=10;
map['L']=50;
map['C']=100;
map['D']=500;
map['M']=1000;
int result = map[s[s.length()-1]];
for(int i =s.length()-2;i>=0;i--){
if(map[s[i]]
Hi mam, i am facing this issue while programming the roman to integer (Syntax Error: invalid syntax
^
Class Solution {
Line 1 (Solution.py))
full program I was did:-
Class Solution {
public int romanToInt(string, s){
Map map = new Hashmap();
map.put("I" , 1);
map.put("V" , 5);
map.put("X" , 10);
map.put("L" , 50);
map.put("C" , 10);
map.put("D" , 500);
map.put("M" , 1000);
int res = map.get(s.charAt(s.length()-1));
for (int i = s.length()-2; i >=0; i--) {
if (map.get(s.chartAt(i)) < map.get (s.charAt(i+1))){
res-=map.get(s.charAt(i));
}
else{
res+=map.get(s.charAt(i));
}
}
return res;
}
}
Do we have a any method to resolve it?
Try to use capital c in character not small
Mapmap = new HashMap();
map.put('I' , 1);
map.put('V' , 5);
map.put('X' , 10);
map.put('L' , 50);
map.put('C' , 100);
map.put('D' , 500);
map.put('M' , 1000);
int res = map.get(s.charAt(s.length()-1));
for (int i = s.length()-2; i >=0; i--) {
if (map.get(s.charAt(i)) < map.get (s.charAt(i+1))){
res-=map.get(s.charAt(i));
}
else{
res+=map.get(s.charAt(i));
}
}
return res;
Use this code, This is edited version of your code.
Bro, plz tell me how do I become good at solving these like you? 😭 I Suck more than my life!
May anyone please tell me why does my code always ignore the last element?
HashMap map = new HashMap();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int total =0;
for(int i=0; i
Because i does not reaches the last position u should add the corresponding integer value of last character and then your answer will be correct
is this an easy ques? oh ma god
I am your 1000th like, haha!
Thanks for the like.
awesome
Your solution is 5 ms. This solution is 4 ms. Which one is better to choose? public int romanToInt(String s) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'I') sum += 1;
if (s.charAt(i) == 'V') sum += 5;
if (s.charAt(i) == 'X') sum += 10;
if (s.charAt(i) == 'L') sum += 50;
if (s.charAt(i) == 'C') sum += 100;
if (s.charAt(i) == 'D') sum += 500;
if (s.charAt(i) == 'M') sum += 1000;
if ( i != 0) {
if (s.charAt(i) == 'V' && s.charAt(i - 1) == 'I') sum -=2;
if (s.charAt(i) == 'X' && s.charAt(i - 1) == 'I') sum -=2;
if (s.charAt(i) == 'L' && s.charAt(i - 1) == 'X') sum -=20;
if (s.charAt(i) == 'C' && s.charAt(i - 1) == 'X') sum -=20;
if (s.charAt(i) == 'D' && s.charAt(i - 1) == 'C') sum -=200;
if (s.charAt(i) == 'M' && s.charAt(i - 1) == 'C') sum -=200;
}
}
return sum;
}
Understood :-)
just wow
C++ code pls
class Solution
{
public:
int romanToInt(string s) {
int ans=0;
mapmp;
mp.insert(make_pair('I',1));
mp.insert(make_pair('V',5));
mp.insert(make_pair('X',10));
mp.insert(make_pair('L',50));
mp.insert(make_pair('C',100));
mp.insert(make_pair('D',500));
mp.insert(make_pair('M',1000));
for(int i=s.length()-2; i>=0;i--){
if(mp[s[i]]
Nice explanation
Thank you very much