Тёмный

DP 21. Target Sum | DP on Subsequences 

take U forward
Подписаться 665 тыс.
Просмотров 162 тыс.
50% 1

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

 

9 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 590   
@sagarmittal1898
@sagarmittal1898 2 года назад
You will be further directed to DP-17 when you watch DP-18 . This series is very well designed !
@immortal6978
@immortal6978 10 месяцев назад
Its Recursion Go back and complete the part to get answer. Higher Problem to lower subproblems
@solitucedagar4298
@solitucedagar4298 2 месяца назад
@@immortal6978 🤣
@sarthakkumar856
@sarthakkumar856 2 года назад
I would have never thought, it can also be solved like this🤯. Thanks for this awesome playlist striver❤
@johncenakiwi
@johncenakiwi 2 года назад
I would have never thought of approaching this problem this way. Superb! Thanks for this. I have watched and solved all the videos till now in this series. Longest Common Subsequence and Longest Increasing Subsequence are much awaited.
@shushant1847
@shushant1847 3 месяца назад
as soon as you wrote those S1 and S2 i figured out ohh hell yess this can be done by that approach! Best one till now!
@subhajitdey135
@subhajitdey135 5 месяцев назад
Before watching your tutorial , I did solve this problem 🙂 using map(the naive recursive way and optimized with map). Here's the solution : int f(int i, int target, vector& nums, unordered_map& dp) { if (i == nums.size()) { if (target == 0) return 1; return 0; } string key = to_string(i) + "_" + to_string(target); if (dp.find(key) != dp.end()) return dp[key]; int ways = f(i + 1, target - nums[i], nums, dp) + f(i + 1, target + nums[i], nums, dp); dp[key] = ways; return ways; } int findTargetSumWays(vector& nums, int target) { unordered_map dp; return f(0, target, nums, dp); }
@stain5570
@stain5570 Месяц назад
chapgpt ahh solution
@parthh3963
@parthh3963 23 дня назад
does this work?
@paritoshsingh588
@paritoshsingh588 2 года назад
Omg i made this problem for Coding ninjas
@shubhdangwal4190
@shubhdangwal4190 5 месяцев назад
you baited me man kudos to u !
@vedanshbhardwaj6548
@vedanshbhardwaj6548 2 года назад
damn man ! been watching all the videos of this playlist from the beginning, this is the best video so far !! and you striver. You are not only teaching how to do DP, but indeed how to think DP ! Kudos to you for this playlist.
@gautham227
@gautham227 2 года назад
In this question the target value can also be negative so it'll be better to take the min of sum+ target and sum - target as the length of prev and curr just because of the fact that, if target is negative then sum + target will be smaller than sum - target 😊
@anything_jara_hatke5237
@anything_jara_hatke5237 2 года назад
Completed till here, please upload more videos. Thank you for such a quality content.
@vaishnavigour5777
@vaishnavigour5777 2 года назад
for 2:30 to 2:50 you said exactly what everyone was thinking 😂😂
@nilimsankarbora5911
@nilimsankarbora5911 8 месяцев назад
Damn man ! been watching all the videos of this playlist from the beginning, this is the best video so far !! and you Striver. You are not only teaching how to do DP, but indeed how to think DP ! Kudos to you for this playlist.💌💌💌💌
@akshayaashok8794
@akshayaashok8794 Месяц назад
😮😮😮Watching this solution made me go waahh!!!! Thank you bhaiya!!
@sandeeperukulla498
@sandeeperukulla498 2 года назад
wow!!!!! awesome explanation, you are gods gift to the coding community🙏🙏🙏🙏🙏
@kazuma0803
@kazuma0803 2 года назад
Once again I was able to solve it on my own. Thank you Striver
@omop5922
@omop5922 Год назад
Target is negative here too. Run time error nhi dega?
@rohanmaurya4060
@rohanmaurya4060 3 дня назад
All dp solutions will fail for negative target
@harihardhik3293
@harihardhik3293 26 дней назад
Sir amazing intuition really opened my eyes
@AlokLaha-te2pd
@AlokLaha-te2pd 2 месяца назад
Thanks striver. I solved this using normal recursion but you helped me to recognise the pattern of this problem.
@chandantaneja6388
@chandantaneja6388 2 года назад
What will be the base case for target as there target can be -1000 to 1000 so how he handles the negative values
@Rohit-zk1lz
@Rohit-zk1lz Год назад
plus minus way. int solve(int ind,int target,vector &nums,map &dp) { if(ind < 0) { //either you achieve the target or you don't achieve the target if(target == 0) return 1; else return 0; } if(dp.find({ind, target}) != dp.end()) { return dp[{ind,target}]; } int plus = 0; int minus = 0; plus = solve(ind-1,target + nums[ind],nums,dp); minus = solve(ind - 1, target - nums[ind],nums,dp); return dp[{ind, target}] = plus + minus; } int findTargetSumWays(vector& nums, int target) { map dp; return solve(nums.size()-1,target,nums,dp); }
@samnoitall9799
@samnoitall9799 5 месяцев назад
if we use a vector instead of a map it doesnt work can you please tell why
@dhruvmohata7528
@dhruvmohata7528 4 месяца назад
@@samnoitall9799 because we have a negative values so we can't put them in vector as we cant have -ve indexes
@priyanshkumar17
@priyanshkumar17 2 месяца назад
​@@samnoitall9799 negative values
@AyushRaj-nt3ot
@AyushRaj-nt3ot Месяц назад
@@samnoitall9799 here's your solution using vector class Solution { public: int recur(int ind, int tgt, vector&nums, vector&dp,int sum){ if(ind
@madhu_mohanreddy
@madhu_mohanreddy 10 дней назад
@@AyushRaj-nt3ot W
@sculptandstyle_
@sculptandstyle_ 2 года назад
I was just studying from ur previous videos and got new♥️
@hashcodez757
@hashcodez757 Месяц назад
"UNDERSTOOD BHAIYA"
@prabhakaran5542
@prabhakaran5542 6 месяцев назад
Understood ❤
@namanchandak1532
@namanchandak1532 Год назад
this is also solution for the same class Solution { public: int sol(vector& nums, int target,int ind,int curr) { if(ind==0) { if(target==curr && nums[0]==0) return 2; if(target==curr+nums[0] || target==curr-nums[0] ) return 1; return 0; } int pos=sol(nums,target,ind-1,curr+nums[ind]); int neg=sol(nums,target,ind-1,curr-nums[ind]); return pos+neg; } int findTargetSumWays(vector& nums, int target) { int n=nums.size(); // vector return sol(nums,target,n-1,0); } };
@aayushbisht4307
@aayushbisht4307 Год назад
thanks man! I was missing this return condition
@shivasai7707
@shivasai7707 Год назад
@@aayushbisht4307 bruh what will be memorization matrix size
@naveenramadheni2482
@naveenramadheni2482 Год назад
@@shivasai7707 May be memorization is not possible becoz curr will go to negative in some recursive calls
@GoldyAwad
@GoldyAwad Год назад
@@naveenramadheni2482 We can memoize it using unordered_map, instead of a 2d vector, use vector
@naveenramadheni2482
@naveenramadheni2482 Год назад
@@GoldyAwad Bro that idea is really good bro. (now got it that how to memoize -ve indexes)
@harshthakur2218
@harshthakur2218 3 месяца назад
s1-s2=target , man striver is the guyyy!!!!
@kathanvakharia
@kathanvakharia Год назад
Understood...Completed (21/56)
@akhildonthula6160
@akhildonthula6160 11 месяцев назад
I never thought I could solve a dp problem :)...thanks legend!
@Nikkhil492
@Nikkhil492 Год назад
I've alternate solution for this, just instead of take/nottake, do +, - #include int solve(int ind, int target, vector& arr){ if(ind==0){ if(arr[0]==target || arr[0]==-target) return 1; else return 0; } int pos=solve(ind-1, target-arr[ind], arr); int neg=solve(ind-1, target+arr[ind], arr); int var=pos+neg; return var; } int targetSum(int n, int target, vector& arr) { // Write your code here. return solve(n-1, target, arr); }
@monujgogoi1866
@monujgogoi1866 Год назад
it doesn't work for large test cases
@rishabhgupta9846
@rishabhgupta9846 Год назад
Damn Damn Damn striver ,was not able to think that this question can be solved using this way.Mapping of problems is very much important.
@anshul5533
@anshul5533 2 года назад
Striver bhaiya i was nnot able to comment in last vedio. I was saying that in DP-20 we can do further optimizations too as we have done in 0/1 knapsack by just using a single array reducing the SC to O(target+1)
@takeUforward
@takeUforward 2 года назад
Yes.
@arindamdongre4143
@arindamdongre4143 Год назад
how?
@arindamdongre4143
@arindamdongre4143 Год назад
I am gettig wrong answer by using only one array
@tasneemayham974
@tasneemayham974 11 месяцев назад
BEST TEACHER EVERRRRR!!!
@manyagarg-xz2fr
@manyagarg-xz2fr Год назад
I solved this problem yesterday and by considering +,- combinations, definitely had space complexity problems. This video gave me a different direction to think this same problem. Thank you!
@gauravpoharkar9797
@gauravpoharkar9797 Год назад
give me recusion solution bro Im unable to do it .
@ok-jg9jb
@ok-jg9jb Год назад
@@gauravpoharkar9797 f(ind, tar) { if(ind==0) if(tar+a[0]==0||tar-a[0]==0) Return 1 Else Retun 0 Plus=f(ind-1, a[ind]+tar) Minus=f(ind-1, a[ind]-tar) Return plus+minus
@siddharth892
@siddharth892 7 месяцев назад
@@gauravpoharkar9797 For the recursion + Memoisation I think a better way would be to use a dictionary rather than the array because of the negative indexes specially in python since It will start indexing from the back.
@lapimpale
@lapimpale 2 года назад
Bro, this time understood the ""problem"" more clearly than solution :D. I read somewhere that understanding the problem well is like problem half solved. You are explaining same in multiple ways. I remember the same incidence from Codestudio contest from last week.
@ka.patelhimeyatulkumar19ba58
@ka.patelhimeyatulkumar19ba58 2 года назад
bhaiyaa aap hai naa aditya verma ka dekhiye acche se samjaya hai. mast hai.
@lapimpale
@lapimpale 2 года назад
@@ka.patelhimeyatulkumar19ba58 free me ad? Me Jo bola wo samajho pehle fir advice dena
@user-is6ky7pp2n
@user-is6ky7pp2n 21 день назад
Understood !! Your are great
@kittupromax
@kittupromax 5 месяцев назад
Understood!
@UECAshutoshKumar
@UECAshutoshKumar 2 месяца назад
Thank You Understood!
@souravkumar-eb1wz
@souravkumar-eb1wz 2 года назад
Understood bro ✌✌ and once again the power of observation is shown 🤩🔥🔥
@Piyush-yp2po
@Piyush-yp2po 10 месяцев назад
negative positive soln f(idx, tar): //base case:- if(idx==0) if(arr(idx)+tar == 0 || arr(idx)-tar == 0) return 1; // logic and recursive call int neg = f(idx-1, tar - (-arr(i))); int pos = f(idx-1, tar - arr(i)); return neg+pos; Time complexity: O(2^N) as every element has 2 options either positive or negative
@JoyBoy_013
@JoyBoy_013 2 месяца назад
Understood
@sanketkale1851
@sanketkale1851 2 года назад
understood bhai , thank you soo much for this entire dp series and all other dsa series as well;
@Parthj426
@Parthj426 2 месяца назад
UNDERSTOOOOOOD.
@abhijitnag8161
@abhijitnag8161 Год назад
just fantastic. really getting enjoyed and feeling interested in coding and problem solving after your vidoes and approach sir. Thank you sir
@SelvaArumugam-xq5mf
@SelvaArumugam-xq5mf 8 месяцев назад
The thought process is great bruh
@himanshuagrawal8012
@himanshuagrawal8012 2 года назад
Before watching this video I was solving through + - method...It took hell lot of space to tabulate.......After watching video...I was shocked It was the same as S1-s2 = d ... #UNDERSTOOD
@vinayakgandhi5690
@vinayakgandhi5690 2 года назад
I think it is not even possible to memoize or tabulate in that way because the range of target is not fixed due to possibility of negative integer, unless you take the max possible sum space on either sides. Please correct me if I am wrong.
@himanshuagrawal8012
@himanshuagrawal8012 2 года назад
@@vinayakgandhi5690 Yes I was taking range as max positive and max negative
@mrinmoyhalder7293
@mrinmoyhalder7293 2 года назад
@@vinayakgandhi5690 we can also use map instead of 2d matrix
@codecrafts5263
@codecrafts5263 8 месяцев назад
Very nice study approaches. Thank you for this.
@anubhavsethi6623
@anubhavsethi6623 9 месяцев назад
target = abs(target) can be used to solve it using iterative dp.
@NARUTOUZUMAKI-bk4nx
@NARUTOUZUMAKI-bk4nx 9 месяцев назад
Thanks for the suggestion
@googleit2490
@googleit2490 10 месяцев назад
Revision done. Was confused in the beginning: I had falsely assumed elements to be negative and thus took time to get to the point. Nov'13, 2023 05:25 pm
@dakshlakhotiya4275
@dakshlakhotiya4275 2 года назад
Please make a vedio on binary lifting algorithm for kth ancestor in BT. Not much is available on RU-vid. Thanks in advance. 🙏🙏
@yeswanthh5068
@yeswanthh5068 2 года назад
Understood
@r_uhi05
@r_uhi05 10 дней назад
Understood!👍
@sahilbadkul7130
@sahilbadkul7130 2 года назад
You're genius man. Understood very well.
@YVGamers
@YVGamers Год назад
understood
@mahtoji8935
@mahtoji8935 10 месяцев назад
chumeshwari explanation bhayia 🥰🥰🥰
@026harshagarwal9
@026harshagarwal9 2 года назад
Striver bhaiya "Maximum profit in Job Scheduling" waala question explain kijiyegaa na and DP+Binary Search type waale questions explain kijiyegaa naa. Thanks for DP series!!
@GandharveSolanki
@GandharveSolanki 2 года назад
below is the solution for the problem here we either assign negative or positive to a number so the target can become negative so we use map to store negative indices. int solution(int n,int target,vector&arr,map&dp) { if(n==0) { if(target==0&&arr[0]==0) return 2; if(arr[0]+target==0) return 1; if(arr[0]-target==0) return 1; return 0; } if(dp.find({n,target})!=dp.end()) return dp[{n,target}]; int pos=solution(n-1,target-arr[n],arr,dp); int neg=solution(n-1,target+arr[n],arr,dp); return dp[{n,target}]=pos+neg; } int findTargetSumWays(vector& nums, int target) { map dp; int n=nums.size(); return solution(n-1,target,nums,dp); } if any doubt you can ask in comment.
@gurvindersingh136
@gurvindersingh136 2 года назад
can you provide the tabulation apporach for the same.
@UEEVijaykumar
@UEEVijaykumar 8 месяцев назад
can we use unorderedmap instead of map?
@cinime
@cinime 2 года назад
Understood! I appreciate for your amazing explanation!!
@original_gangsta_
@original_gangsta_ 2 года назад
Understood .. 💯💯💯
@sarthakragwan5248
@sarthakragwan5248 2 года назад
Pure genius 👌
@ranasauravsingh
@ranasauravsingh 2 года назад
UNDERSTOOD... ! Thanks striver for the video... :)
@GungunRamchandani
@GungunRamchandani 2 месяца назад
MindBlowingggggg
@anuragpandey8165
@anuragpandey8165 2 года назад
I used positive negative approach and memoized using map [ACCEPTED]
@ShubhamKumar-vp4pu
@ShubhamKumar-vp4pu 2 года назад
i am also using same approach but getting wrong answer, can you give your code
@anuragpandey8165
@anuragpandey8165 2 года назад
@@ShubhamKumar-vp4pu int solve(int ind, int target, vector &arr, map &mp) { if(ind == 0) { if(target + arr[0] == 0 && target - arr[0] == 0) return 2; if(target + arr[0] == 0 || target - arr[0] == 0) return 1; return 0; } if(mp.find(make_pair(ind, target)) != mp.end()) return mp[{ind, target}]; int neg = solve(ind-1, target+arr[ind], arr, mp); int pos = solve(ind-1, target-arr[ind], arr, mp); return mp[{ind, target}] = neg + pos; } int targetSum(int n, int target, vector& arr) { // Write your code here. //vector dp(n, vector(target+1, -1)); //unordered_map mp; map mp; return solve(n-1, target, arr, mp); }
@akashreddy7860
@akashreddy7860 5 месяцев назад
Tabulation is not coming
@venkateshvenky2880
@venkateshvenky2880 2 года назад
#understood
@adityavarma1334
@adityavarma1334 2 года назад
love you Striver❤❤
@Harsh-jc2bz
@Harsh-jc2bz День назад
understood
@shantipriya370
@shantipriya370 10 месяцев назад
understood at its best..
@theresilientpianist7114
@theresilientpianist7114 2 месяца назад
Superb yarrrrrrrr
@AbhishekKumar-cv1dh
@AbhishekKumar-cv1dh 10 месяцев назад
Understoood so damn well 🔥🔥🔥🔥
@kunalgupta343
@kunalgupta343 Месяц назад
Will this solution work for negative targets too?
@ayushpatel4475
@ayushpatel4475 Год назад
its speechless🤐
@Hrushi_2000
@Hrushi_2000 11 месяцев назад
Understood. Thankyou Sir
@samyakagarwal1777
@samyakagarwal1777 Год назад
understood , you are the best teacher
@crazyduniya128
@crazyduniya128 Год назад
me come back back after solving throughh recursion > memorization > tabulation > space optimization.... and then look at the video🤯🤯
@saumilaggarwal5233
@saumilaggarwal5233 10 месяцев назад
did it on my own, thnx
@preetisahani5054
@preetisahani5054 11 месяцев назад
Understood very well
@asyedmohammedtaha3819
@asyedmohammedtaha3819 2 месяца назад
genius !!!
@farhattasnim8856
@farhattasnim8856 3 месяца назад
understood!!😀😀😀
@NARUTOUZUMAKI-bk4nx
@NARUTOUZUMAKI-bk4nx 9 месяцев назад
Understoood
@DevashishJose
@DevashishJose 8 месяцев назад
understood thank you so much
@talkswithprabh5374
@talkswithprabh5374 18 дней назад
This problem shows never get afraid of weird language of question - try identifying patterns
@ugthesep5706
@ugthesep5706 11 дней назад
I don't think the language of the problem was weird it was pretty straight forward
@nookalareshwanth1785
@nookalareshwanth1785 3 месяца назад
Big Brain...
@abhishekm385
@abhishekm385 2 месяца назад
understood!!
@chanchalroy3417
@chanchalroy3417 8 месяцев назад
Incredible 😮
@freetechinfo2024
@freetechinfo2024 Год назад
Thanku
@LowkeyCoder
@LowkeyCoder 9 месяцев назад
"Understood!"
@patel-nc8di
@patel-nc8di Год назад
kya playlist banayi hai yaar striver bhaiya, reallly very powerfull
@iEntertainmentFunShorts
@iEntertainmentFunShorts 2 года назад
US ❤️ quality 👌
@naazfilz
@naazfilz 26 дней назад
Just Lit
@lakeshkumar1252
@lakeshkumar1252 Год назад
UNDERSTOOD
@vivekreddy820
@vivekreddy820 8 месяцев назад
amazing understood
@shashankgaur1718
@shashankgaur1718 Год назад
Understood.
@samsmith3961
@samsmith3961 8 месяцев назад
for edge cases like: {1,0}, {0,1}, {0,0,0,0,0,0,0}: #include using namespace std; class Solution { int helperSO(vector &arr, int target) { vector prev(target + 1, 0), curr = prev; curr[0] = prev[0] = 1; if (arr[0]
@deepakt4737
@deepakt4737 11 месяцев назад
solved the problem using recursive approach , just added extra parameter p to maintain the path sum i.e., p+arr[i] and p-arr[i]. and in base condition just check if p is equal to target or not; class Solution { public int findTargetSumWays(int[] arr, int target) { int n = arr.length,k=target; int p=0; return find(n-1,k,p,arr); } int find(int n,int k,int p,int[] arr){ if(n==-1) return (k==p)?1:0; int left= 0,right=0; right = find(n-1,k,p+arr[n],arr); left = find(n-1,k,p-arr[n],arr); return left+right; } }
@ayushgautam169
@ayushgautam169 Год назад
understood striver!!
@MukeshKumar-cc3uh
@MukeshKumar-cc3uh 6 месяцев назад
Nice! Understood ❤.
@rishr_
@rishr_ Месяц назад
i solved it using +,- approach where i biased the -ve sm with some extra space, defintely a overengineered solution, if only i had realized the pattern of previous question 🥲
@VikashYadav-px8ei
@VikashYadav-px8ei Год назад
Understood 🎉
@techsavy5669
@techsavy5669 Год назад
Not-understood.
@rishavkumar2182
@rishavkumar2182 2 года назад
"UNDERSTOOD"
@immortal6978
@immortal6978 10 месяцев назад
Understood !!'
@subhajyotibhowmik9192
@subhajyotibhowmik9192 Год назад
Understood ❤
@bhavyagautam8036
@bhavyagautam8036 Год назад
Understood!!