494. Target Sum | Leetcode Daily Challenge

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

Комментарии • 2

  • @touhidiutcse
    @touhidiutcse 21 день назад

    Thanks so much for covering recursive approach alongside optimizing it by memoization & dynamic programming, Wow!

  • @newbie6449
    @newbie6449 21 день назад +1

    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);
    }
    };