Delete and Earn - Dynamic Programming - Leetcode 740 - Python

  Рет қаралды 52,665

NeetCode

NeetCode

Күн бұрын

Пікірлер: 66
@tandradesouza
@tandradesouza 2 жыл бұрын
Even though this is a "medium" in leetcode, this guy makes every coding problem seem like an easy one. Hey man, you've got such great teaching skills. Thank you for sharing your algo approaches here. This channel is definitely gold.
@pseudo_adult17
@pseudo_adult17 Жыл бұрын
Ty! np: sorted() converts the set of nums array to the list in Python; can skip list() function of line #4
@mocamoca_enh
@mocamoca_enh 2 жыл бұрын
I have a couple of coding interviews coming later this month. Not expecting much since I pretty much just started relearning a few weeks back but emulating and learning from your explanations and solutions helped me learn more in those couple of weeks than I have in months of bruteforcing LC back in college.
@firezdog
@firezdog 10 ай бұрын
another small improvement would be to consider i = 0 as a base case, i.e. fill out earn1 as 0 and earn2 as nums[0] * counts[nums[0]], then start iterating from i = 1
@techandmore12
@techandmore12 Ай бұрын
Sorting already takes O(n) space. So using an array does not affect space complexity
@chahalpawanpreet
@chahalpawanpreet Жыл бұрын
This is an exceptionally well taught lesson
@NeetCode
@NeetCode Жыл бұрын
Thank you!
@krishnachaitanyapullakandam
@krishnachaitanyapullakandam 2 жыл бұрын
This is by far one of the best DP explanations, thanks so much!
@sdmfslkdm
@sdmfslkdm Жыл бұрын
did the recursive solution on my own but optimising this is tough
@freshlyminted6999
@freshlyminted6999 2 жыл бұрын
sooo elegant - thank you!!
@vedantshinde2277
@vedantshinde2277 2 жыл бұрын
I implemented a sub-optimal solution using hashmap and set which worked for 50% of the testcases but when you said it is similar to house robber, a light-bulb went off in my head!! You're a real G!
@shashijais789
@shashijais789 2 жыл бұрын
Brilliant explanation. Thanks for this.
@NeetCode
@NeetCode 2 жыл бұрын
No problem 👍
@chinmayp5859
@chinmayp5859 5 ай бұрын
Amazing Explaination !
@_rohit97
@_rohit97 2 жыл бұрын
@ time stamp 12:48 you told you over complicated it up! Bruhh Nooo you did not! It was pristine!
@yashgupta-fk3zc
@yashgupta-fk3zc 2 жыл бұрын
in 5 min of a video i was able to code it my self
@11iqra
@11iqra Жыл бұрын
Amazing explanation!
@ananya___1625
@ananya___1625 2 жыл бұрын
wow..You gave a very good hint which made me solve the problem without watching the solution .. Anyways watched the solution after solving..Thank you very much for your efforts
@chasevanhaselen1680
@chasevanhaselen1680 2 жыл бұрын
why do we only need to look at the two previous values? couldn't values before then change depending on which current value we choose?
@manan_hingorani
@manan_hingorani 5 ай бұрын
Leetcode weekly 402, almost similar question came (Q3)
@roadtomillion9499
@roadtomillion9499 2 жыл бұрын
Hey I'm new to leetcode I recently found your channel I do solve easy problems by following your explanation But it seems I tend to forget the problem that I solved within a week How to retain the concepts that you teach in every video?
@mrditkovich2339
@mrditkovich2339 2 жыл бұрын
Hi, I was facing the same problem beforehand. Any coding problem has 2 things: intuition and coding. You only have to remember the intuition. Once you are able to understand it, coding is no big deal !
@roadtomillion9499
@roadtomillion9499 2 жыл бұрын
@@mrditkovich2339 hey thanks for the suggestion These days I started to write down those algorithms in my notebook then execute them in code Now I'm feeling far better
@mrditkovich2339
@mrditkovich2339 2 жыл бұрын
@@roadtomillion9499 Glad to hear ! As an addition, please prefer quality over quantity. Most of the questions related to graph and DP follow a pattern.
@omarhosny6947
@omarhosny6947 2 жыл бұрын
@@roadtomillion9499 I also recommend following with the Spaced Repetition System in order to revisit what you have solved.
@ngneerin
@ngneerin 2 жыл бұрын
Thanks. More videos will be appreciated 🙂
@robalexnat
@robalexnat 2 жыл бұрын
Mr NeetCode, how can we ever achieve your level of intuitive understanding of algorithms, dynamic programming, and the like? Where should we look?
@mdlwlmdd2dwd30
@mdlwlmdd2dwd30 2 жыл бұрын
Just practice
@raghav5354
@raghav5354 Жыл бұрын
If I get into faang, you deserve my first salary lol. such a quality explanation
@johnbanyan11
@johnbanyan11 Жыл бұрын
I'll atleast meet him and give me a hug.
@amanshujaiswal4728
@amanshujaiswal4728 Жыл бұрын
good explanation
@yang5843
@yang5843 2 жыл бұрын
Was this question inspired by someone on your discord?
@NeetCode
@NeetCode 2 жыл бұрын
Yeah, I wanted to do it much sooner... but better late than never :)
@yagniktalaviya2146
@yagniktalaviya2146 2 жыл бұрын
man, you are everywhere!!
@deepakumari1858
@deepakumari1858 2 жыл бұрын
Hey, Can you please also mention the Time and Space complexity of the final solution in your videos....Interviewers generally ask for it. Thanks.
@hemesh5663
@hemesh5663 2 жыл бұрын
Hey, the time complexcity is nlogn because we are sorting and traversing through the array once and space is complexity is 2n one of dictionary and other for array but if we take big oh then it is n. Hope it helps.
@gary1951
@gary1951 2 жыл бұрын
If we have [2,3,5] and we choose 5, won't we get 5 + 3 + 2 =10? from your method and leetcode would say is only 8.
@vishwaashegde4987
@vishwaashegde4987 2 жыл бұрын
you cannot add 2,3 since 3-1=2
@ahmedzaidi4228
@ahmedzaidi4228 Жыл бұрын
only if this dude coded in c++
@rajeevradnair
@rajeevradnair 2 жыл бұрын
Dear Neetcode - why did you sort your array? And why did the DP function not include numbers greater than the selected value (like n+2, n+3, etc)
@sauravgupta7415
@sauravgupta7415 Жыл бұрын
So that we can track the n-1 numbers easily. Also at every index we maintain the max value.
@yang5843
@yang5843 2 жыл бұрын
I'm surprised you're not sharing the time optimal solution, the O(n) version instead of the O(nlogn) one
@AshutoshKumar-es8xy
@AshutoshKumar-es8xy 2 жыл бұрын
How nlogn
@ashishchoubey1380
@ashishchoubey1380 2 жыл бұрын
@@AshutoshKumar-es8xy sorting was used
@orellavie6233
@orellavie6233 2 жыл бұрын
Instead of commenting meaningless information, publish it
@Aripoma
@Aripoma Жыл бұрын
A year passed and you haven’t provided the o(n) solution
@AkifIsmail-f9p
@AkifIsmail-f9p Ай бұрын
so there is no O(n) solution I presume. Went through the solution board but there wasn't any
@vikassinghbhadoria4386
@vikassinghbhadoria4386 2 жыл бұрын
i really like ur explaining method but the thing is i and most beginner cp enthusiast code in c++ this creates a prblm for all of us if u can code in c++ that will be really helpfull for us thanks btw
@anjaliyadav9360
@anjaliyadav9360 2 жыл бұрын
Thanks !
@firezdog
@firezdog 10 ай бұрын
it seems unnecessary to make nums into a set -- if you are going to use a Counter() you get a set out of the keys anyway
@bryannaoumbarakat4144
@bryannaoumbarakat4144 2 жыл бұрын
HI, i don't know if you are answering but i am not getting the idea of the start of the operations, like when we have earn1 = 0 and earn2 = 0, i am trying to imagine it as a dp list and apply it but lost
@AbhishekS-cv3cr
@AbhishekS-cv3cr Жыл бұрын
damm that was such a good explanation
@shrimpo6416
@shrimpo6416 2 жыл бұрын
LLLLLLLOVE this explanation!!!!!!
@dineshpabbi7005
@dineshpabbi7005 2 жыл бұрын
Guys I find it very discouraging when I cannot figure out a DP problem. I want to be able to make base cases but some of the questions just have this weird trick.. Is it just me ?
@ahmedhamed5133
@ahmedhamed5133 2 жыл бұрын
Amazing
@TVSh0rts
@TVSh0rts Жыл бұрын
The fact You have to know that You need to sort it first and then eliminate duplicates is ludicrous. How on this earth is anyone supposed to come up with that? it's just guessing
@ronakshah725
@ronakshah725 2 ай бұрын
You don’t
@aayushpagare9366
@aayushpagare9366 Жыл бұрын
Once he draws a recursion tree i can write a recursive code
@amitupadhyay6511
@amitupadhyay6511 2 жыл бұрын
sorting is the key lol
@jogendarmahto3048
@jogendarmahto3048 2 жыл бұрын
🔥🔥🔥
@AmarjeetKumar-en1gk
@AmarjeetKumar-en1gk Жыл бұрын
I am Stupid
@soumikkarmakar7433
@soumikkarmakar7433 2 жыл бұрын
for(int i=2;i
@soumikkarmakar7433
@soumikkarmakar7433 2 жыл бұрын
man!!! i wasted like 3-4 hours and the mistake was i didn't put == inside the if statement and it didn't even show an error.
@bidishadas832
@bidishadas832 5 ай бұрын
Can you please share your complete code? I am getting 47 where it should be 37
@soumikkarmakar7433
@soumikkarmakar7433 5 ай бұрын
@@bidishadas832 Here you go. It isn't concise tho because I was a noob back then. class Solution{ public: int deleteAndEarn(vector& nums){ if(nums.size()==1) return nums[0]; if(nums.size()==2){ sort(nums.begin(),nums.end()); if(nums[0]+2>=nums[1]) return (nums[0]+nums[1]); else if(nums[0]==nums[1]) return 2*nums[0]; else if(nums[0]+1==nums[1]) return nums[1]; } map m; for(int i=0;i
Longest Increasing Path in a Matrix - Leetcode 329
17:45
NeetCode
Рет қаралды 65 М.
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
Каха и лужа  #непосредственнокаха
00:15
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 15 МЛН
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 8 МЛН
Find K Closest Elements - Leetcode 658 - Python
18:21
NeetCode
Рет қаралды 75 М.
Coin Change - Dynamic Programming Bottom Up - Leetcode 322
19:23
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Open the Lock - Leetcode 752 - Python
14:22
NeetCode
Рет қаралды 41 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 577 М.
Implement Trie (Prefix Tree) - Leetcode 208
18:56
NeetCode
Рет қаралды 212 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 403 М.
Edit Distance - Dynamic Programming - Leetcode 72 - Python
21:00
Каха и лужа  #непосредственнокаха
00:15