From 09699514714acc1c33b9c78fba40cc56f386cfab Mon Sep 17 00:00:00 2001 From: Eat-Swap Date: Sat, 21 May 2022 16:10:40 +0800 Subject: [PATCH] add: 220521 [cpp] --- cpp/2205/220521.cpp | 25 +++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220521.cpp diff --git a/cpp/2205/220521.cpp b/cpp/2205/220521.cpp new file mode 100644 index 0000000..cf3f35f --- /dev/null +++ b/cpp/2205/220521.cpp @@ -0,0 +1,25 @@ +#include +#include + +/** + * 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& 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]; + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 498f402..119a00a 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220520.cpp) +ADD_EXECUTABLE(2205 220521.cpp)