Roman to Integer | LeetCode 13 | Coding Interview Tutorial

preview_player
Показать описание
Roman to Integer solution: LeetCode 13

AFFILIATE LINKS
If you're interested in learning algorithms, these are great resources.

💲 All coupons and discounts 💲

Roman to Integer | LeetCode 13 | Coding Interview
#romantointeger #leetcode #algorithms #terriblewhiteboard #codinginterview

Click the time stamp to jump to different parts of the video.
00:00 Title
00:06 Problem readout
02:22 Whiteboard solution
06:38 Coding solution
15:36 Result and outro
Рекомендации по теме
Комментарии
Автор

If there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!

TerribleWhiteboard
Автор

You legit have some of the best explanations and problem break downs. Drawing out the problem and solving it side by side with the code is an excellent idea.

alvarocastro-cid
Автор

Dude. Such a great job with the explanation! Thanks so much! Also, see you haven't posted in about 2 years, hope you're doing well.

CameraMeetsWorld
Автор

thank you for making these! Still helpful 2 years later! Your explanations are super helpful!

chips-n-salsa
Автор

Dude, this was so helpful with the visuals on the left side breaking down the code! liked and subbed

TheCaioFreitas
Автор

Best explanation so far, seen other videos from different channel but only video explaining in so much depth.

shashibhushansingh
Автор

Why aren't you uploading anymore? Your explanation makes the logic crystal clear.

rv-bz
Автор

Great explanation! Breaking down the problem and turning your logic into code step by step was a great way to tackle the problem. I believe it is a great approach :)

hunorvadasz-perhat
Автор

thanks for the video.Good explanation :)

AyushiSharmaDSA
Автор

Thank you so much, your videos are awesome

TolgayToklar
Автор

Very easy to follow explanation! Thanks!

logandallalio
Автор

Amazing. Thanks.
var romanToInt = function (s) {
const list = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};

let total = 0;

for (let i = 0; i < s.length; i++) {
let currentInt = list[s.charAt(i)]; //Look at the first roman numeral pass it through the map and get the value
let nextInt = list[s.charAt(i + 1)];

if (nextInt) {
//Checks for presence of a nextInt (edge case)
if (currentInt >= nextInt) {
//If currentInt is greater than or equal to nextInt
total += currentInt; //Add the current integer to the total
} else {
total += nextInt - currentInt; //If currentInt is not greater than or equal to nextInt we subtract and add it to total
i++; //Since we have accounted for both values we are incrementing to check next set
}
} else {
total += currentInt;
}
}
return total;
};

DmitriyMalayevProfile
Автор

Beautiful, I was following the other approach which was not elegant. Yours is better.
total += dicto[l1[i]]
total -= 2*dicto[l1[i-1]]

historyvspresent
Автор

my solution was indentical to this in cpp but the problem its slow, 40 ms to be exact, and is faster than 5.93% of cpp programs..
any ideas on why that's the case ? is there a faster implementation algorithm ??

seal
Автор

Thanks for the great explanation. But when i try this in python it gives an error for nextInt :- string index out of range
Can you please help?

mortysmith
Автор

Why are you not uploading videos? Please make more youtube videos

shivashankar
Автор

I think your spoken explanation makes more sense: the number 4 is written as IV. It's not that you do subtraction, it's that the written symbol IV is 4. The rule doesn't have anything to do with "the number on the left is smaller than the number on the right" - the rule is that IV is the way you write 4, and CM is 900. It's just a lookup table. (Having said that, I think the problem is so simple that anyone who does anything sensible is going to be fine in an interview.)

JamesMooreSeattle
Автор

A cleaner alternative that avoids O(n)^2 is

const romanToInt = (s) => {
let map = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
let total = 0
for (let i = 0; i < s.length; i++) {
let current = map[s[i]]
let next = map[s[i + 1]]
if (next && current >= next) {
total += current
} else if (next && current < next) {
total += next - current
i++
} else {
total += current
}
}
return total
};

hideinbush
Автор

better than nick white's 🤭 explanations

WhatIsThisAllAbout
Автор

Channel name should be wonderful whiteboard not a terrible whiteboard.

anandparmar