π One-stop destination for all your technical interview Preparation π
dp[n-1][cost]
OR prev[cost]
.long countWaysToMakeChange(int* den, int n, int x)
{
vector<long> prev(x + 1, 0), curr(x + 1, 0);
for (int T = 0; T <= x; T++) {
prev[T] = (T % den[0] == 0); // changed here
}
for (int i = 1; i < n; i++) {
for (int T = 0; T <= x; T++) {
long notTake = prev[T];
long take = 0;
if (den[i] <= T) take = curr[T - den[i]]; // changed here
curr[T] = take + notTake; // changed here
}
prev = curr;
}
return prev[x]; // changed here
}