题解 | #训练聪明的牛II#
训练聪明的牛II
https://www.nowcoder.com/practice/79f86360f2894f76b88d33b28a5d09b8
#include <climits> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param weights int整型vector * @param totalWeight int整型 * @return int整型 */ int res = INT_MAX; bool found = false; int minEatTimes(vector<int>& weights, int totalWeight) { if (totalWeight == 0) return 0; // write code here vector<int> dp(totalWeight+1, totalWeight+1); dp[0] = 0; for (int i = 1; i <= totalWeight; i++) { for (auto weight:weights) { if (weight <= i) { dp[i] = min(dp[i], dp[i-weight]+1); } } } return dp[totalWeight] > totalWeight ? -1 : dp[totalWeight]; } };