class Solution {
public:
/**
* 最少货币数
* @param arr int整型vector the array
* @param aim int整型 the target
* @return int整型
*/
int minMoney(vector<int>& arr, int aim) {
int len = arr.size();
if (len == 0) {
return -1;
}
if (aim == 0) {
return 0;
}
sort(arr.begin(), arr.end());
if (aim < arr[0]) {
return -1;
}
int tempMax = max(arr[len - 1], aim);
vector<int>dp(tempMax + 1, INT_MAX);
for (int i = 0; i < len; i++) {
dp[arr[i]] = 1;
}
for (int i = 0; i <= aim; i++) {
if (dp[i] == 1) {
continue;
}
for (int j = 0; j < len; j++) {
if (i >= arr[j]) {
dp[i] = min(dp[i-arr[j]] + 1,dp[i]);
}
}
}
return dp[aim] == INT_MAX ? -1 : dp[aim];
// write code here
}
};