Тёмный

Maximum Score From Removing Substrings - Leetcode 1717 - Python 

NeetCodeIO
Подписаться 217 тыс.
Просмотров 13 тыс.
50% 1

Опубликовано:

 

21 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 99   
@ishwarkoki1119
@ishwarkoki1119 3 месяца назад
Video req : " proof by contradiction "
@imtsrk04
@imtsrk04 3 месяца назад
Proof by contradiction was just perfect!!! Seriously a GOATed video of yours!! Keep making more :)
@MP-ny3ep
@MP-ny3ep 3 месяца назад
Terrific explanation ! Love how in-depth you went. Too good.
@md_pedia1
@md_pedia1 3 месяца назад
Ur explanation was phenomenal
@forsakengod6668
@forsakengod6668 3 месяца назад
do make the course on discrete maths . it will be awesome learning from you. ❤❤
@divyanshudwivedi3756
@divyanshudwivedi3756 3 месяца назад
This video cleared all my doubts that I had after seeing the editorials on leetcode. Great job bro ! keep it up ! Subscribed and liked !
@divyaraj-rana
@divyaraj-rana 2 месяца назад
Pure talent! Thanks for explaining in-depth and iterating through your approach! Amazed by your work you do!
@rishabhmediratta6200
@rishabhmediratta6200 3 месяца назад
WOW WOW WOW WOW this is my favorite video of yours of ALL TIME. I have tears. PURE ART.
@GelfandTransform
@GelfandTransform 3 месяца назад
Great vid, thank you 💪 I think it is not trivial why the greedy algorithm gives the optimal result. The challenge is to prove that removing e.g. one “ab” doesn’t ruin a potential goldmine of getting “bbbaaa” later.
@mohamedanas8493
@mohamedanas8493 3 месяца назад
mind blowing explanation
@GeetainSaar
@GeetainSaar 3 месяца назад
13:45 oh my god😂😂😂😂😭😭 you are awesome
@NeetCodeIO
@NeetCodeIO 3 месяца назад
My job is solving LC problems but my true love is mathematics ❤️ ... maybe in the next life
@krishnanandyadav9248
@krishnanandyadav9248 3 месяца назад
@@NeetCodeIO lets do that discrete mathematics course then
@iamgt2392
@iamgt2392 3 месяца назад
Amazing explanation!
@grantpeterson2524
@grantpeterson2524 3 месяца назад
I actually managed to make a single-pass solution that doesn't require mutating the input string OR using any intermediate data structure, just 3 integers. For each section of "a" and "b" strings, we can basically just keep a counter of the "ideal first element"s, of the "ideal second elements"s, and of the "ideal pairs" as we iterate. As we iterate, we try to greedily create as many "ideal pairs" as we can. We do this according to this simple rule: If we encounter an "ideal second element", then we check to see if the "ideal first element" counter is at least 1. If it is, then we can form an ideal pair, so we increase the "ideal pair" count by 1, and take away 1 "ideal first element" (since it is consumed to make the pair). If we have reached an ideal second element but we have NO ideal first elements, then we simply increase the increase the "ideal second element" counter. Finally, if we have an ideal first element, we just increment that counter no matter what. Finally, at the end, we know that for any pair that wasn't made into an ideal pair, it must be a non-ideal pair, so we just take "ideal points * ideal pairs + min(ideal first element, ideal second element) * nonideal points". Here is the code in Rust: ``` pub fn maximum_gain(s: String, x: i32, y: i32) -> i32 { let (ideal_start, ideal_points, other_points) = if x > y { ('a', x, y) } else { ('b', y, x) }; s.split(|c| c != 'a' && c != 'b') .map(|char_section| { let (start_count, end_count, ideal_pairs) = char_section.chars() .fold((0, 0, 0), |(start_count, end_count, ideal_pairs), c| match (start_count, c == ideal_start) { (0, false) => (start_count, end_count + 1, ideal_pairs), (_, false) => (start_count - 1, end_count, ideal_pairs + 1), _ => (start_count + 1, end_count, ideal_pairs) } ); start_count.min(end_count) * other_points + ideal_pairs * ideal_points }) .sum() } ```
@abhishekdhyade7500
@abhishekdhyade7500 3 месяца назад
Wow, just phenomenal explanation. Just epic. Thanks a lot Neetcode
@notcountdankula
@notcountdankula 3 месяца назад
God level explanation 🔥🔥
@adarshbhaskar
@adarshbhaskar 3 месяца назад
Here I was thinking how to handle "ab" after 2nd phase and the "Proof of contradiction" just blew my mind! You should definitely do a course on discrete math
@CuriousAnonDev
@CuriousAnonDev 3 месяца назад
damn, this was a tuff question for a medium tag! thanks for the explanation
@AbdulRehmanKhan.
@AbdulRehmanKhan. 3 месяца назад
Damn, Navi got me so excited at 14:00
@chandlerbing8164
@chandlerbing8164 3 месяца назад
Please create course of Math There are many people who does not come in software industry from math background especially in India where people do BCA / MCA to be in software industry so we can learn math and also improve logical thinking.
@gavinebenezer1670
@gavinebenezer1670 3 месяца назад
I think it would be cool if u showed code in like c++ or some other language for the O(1) space solution. Awesome video!!
@utkarshdewan8736
@utkarshdewan8736 3 месяца назад
It will be there on his website in the All section
@ishansheth3005
@ishansheth3005 3 месяца назад
great explanation man!!
@AkshayGohilYT
@AkshayGohilYT 3 месяца назад
14:20 🤯 Literally
@fxrcode7923
@fxrcode7923 3 месяца назад
awesome proof by contradiction . wondering if you have the problem for this kind of problem that asking for optimal scores after modify given data (str, tree, graph, etc), sometime, it's solved by DP/BFS.
@wyclifflumumba2064
@wyclifflumumba2064 3 месяца назад
Navdeep, you did indeed blow my mind haha. Good explanation
@pastori2672
@pastori2672 3 месяца назад
i would love a course of proof by contradiction and proof by like counter example and other types of proofs and generally just algebra, i think it actually requires reasoning just like algorithms and will actually help in the long run :)
@blenderbottle382
@blenderbottle382 3 месяца назад
How do you know when choosing a greedy approach that there will never be a case where the greedy choice produces a suboptimal result in the end? I.e you chose "ab" because it is higher point, but that actually causes an output that produces a suboptimal result? Thats where I got stuck with this problem
@shidrathrahman423
@shidrathrahman423 3 месяца назад
Neetcode please make a course on discrete Mathematics I really want to learn more about it please please
@deveshahuja5096
@deveshahuja5096 3 месяца назад
8:23 the berlin wall?
@NeetCodeIO
@NeetCodeIO 3 месяца назад
Yeah that's what came to my mind as well
@letsprogress4124
@letsprogress4124 3 месяца назад
genuinely S tier video
@josss7866
@josss7866 3 месяца назад
Any hint at when your python for interviews course is coming out? I'm really excited for it
@atharvsankpal
@atharvsankpal 3 месяца назад
Let's go for DM course
@plinygg
@plinygg 3 месяца назад
discrete math course would be awesome👍👍
@Supakills101
@Supakills101 3 месяца назад
Strings are mutable in c++ (c++ mention)
@SalmanZaman
@SalmanZaman 3 месяца назад
very nice explanation...
@finemechanic
@finemechanic 3 месяца назад
Actually, you didn't prove that taking all 'ab' first is the optimal solution. You just proved that having done that you will not get any 'ab' at the second stage.
@gameacc6079
@gameacc6079 3 месяца назад
Yup realised that too. Felt abit of dunning kruger
@NeetCodeIO
@NeetCodeIO 3 месяца назад
The proof wasn't about taking "ab" first, it was about eliminating all "ab" and "ba" in two phases, regardless of which we choose first. And yes, technically i didn;t do a formal proof, i dont think anyone wants to see that. but let me ask you, whats the difference between "ab" and "ba"? Aren't they symetrical? If we took "ba" first (assuming it has the higher score) we would end up with the same result. Try it if you dont believe me. :)
@finemechanic
@finemechanic 3 месяца назад
@@NeetCodeIO At 14:45 you're speaking about proving greedy algorithms, and my point is you didn't provide the proof. Sorry if you didn't mean that. Let's suppose 'ab' has the most score. Then what if removing 'ba' before all 'ab''s are removed could form a new pair and give better total score? One have to prove this wrong in order to prove that a greedy algorithm gives an optimal solution.
@zweitekonto9654
@zweitekonto9654 3 месяца назад
​​@@finemechanic this is proved by using the same method in the video, taking all cases, axxa, axxb etc. Once you strike out each case, you would realize taking the maximum pair first has to be the optimal solution and any other choice would lead to lower points.
@victorrodriguez698
@victorrodriguez698 3 месяца назад
goated explanantion
@chien-yuyeh9386
@chien-yuyeh9386 3 месяца назад
Interesting question!
@grzegorzdziedzic3786
@grzegorzdziedzic3786 3 месяца назад
proof by contradiction - We need it
@pratikshitsingh415
@pratikshitsingh415 3 месяца назад
O(1) space complexity solution is interesting!
@gamania0258
@gamania0258 3 месяца назад
13:33 bruh become Jett when killing this problem.
@eshukla15
@eshukla15 3 месяца назад
we are very much interested my guy, anything you wanna teach, just go for it, hehe
@juanmacias5922
@juanmacias5922 3 месяца назад
LFG discreet math video :D
@TenzDelek
@TenzDelek 3 месяца назад
bro did a full analysis
@jyotiutb
@jyotiutb 3 месяца назад
this is great
@xSanu.
@xSanu. 3 месяца назад
Please make proof by contradiction videos available. Thank you. 🙏
@armaan_gohil
@armaan_gohil 3 месяца назад
Not gonna lie, mind blown!
@segue97
@segue97 3 месяца назад
imagine seeing this in an interview for the first time. how does one come up with a working solution and proof in 20 minutes?
@neks2081
@neks2081 3 месяца назад
A lot of practice
@harshitarupani811
@harshitarupani811 3 месяца назад
Hey ! Can u explain how we will update score in space optimal solution?
@ABHDelirious
@ABHDelirious 3 месяца назад
I would like a course on discrete maths
@yaxprajapati115
@yaxprajapati115 3 месяца назад
How did you determine that we need to use a stack for this solution? If I encounter a different question, how will I know when to use a stack?
@neks2081
@neks2081 3 месяца назад
If you didn’t think of a stack in this problem, you haven’t solved enough stack problems. Do more stack problems.
@_lax.man_5473
@_lax.man_5473 3 месяца назад
Mind = blown 😶‍🌫️
@aadil4236
@aadil4236 3 месяца назад
My first instinct was to solve it with dp. I did code the solution but it was brute force. Is it ok? Or do I have to come up with this optimal approach at once. will solving this question in brute force manner will indicate to the interviewer that I am good enough?
@dhatrishdixit6004
@dhatrishdixit6004 3 месяца назад
bro can you provide brute force solution cause my brute force one was getting wrong
@neks2081
@neks2081 3 месяца назад
No. Brute force is usually not enough. It is a good starting point tho
@aadil4236
@aadil4236 3 месяца назад
@@dhatrishdixit6004 I posted a link to my submission
@DeepakGupta-ky8mp
@DeepakGupta-ky8mp 3 месяца назад
My Same logic in C++ runs slower, why becoz of too many time of reverse string becoz of stack??
@janardannn
@janardannn 3 месяца назад
strings are mutable in cpp so 0(n) gain is real
@jegadheeswarank6290
@jegadheeswarank6290 3 месяца назад
How u become this good at explaining
@NeetCodeIO
@NeetCodeIO 3 месяца назад
No secret tips, it's just Ive explained about 600 LC problems. I think anyone would get better at explaining if they did that
@yaswanthfinds
@yaswanthfinds 3 месяца назад
discrete maths please
@priyam86f
@priyam86f 3 месяца назад
yep, proud of the fact that I was able to think of a leetcode medium problem solution in less than 2 minutes(solution accepted). My approach was, iterate over the string character by character, if the current char is the same as that of the high value string, and the next char is same as that of the high value string, we skip them and add the high value score sum. Once we are done with the high value string we then move to the low value once repeating the process. By doing this, the problem can be easily solved.
@SurajSingh-mf3vz
@SurajSingh-mf3vz 3 месяца назад
I somehow solved it in constant space in python. What do you think? class Solution: def maximumGain(self, s: str, x: int, y: int) -> int: a_count = b_count = 0 res = 0 for c in s: if x >= y: if c == 'a': a_count += 1 elif c == 'b': if a_count > 0: a_count -= 1 res += x else: b_count += 1 else: res += y * min(a_count, b_count) a_count = b_count = 0 else: if c == 'b': b_count += 1 elif c == 'a': if b_count > 0: b_count -= 1 res += y else: a_count += 1 else: res += x * min(a_count, b_count) a_count = b_count = 0 res += min(x, y) * min(a_count, b_count) return res
@budderpenguin43
@budderpenguin43 3 месяца назад
this is not correct. how are you enforcing that the ab string is consecutive? also you are never modifying the input string
@SurajSingh-mf3vz
@SurajSingh-mf3vz 3 месяца назад
​@@budderpenguin43 Its different approach from what neetcode has shown for the O(1) space solution
@viktoriia7480
@viktoriia7480 3 месяца назад
I'm so frustrated that I thought of the exact solution you explained, the idea about stack and that we can first eliminate most expensive substrings and then others, but couldn't understand how to code it.... I got stuck when the idea which you proved can not exist by contradiction came to my mind
@viktoriia7480
@viktoriia7480 3 месяца назад
Actually I coded it after your explanation without looking at your solution. Thanks!
@sachethkoushik2265
@sachethkoushik2265 3 месяца назад
Thankyou so much for your daily videos, i managed tto score an intern due to this, thankyou so soo muchhhh🥹🥹
@NeetCodeIO
@NeetCodeIO 3 месяца назад
LETS GOOO love the profile pic
@sachethkoushik2265
@sachethkoushik2265 3 месяца назад
@@NeetCodeIO Thanks 😆😆
@sachethkoushik2265
@sachethkoushik2265 3 месяца назад
@@NeetCodeIO your reply made my dayyy 🥹🥹
@tiger7858
@tiger7858 3 месяца назад
🙌🙌 You are Genius 🫡🙌🎩
@chaitanyasharma6270
@chaitanyasharma6270 3 месяца назад
class Solution { StringBuilder left; public int maximumGain(String s, int x, int y) { int ans=0; left=new StringBuilder(s); if(x>y){ ans+=takeAway("ab")*x; ans+=takeAway("ba")*y; } else{ ans+=takeAway("ba")*y; ans+=takeAway("ab")*x; } return ans; } public int takeAway(String sub){ int i=left.indexOf(sub); int c=0; while(i>=0){ c++; left.delete(i, i + 2); i=left.indexOf(sub); } return c; } } TLE
@RohitRaj-hl6ji
@RohitRaj-hl6ji 3 месяца назад
I can only think of dp solution. Greedy feels hard
@yurei_12
@yurei_12 3 месяца назад
can someone explain to me why it doesn't work in python? I know that string is immutable, but can't you still convert it to an array
@finemechanic
@finemechanic 3 месяца назад
You will have O(n) space complexity right at the moment of building the array.
@deadlyecho
@deadlyecho 3 месяца назад
A typical greedy, but you need to make sure otherwise it's a DP
@daniellang231
@daniellang231 3 месяца назад
🐐
@sidazhong2019
@sidazhong2019 Месяц назад
My brain is damaged this morning.
@rakeshnoothi6852
@rakeshnoothi6852 3 месяца назад
@gameacc6079
@gameacc6079 3 месяца назад
## SOLN FOR LIST OPTIMIZED O(1) space class Solution: def maximumGain(self, s: str, x: int, y: int) -> int: if x > y: l, r = 'a', 'b' else: l, r = 'b', 'a' x, y = y, x # assume ab > ba # a XXX b # XXX is some eliminations of ba, like baba bbaa etc. notice the first and last char of XXX is b and a respectively, so they must have been eliminated in first trial # why choosing the larger valued one is preferred? the only case it is worse is that we give up >= 2 smaller ones, but thats notpossible. worse case is smth like baba. using ab won't make us miss > 1 ba s = list(s) res = 0 prev = -1 for i in range(len(s)): if prev != -1 and s[prev] == l and s[i] == r: prev -= 1 res += x else: s[prev+1] = s[i] prev += 1 pprev = -1 for i in range(prev+1): if pprev != -1 and s[pprev] == r and s[i] == l: pprev -= 1 res += y else: s[pprev + 1] = s[i] pprev += 1 return res
@PhaniHarshithKotturu
@PhaniHarshithKotturu 3 месяца назад
Feels like someone annoyed him about this problem, especially the phase 2 😂😂
@SajalNema13
@SajalNema13 3 месяца назад
beauty
@rakeshnoothi6852
@rakeshnoothi6852 3 месяца назад
Give us Discrete Math 🧠🧠
@GeetainSaar
@GeetainSaar 3 месяца назад
pls provide codein other languages cpp java or atleast give your code in description
@carlpittenger
@carlpittenger 3 месяца назад
a variation of the O(1) space solution is in the corresponding leetcode editorial, but it can only be done in langs with a mutable string parameter s like c++ and rust, but python and java have an immutable string parameter s
@SameerAnand-YearBTechElectrica
@SameerAnand-YearBTechElectrica 3 месяца назад
it was a boo boo
@jegadheeswarank6290
@jegadheeswarank6290 3 месяца назад
Great explanation ❤
Далее
Microservices are Technical Debt
31:59
Просмотров 549 тыс.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
Faster than Rust and C++: the PERFECT hash table
33:52
Просмотров 591 тыс.
I Solved 100 LeetCode Problems
13:11
Просмотров 144 тыс.
Lucky Numbers in a Matrix - Leetcode 1380 - Python
16:21
Filling Bookcase Shelves - Leetcode 1105 - Python
19:17
God-Tier Developer Roadmap
16:42
Просмотров 7 млн