Размер видео: 1280 X 720853 X 480640 X 360
Показать панель управления
Автовоспроизведение
Автоповтор
Thanks so much for covering recursive approach alongside optimizing it by memoization & dynamic programming, Wow!
my recusive solution class Solution { int tryAllWays(int index,int &cnt,vector& nums,int sum,int target) { if(index==nums.size()) { if(sum==target) { cnt+=1; return cnt; } return cnt; } // sum+=nums[index]; tryAllWays(index+1,cnt,nums,sum+nums[index],target); tryAllWays(index+1,cnt,nums,sum-nums[index],target); return cnt; }public: int findTargetSumWays(vector& nums, int target) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int cnt=0; return tryAllWays(0,cnt,nums,0,target); }};
Thanks so much for covering recursive approach alongside optimizing it by memoization & dynamic programming, Wow!
my recusive solution class Solution
{
int tryAllWays(int index,int &cnt,vector& nums,int sum,int target)
{
if(index==nums.size())
{
if(sum==target)
{
cnt+=1;
return cnt;
}
return cnt;
}
// sum+=nums[index];
tryAllWays(index+1,cnt,nums,sum+nums[index],target);
tryAllWays(index+1,cnt,nums,sum-nums[index],target);
return cnt;
}
public:
int findTargetSumWays(vector& nums, int target)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int cnt=0;
return tryAllWays(0,cnt,nums,0,target);
}
};