category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Minimizing Coins, Foundational, Knapsack.

vi arr;
vi dp;

int answer(int n) {
  if (n == 0) return 0;
  if (n <= 0) return INF;

  if (dp[n] != 0) return dp[n];
  int mn = INF;
  for (const auto& i: arr) {
    mn = min(answer(n - i) + 1, mn);
  }

  dp[n] = mn;
  return mn;
}

void solve() {
  int n; cin >> n;
  int target; cin >> target;

  arr = vi(n);
  dp = vi(target + 1, 0);
  for (auto& i: arr) cin >> i;

  auto k = answer(target);

  if (dp[target] == INF) cout << "-1\n";
  else cout << dp[target] << '\n';

}