add: 220521 [cpp]
This commit is contained in:
parent
77b7de421a
commit
0969951471
|
|
@ -0,0 +1,25 @@
|
|||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
/**
|
||||
* 322. Coin Change
|
||||
* You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
|
||||
* Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
|
||||
* You may assume that you have an infinite number of each kind of coin.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int coinChange(const std::vector<int>& coins, int amount) {
|
||||
if (!amount)
|
||||
return 0;
|
||||
int dp[10005];
|
||||
std::memset(dp, 0x7F, sizeof dp);
|
||||
dp[0] = 0;
|
||||
for (int i = 0; i < 10005; ++i)
|
||||
for (int j : coins)
|
||||
if (i - j >= 0 && dp[i - j] < 0x7F7F7F7F)
|
||||
dp[i] = std::min(dp[i], dp[i - j] + 1);
|
||||
return dp[amount] >= 0x7F7F7F7F ? -1 : dp[amount];
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2205)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2205 220520.cpp)
|
||||
ADD_EXECUTABLE(2205 220521.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue