Recursive DP solution to Coin Combinations II CSES giving TLE

Revision en1, by discontinuous, 2025-03-21 20:16:17

I tried solving this problem of CSES Problemset: Coin Combinations II

This is the solution I came up with using Recursive DP :-

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define pb push_back

const ll MOD = 1e9 + 7;
int n, k, a, b, c, d, x, y;

vector<int> arr(101);
vector<vector<int>> dp(1000001, vector<int>(101, -1));

int ways(int target, int index) {
	if(target == 0) return 1;
	if(index == n || target < 0) return 0;

	if(dp[target][index] != -1) {
		// cout << target << ' ' << index << "\n";
		return dp[target][index];
	}
	else dp[target][index] = ((ways(target - arr[index], index))%MOD + (ways(target, index+1))%MOD)%MOD;

	return dp[target][index];
}

int main() {
    ios::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);
 
	cin >> n >> k;
	for(int j = 0; j<n; j++) cin >> arr[j];

	cout << ways(k, 0);
 
	return 0;
}

Most of the tutorials used Iterative DP for the solution but I am not able to see why this recursive DP solution won't work. This solution gives TLE on 7/15 test cases.

Can someone please help me with this

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English discontinuous 2025-03-21 20:16:17 1239 Initial revision (published)