AlgoPlus v0.1.0
Loading...
Searching...
No Matches
knapsack_1d.h
1#ifndef COIN_CHANGE_H
2#define COIN_CHANGE_H
3
4#ifdef __cplusplus
5#include <cstdint>
6#include <iostream>
7#include <vector>
8#endif
9
16int64_t min_cost(std::vector<int> arr, int N) {
17 int max_of = N + 1;
18 std::vector<int> dp(N + 1, max_of);
19 dp[0] = 0;
20 for (int i = 0; i <= N; i++) {
21 for (auto& x : arr) {
22 if (x <= i) {
23 dp[i] = std::min(dp[i], 1 + dp[i - x]);
24 }
25 }
26 }
27 return dp[N] > N ? -1 : dp[N];
28}
29
30#endif