class Solution{
public:
bool solve(vector<int>&arr, int sum, int curr, int idx, vector<vector<int>>& dp)
{
if(curr==sum) return true;
if(curr>sum||idx==arr.size()) return false;
if(dp[curr][idx]!=-1) return dp[curr][idx];
bool ck=false;
ck=ck|solve(arr, sum, curr+arr[idx], idx+1, dp);
ck=ck|solve(arr, sum, curr, idx+1, dp);
return dp[curr][idx]=ck;
}
bool isSubsetSum(vector<int>arr, int sum){
// code here
vector<vector<int>> dp(100000, vector<int> (arr.size(), -1));
return solve(arr, sum, 0, 0, dp);
}
};
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter