id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n priority_queue<pair<int,int>>pq;\n map<int,int>mp;\n for(int i=0;i<arr.size();i++)\n {\n mp[arr[i]]++;\n }\n for(auto x:mp)\n {\n pq.push({x.second,x.first});\n }\n int x=n;\n vector<int>v;\n while(x>(n/2))\n {\n v.push_back(pq.top().second);\n x=x-(pq.top().first);\n pq.pop();\n }\n return v.size();\n }\n};", "memory": "88356" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n unordered_map<int,int>mp;\n for(int i=0;i<arr.size();i++){\n mp[arr[i]]++;\n }\n vector<pair<int,int>>v;\n for(auto it:mp){\n v.push_back({it.second,it.first});\n }\n sort(v.rbegin(),v.rend());\n int i=0,cnt=0,sum=0;\n while(i<v.size()){\n sum=sum+v[i].first;\n cnt++;\n if(sum>=n/2){\n return cnt;\n }\n i++;\n }\n return -1;\n }\n};", "memory": "88356" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<long,long>fre;\n long n=arr.size();\n\n for(auto& ele:arr){\n fre[ele]++;\n }\n\n priority_queue<long>p;\n \n for(auto& ele:fre){\n p.push(ele.second);\n }\n\n int ct=0;\n long sum=0;\n while(!p.empty()){\n\n sum=sum+p.top();\n p.pop();\n ct++;\n \n if(sum >= n/2){\n break;\n }\n }\n\n return ct;\n }\n};", "memory": "88743" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <unordered_map>\n#include <queue>\n\nclass Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n std::unordered_map<int, size_t> counter;\n for (auto el: arr)\n counter[el]++;\n \n std::priority_queue<size_t> el_counts;\n for (auto [el, counter]: counter)\n el_counts.push(counter);\n\n int to_remove = 0;\n size_t removed_sz = 0;\n while (removed_sz < arr.size() / 2) {\n removed_sz += el_counts.top();\n el_counts.pop();\n to_remove++;\n }\n\n return to_remove;\n }\n};", "memory": "89131" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#pragma GCC target(\"avx, mmx, sse2, sse3, sse4\")\n\nauto disableSync = [](void) noexcept -> int\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution final\n{\npublic:\n int minSetSize(const std::vector<int>& nums) const noexcept\n {\n std::unordered_map<int, size_t> counter;\n for (const int num : nums)\n ++counter[num];\n std::vector<size_t> counts;\n counts.reserve(counter.size() + 4);\n for (const auto& [_, count] : counter)\n counts.push_back(count);\n std::sort(counts.rbegin(), counts.rend());\n const size_t halfSize = nums.size() >> 1;\n int nbRemoved = 0, nbSets = 1;\n for (auto it = counts.cbegin(); true; ++it, ++nbSets)\n if ((nbRemoved += *it) >= halfSize)\n return nbSets;\n return INT_MAX; \n }\n};", "memory": "89518" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int dp[amount+1];\n for(int i = 1; i <= amount; i++)dp[i] = 1e9;\n dp[0] = 0;\n for(int i = 1; i <= amount; i++){\n for(int x : coins){\n if(i >= x)dp[i] = min(dp[i], 1 + dp[i-x]);\n }\n }\n return ((dp[amount]>=1e9)?-1:dp[amount]);\n }\n};", "memory": "12026" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution\n{\npublic:\n int coinChange(vector<int> &coins, int amount)\n {\n int dp[amount + 1];\n dp[0] = 0;\n for (int i = 1; i <= amount; ++i)\n {\n dp[i] = amount + 1;\n for (int x: coins)\n {\n if (i - x < 0) continue;\n dp[i] = min(dp[i], dp[i - x] + 1);\n }\n }\n return dp[amount] == amount + 1 ? -1 : dp[amount];\n }\n};\n", "memory": "12026" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "#define ll long long\nvector<ll>coins_now;\nll n;\nll dp[100005];\n\nll func(ll koto)\n{\n //cout<<\"koto: \"<<koto<<endl;\n if(koto==n){return 0;}\n else if(koto>n){return 1e9;}\n if(dp[koto]!=-1)\n {\n return dp[koto];\n }\n ll koita=1e18;\n for(int i=0;i<coins_now.size();i++)\n {\n //cout<<\"paisa: \"<<coins_now[i]<<endl;\n koita=min(koita, 1+func(koto+coins_now[i]));\n }\n //cout<<\"koita: \"<<koita<<endl;\n return dp[koto]=koita;\n}\n\n\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n for(int i=0;i<coins.size();i++)\n {\n coins_now.push_back(coins[i]);\n }\n n=amount;\n for(int i=0;i<=n+5;i++)\n {\n dp[i]=-1;\n }\n ll now=func(0);\n coins_now.clear();\n if(now<0 || now>=1e9)\n return -1;\n return now;\n\n }\n};", "memory": "12880" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "#include <algorithm>\n#include <limits>\n#include <functional>\n#include <unordered_map>\n\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n const int MAX = 10001;\n int dp[MAX];\n std::fill(dp, dp + MAX, MAX);\n dp[0] = 0;\n \n for (int coin: coins) {\n for (int i = coin; i < amount+1; i++) {\n if (dp[i - coin] != MAX) {\n dp[i] = std::min(dp[i], 1 + dp[i - coin]);\n }\n }\n }\n \n return (dp[amount] != MAX) ? dp[amount] : -1;\n }\n};", "memory": "12880" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dp[10001][12];\n int func(vector<int>& coins, int amount,int i){\n int n = coins.size();\n if(amount == 0){\n return 0;\n }\n\n int ans = INT_MAX;\n\n if(i>=n || amount<0){\n return INT_MAX;\n }\n\n if(dp[amount][i] != -1){\n return dp[amount][i];\n }\n\n for(int j = i;j<n;j++){\n if(amount>=coins[j]){\n int val = func(coins,amount-coins[j],j);\n if(val != INT_MAX){\n ans = min(ans,val+1);\n }\n }\n }\n return dp[amount][i] = ans;\n }\n int coinChange(vector<int>& coins, int amount) {\n memset(dp,-1,sizeof(dp));\n int ans = func(coins,amount,0);\n if(ans == INT_MAX){\n return -1;\n }\n return ans;\n }\n};", "memory": "13734" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long int a[(unsigned long)1e4+1][13];\n long long int coinChange(vector<int>& coins, int amount) {\n for(int i=0;i<1e4+1;i++)\n {\n for(int j=0;j<13;j++)\n {\n a[i][j]=-1;\n }\n }\n for(int j=0;j<13;j++)\n {\n a[0][j]=0;\n }\n if(ans(coins, amount,coins.size()-1)>=INT_MAX) return -1;\n return ans(coins, amount,coins.size()-1);\n }\n long long int ans(vector<int>& coins, int amount,int n)\n {\n if(amount<0 || n<0) return INT_MAX;\n if(a[amount][n]!=-1) return a[amount][n];\n a[amount][n]=min(1+ans(coins,amount-coins[n],n),ans(coins,amount,n-1));\n return a[amount][n];\n }\n};", "memory": "13734" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int coinChange(vector<int> &coins, int amount) {\n if (!amount)\n return 0;\n\n vector<short> lefts_coin_cnt(amount + 1, -1);\n lefts_coin_cnt[amount] = 0;\n\n for (int coin_cnt = 1, new_left_cnt = 1; coin_cnt <= amount && new_left_cnt;\n ++coin_cnt) {\n for (int n = 0; n <= amount; ++n) { // huge loop\n if (lefts_coin_cnt[n] == coin_cnt - 1) {\n --new_left_cnt;\n for (auto coin : coins) {\n if (n == coin) {\n // cout << coin_cnt << \": \" << new_left_cnt << endl;\n return coin_cnt;\n } else if (n > coin && lefts_coin_cnt[n - coin] == -1) {\n lefts_coin_cnt[n - coin] = coin_cnt;\n ++new_left_cnt;\n // cout << coin_cnt << \": \" << n << \" \" << n - coin << endl;\n }\n }\n }\n }\n }\n\n return -1;\n }\n\n int XcoinChange(vector<int> &coins, int amount) {\n /* 1 2 5\n * 11 | 10 9 6 | 5 8 7 4 5 1 | 3 0 (done)\n *\n * amount = x^a + y^b + ...\n */\n vector<int> lefts{amount};\n bitset<10000> is_left;\n\n for (int coin_cnt = 0; !lefts.empty(); ++coin_cnt) {\n vector<int> next_lefts;\n for (auto coin : coins) {\n if (amount / coin >= coin_cnt) {\n for (auto left : lefts) { // huge loop\n if (!left)\n return coin_cnt;\n else if (left >= coin && !is_left[left - coin]) {\n is_left[left - coin] = true;\n next_lefts.emplace_back(left - coin);\n }\n }\n }\n }\n\n lefts = std::move(next_lefts);\n }\n\n return -1;\n }\n};", "memory": "14588" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "short min(short a, short b) {\n return a < b ? a : b;\n}\n\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n vector<short> dp(amount + 1, SHRT_MAX);\n dp[0] = 0;\n // Time complexity O(N * M)\n // N -> amount of coins\n // M -> designated amount\n for (int i = 1; i <= amount; i++) {\n for (int j = 0; j < coins.size(); j++) {\n if (i >= coins[j] && dp[i - coins[j]] != SHRT_MAX) {\n dp[i] = min(dp[i - coins[j]] + 1, dp[i]);\n }\n }\n }\n return dp[amount] == SHRT_MAX ? -1 : dp[amount];\n }\n};", "memory": "14588" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> arr;\n int n ;\n int target;\n int t[13][10001];\n int fun(int idx,long currSum){\n if (idx == n) return INT_MAX-1;\n if (currSum > target) return INT_MAX-1;\n if (currSum == target) return 0;\n if (t[idx][currSum] != -1) return t[idx][currSum];\n long take = 1 + fun(idx,currSum+arr[idx]);\n long take2 = 1 + fun(idx+1,currSum+arr[idx]);\n long skip = 0 + fun(idx+1,currSum);\n return t[idx][currSum]= min({take,skip,take2});\n }\n int coinChange(vector<int>& coins, int amount) {\n arr = coins;\n n = coins.size();\n target =amount;\n memset(t,-1,sizeof(t));\n long ans = fun(0,0);\n if (ans != INT_MAX-1) return ans;\n return -1;\n }\n};", "memory": "15441" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\nint n;\nint coin[12];\nint dp[12][10001];\n int rec(int i, int left){\n if(left<0) return 1e9;\n if(i==n){\n if(left==0) return 0;\n else return 1e9;\n }\n\n if(dp[i][left]!=-1) return dp[i][left];\n int ans=1e9;\n ans=min({ans,rec(i+1,left), rec(i,left-coin[i])+1});\n\n return dp[i][left]=ans;\n }\n int coinChange(vector<int>& coins, int amount) {\n n=coins.size();\n memset(dp,-1,sizeof(dp));\n for(int i=0; i<n; i++) coin[i]=coins[i];\n if(rec(0,amount)==1e9) return -1;\n return rec(0,amount);\n }\n};", "memory": "15441" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int m=amount+1;\n vector<int>dp(m,m);\n dp[0]=0;\n for(int i=1;i<m;i++){\n for(int j=0;j<coins.size();j++){\n if(coins[j]<=i){\n dp[i]=min(dp[i],dp[i-coins[j]]+1);\n }\n }\n }\n return dp[m-1]>m-1?-1:dp[m-1];\n }\n};", "memory": "16295" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n vector<int> dp(amount + 1, amount + 1);\n dp[0] = 0;\n \n for (int i = 1; i <= amount; i++) {\n for (int coin : coins) {\n if (coin <= i) {\n dp[i] = min(dp[i], dp[i - coin] + 1);\n }\n }\n }\n \n return dp[amount] > amount ? -1 : dp[amount];\n }\n};\n// LC rejects this but it has correct O time.\n/*using HashPair = decltype([](const pair<int, int>& pair) {\n int first = hash<int>{}(pair.first);\n int second = hash<int>{}(pair.second);\n return first ^ (second >> 1);\n});\nclass Solution {\nprivate:\n unordered_map<pair<int, int>, int, HashPair> memo;\npublic:\n int dp(vector<int>& coins, int n, int amount) {\n if(amount == 0) {\n return 0;\n } else if((amount < 0) || (n < 0)) {\n return -1;\n } else if(memo.find({n, amount}) != memo.end()) {\n return memo[{n, amount}];\n }\n\n int poss = amount / coins[n];\n int fewest = -1;\n for(int i = 0; i <= poss; i++) {\n int next = dp(coins, n - 1, amount - i * coins[n]);\n if(next >= 0) {\n if(fewest < 0) {\n fewest = i + next;\n } else {\n fewest = min(fewest, i + next);\n }\n }\n }\n\n memo[{n, amount}] = fewest;\n return fewest;\n }\n\n int coinChange(vector<int>& coins, int amount) {\n return dp(coins, coins.size() - 1, amount); \n }\n};*/", "memory": "16295" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solveusingTab(vector<int>& coins, int amount) {\n sort(coins.rbegin(), coins.rend());\n\n // Create a dp array of size amount + 1, initialized to INT_MAX\n vector<int> dp(amount + 1, INT_MAX);\n dp[0] = 0; // No coins are needed to make the amount 0\n\n // Iterate over all amounts from 1 to amount\n for (int i = 1; i <= amount; i++) {\n for (int coin : coins) {\n // Break early if the coin is larger than the current amount\n if (coin > i) continue;\n // Check for overflow and update dp array\n if (dp[i - coin] != INT_MAX) {\n dp[i] = min(dp[i], dp[i - coin] + 1);\n }\n }\n }\n\n // If dp[amount] is still INT_MAX, it means it's impossible to make up the amount\n return dp[amount] == INT_MAX ? -1 : dp[amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n int ans;\n ans = solveusingTab(coins, amount);\n return ans;\n }\n};", "memory": "17149" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solveusingTab(vector<int>& coins, int amount) {\n sort(coins.rbegin(), coins.rend());\n\n // Create a dp array of size amount + 1, initialized to INT_MAX\n vector<int> dp(amount + 1, INT_MAX);\n dp[0] = 0; // No coins are needed to make the amount 0\n\n // Iterate over all amounts from 1 to amount\n for (int i = 1; i <= amount; i++) {\n for (int coin : coins) {\n // Break early if the coin is larger than the current amount\n if (coin > i) continue;\n // Check for overflow and update dp array\n if (dp[i - coin] != INT_MAX) {\n dp[i] = min(dp[i], dp[i - coin] + 1);\n }\n }\n }\n\n // If dp[amount] is still INT_MAX, it means it's impossible to make up the amount\n return dp[amount] == INT_MAX ? -1 : dp[amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n int ans;\n ans = solveusingTab(coins, amount);\n return ans;\n }\n};", "memory": "17149" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n\n int findFewestByRecursion(vector<int>& coins, int amount){\n\n /// base case\n if(amount == 0) return 0;\n if( amount < 0) return INT_MAX;\n\n\n int mini = INT_MAX;\n for(int i = 0; i < coins.size(); i++){\n int ans = findFewestByRecursion(coins,amount-coins[i]);\n if(ans != INT_MAX){\n mini = min(mini, 1+ans);\n }\n }\n return mini;\n\n\n }\n int findFewestByMemo(vector<int>& coins, int amount,vector<int>&dp){\n\n /// base case\n if(amount == 0) return 0;\n if( amount < 0) return INT_MAX;\n\n // Check value present in DP \n if(dp[amount] != -1){\n return dp[amount];\n }\n\n int mini = INT_MAX;\n for(int i = 0; i < coins.size(); i++){\n int ans = findFewestByMemo(coins,amount-coins[i],dp);\n if(ans != INT_MAX){\n mini = min(mini, 1+ans);\n }\n }\n dp[amount]= mini;\n return dp[amount];\n\n\n }\n\n int coinChange(vector<int>& coins, int amount) {\n //if(amount == 0) return 0; \n vector<int> dp(amount+1, -1); // step1\n int ans = findFewestByMemo(coins,amount,dp);\n if(ans == INT_MAX)return -1;\n else return ans;\n }\n};", "memory": "18003" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int ans = INT_MAX;\n vector < int > memo;\n int ways(int amount, vector < int > &coins)\n {\n if(amount<0) return INT_MAX;\n if(amount==0)\n {\n return 0;\n }\n if(memo[amount]!=-1) return memo[amount];\n int min_coins = INT_MAX;\n for(int i = 0;i<coins.size();i++)\n {\n int result = ways(amount-coins[i],coins);\n if(result!=INT_MAX) min_coins = min(min_coins, result+1);\n }\n return memo[amount] = min_coins;\n }\n int coinChange(vector<int>& coins, int amount) {\n memo.resize(amount+1,-1);\n ans = ways( amount, coins);\n return ans==INT_MAX?-1:ans;\n }\n};", "memory": "18003" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "const int N = 15,M = 1e5;\nint mem[N][M];\nint n;\nclass Solution {\npublic:\n int dp(int ind,int amount,vector<int>& coins)\n {\n if(amount==0)\n return 0;\n int &ret = mem[ind][amount];\n if(~ret)return ret;\n ret = 1e9;\n for(int i=0;i<n;i++)\n {\n if(amount>=coins[ind])ret = min(ret,1+dp(ind,amount-coins[ind],coins));\n if(i+1<n)\n ret = min(ret,dp(i+1,amount,coins));\n }\n return ret;\n\n }\n int coinChange(vector<int>& coins, int amount) {\n n = coins.size();\n memset(mem,-1,sizeof mem);\n int ans = dp(0,amount,coins);\n return (ans>=1e9?-1:ans);\n }\n};", "memory": "18856" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int> &coins, int amount) {\n /* 1 2 5\n * 11 | 10 9 6 | 5 8 7 4 5 1 | 3 0 (done)\n *\n * amount = x^a + y^b + ...\n */\n if (!amount)\n return 0;\n\n vector<int> lefts{amount};\n bitset<10000> is_left;\n\n for (int coin_cnt = 0; !lefts.empty(); ++coin_cnt) {\n vector<int> next_lefts;\n for (auto coin : coins) {\n if (amount / coin > coin_cnt) {\n for (auto left : lefts) { // huge loop\n if (!left)\n return coin_cnt;\n else if (left == coin)\n return coin_cnt + 1;\n else if (left > coin && !is_left[left - coin]) {\n is_left[left - coin] = true;\n next_lefts.emplace_back(left - coin);\n }\n }\n }\n }\n\n lefts = std::move(next_lefts);\n }\n\n return -1;\n }\n};", "memory": "23979" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long int dp[100000][13];\n long long int helper(vector<int>& coins, int amount , long long int sum , int index)\n {\n if(index == coins.size() || sum > amount){\n return INT_MAX -1; \n }\n if(sum == amount){\n return 0; \n }\n if(dp[sum][index] !=-1) {\n return dp[sum][index];\n\n }\n \n long long int take =1+ helper(coins, amount , sum+coins[index] , index);\n long long int notake =0+ helper(coins, amount , sum , index+1);\n long long int mini = min(take,notake);\n dp[sum][index] =min( take , notake); \n return mini;\n \n }\n\n int coinChange(vector<int>& coins, int amount) {\n memset(dp,-1,sizeof(dp));\n long long int sum = 0;\n int index = 0;\n int ans = helper(coins , amount , sum , index );\n if(ans == INT_MAX-1){\n return -1;\n }\n \n return ans;\n }\n};", "memory": "23979" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n /* cal(amt, ind) = cal(amt, ind-1) | cal(amt-coins[ind], ind); */\n int n = coins.size();\n vector<vector<int>>dp(2, vector<int>(amount+1,amount+1));\n dp[1][0] = 0;\n for(int i = 0; i < n; i++)\n {\n for(int j = 0; j <= amount; j++)\n {\n dp[i%2][j] = dp[!(i%2)][j];\n if (j-coins[i] >= 0)\n dp[i%2][j] = min(dp[i%2][j], dp[i%2][j-coins[i]] + 1);\n }\n }\n if (dp[!(n%2)][amount] == amount+1)\n return -1;\n return dp[!(n%2)][amount];\n }\n};", "memory": "24833" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic://now lets solved this using 2d dp tabulation\n int coinChange(vector<int>& coins, int amount) {\n int n=coins.size();\n //lets declare our dp vector of 2d\n vector<vector<int>>dp(2,vector<int>(amount+1,INT_MAX)); //initializing with INT_MAX\n //now for 0th col setting every value as 0\n for(int i=0;i<=1;i++) //for 0th column\n {\n dp[i][0]=0;\n }\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=amount;j++)\n {\n if(j-coins[i-1]<0) dp[1][j]=dp[0][j]; //leave\n else\n {\n long long take=1LL+dp[1][j-coins[i-1]];\n long long leave=dp[0][j];\n dp[1][j]=min(take,leave);\n }\n }\n //now we have to copy the 1th row and paste it into 0th row\n for(int k=1;k<=amount;k++) //k=1 because we will not make any changes in the 0th col connected cell\n {\n dp[0][k]=dp[1][k];\n }\n }\n int ans=dp[1][amount]; //as size are (2)*(amount+1)\n if(ans==INT_MAX) return -1; //no valid combinations are present\n return ans;\n }\n};", "memory": "24833" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n queue<pair<int,int>> q;\n vector<int> vis(amount+1,0);\n if(amount == 0){\n return 0;\n }\n q.push({amount,0});\n vis[amount] = 1;\n while(!q.empty()){\n int nums = q.front().first;\n int steps = q.front().second;\n q.pop();\n for(int i=0; i<coins.size(); i++){\n if(nums-coins[i] == 0){\n return steps+1;\n }\n if(nums-coins[i] > 0 && !vis[nums-coins[i]] ){\n q.push({nums-coins[i],steps+1});\n vis[nums-coins[i]] = 1;\n }\n }\n }\n return -1;\n }\n};", "memory": "25686" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\n\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0){return 0;}\n sort(coins.begin(), coins.end());\n int ind = coins.size()-1;\n int minn = 1e9;\n queue<pair<pair<int,int>,int>> q;\n q.push({{amount,0},ind});\n int hash[10009];\n memset(hash,-1,sizeof(hash));\n\n while(!q.empty()){\n auto top = q.front();\n q.pop();\n int amt = top.first.first;\n int cnt = top.first.second;\n int ind = top.second;\n if(amt == 0){return cnt;}\n // if(amt<0){continue;}\n for(int i = ind; i>=0; i--){\n // cout<<\"here \"<<i<<endl;\n int newamt = amt-coins[i];\n if(newamt == 0){return cnt+1;}\n if(newamt>0 && hash[newamt]==-1){\n q.push({{newamt,cnt+1},i});\n hash[newamt] = 1;\n }\n }\n }\n return -1; \n }\n};", "memory": "25686" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "// class Solution {\n// public:\n// int coinChange(vector<int>& coins, int amount) {\n// const int optimal = GetOptimal(0, amount, coins);\n// return optimal == INT_MAX ? -1 : optimal;\n// }\n// private:\n// int GetOptimal(const int idx, const int rem_amount, const vector<int>& coins) {\n// if (rem_amount == 0) { return 0; }\n// if (rem_amount < 0 || idx == coins.size()) { return INT_MAX; }\n// if (auto search = dp_.find({idx, rem_amount}); search != dp_.end()) {\n// return search->second;\n// }\n// int min_here = INT_MAX;\n// const auto coin_selected = GetOptimal(idx, rem_amount - coins[idx], coins);\n// if (coin_selected < INT_MAX) {\n// min_here = std::min(min_here, coin_selected + 1);\n// }\n// min_here = std::min(min_here, GetOptimal(idx + 1, rem_amount, coins));\n\n// dp_.insert({{idx, rem_amount}, min_here});\n// return min_here;\n// }\n\n// std::map<std::tuple<int, int>, int> dp_;\n// };\n// // Min of selecting that coin and not selecting it.\n\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n vector<int> dp;\n vector<int> prev_dp;\n for (int i = 0; i <= amount; ++i) {\n dp.push_back(INT_MAX);\n }\n for (const int& coin : coins) {\n prev_dp = dp;\n dp.clear();\n dp.push_back(0);\n for (int amount_here = 1; amount_here <= amount; ++amount_here) {\n if (amount_here >= coin) {\n if (dp[amount_here - coin] < INT_MAX) {\n dp.push_back(std::min(dp[amount_here - coin] + 1, prev_dp[amount_here]));\n } else {\n dp.push_back(prev_dp[amount_here]);\n }\n } else {\n dp.push_back(prev_dp[amount_here]);\n }\n }\n }\n return dp[amount] == INT_MAX ? -1 : dp[amount];\n }\n};\n// Min of prev_dp[idx], dp[amount - coin] + 1", "memory": "26540" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<long> prev(amount+1, 1e9), curr(amount+1, 1e9); // Initialize DP table with large values\n prev[0]=0; curr[0]=0;\n\n for (int i = 1; i <= amount; i++) {\n if (i % coins[0] == 0) {\n prev[i] = i / coins[0]; // Minimum coins required using only the first coin type\n }\n }\n\n // Fill the DP table for the remaining coins\n for (int ind = 1; ind < n; ind++) {\n for (int target = 1; target <= amount; target++) {\n long notTaken = prev[target]; // Not taking the current coin\n\n long taken = 1e9; // Default to large value\n if (coins[ind] <= target) taken = 1 + curr[target - coins[ind]]; // Taking the current coin\n\n curr[target] = min(notTaken, taken); // Minimum of taking and not taking the coin\n }\n prev = curr;\n curr.resize(amount+1, 1e9);\n }\n\n // If the ans is still 1e9, it means it's not possible to form the amount\n long ans = prev[amount];\n return (ans >= 1e9) ? -1 : ans;\n }\n};", "memory": "27394" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long int miniCoinR(vector<int> &coins,int idx,int amount){\n if(amount==0){\n return 0;\n }\n if(idx==0){\n return INT_MAX;\n }\n if(coins[idx-1]>amount){\n return miniCoinR(coins,idx-1,amount);\n }\n return min(miniCoinR(coins,idx-1,amount),1+miniCoinR(coins,idx,amount-coins[idx-1]));\n }\n long long int miniCoinM(vector<int> &coins,int idx,int amount,vector<vector<int>> &dp){\n if(amount==0){\n return 0;\n }\n if(idx==0){\n return INT_MAX;\n }\n if(dp[idx][amount]!=-1){\n return dp[idx][amount];\n }\n if(coins[idx-1]>amount){\n return dp[idx][amount]=miniCoinM(coins,idx-1,amount,dp);\n }\n return dp[idx][amount]=min(miniCoinM(coins,idx-1,amount,dp),1+miniCoinM(coins,idx,amount-coins[idx-1],dp));\n }\n long long int miniCoinT(vector<int> &coins,int total){\n vector<vector<long long int>> dp(coins.size()+1,vector<long long int>(total+1,-1));\n for(int amount=0;amount<=total;amount++){\n dp[0][amount]=INT_MAX;\n }\n for(int idx=0;idx<=coins.size();idx++){\n dp[idx][0]=0;\n }\n for(int idx=1;idx<=coins.size();idx++){\n for(int amount=1;amount<=total;amount++){\n if(coins[idx-1]>amount){\n dp[idx][amount]=dp[idx-1][amount];\n }\n else{\n dp[idx][amount]=min(dp[idx-1][amount],1+dp[idx][amount-coins[idx-1]]);\n }\n }\n }\n return dp[coins.size()][total];\n }\n long long int miniCoinO(vector<int> &coins,int total){\n vector<long long int> dp(total+1,INT_MAX),temp(total+1,INT_MAX);\n dp[0]=0;\n temp[0]=0;\n for(int idx=1;idx<=coins.size();idx++){\n for(int amount=1;amount<=total;amount++){\n if(coins[idx-1]>amount){\n temp[amount]=dp[amount];\n }\n else{\n temp[amount]=min(dp[amount],1+temp[amount-coins[idx-1]]);\n }\n }\n dp=temp;\n }\n return dp[total];\n }\n int coinChange(vector<int>& coins, int amount) {\n if(amount==0){\n return 0;\n }\n // long long int val=miniCoinR(coins,coins.size(),amount);\n\n // vector<vector<int>> dp(coins.size()+1,vector<int>(amount+1,-1));\n // long long int val=miniCoinM(coins,coins.size(),amount,dp);\n\n // long long int val=miniCoinT(coins,amount);\n\n long long int val=miniCoinO(coins,amount);\n\n if(val>=INT_MAX){\n return -1;\n }\n return val;\n }\n};", "memory": "28248" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<long> prev(amount+1, 1e9), curr(amount+1, 1e9); // Initialize DP table with large values\n prev[0]=0; curr[0]=0;\n\n for (int i = 1; i <= amount; i++) {\n if (i % coins[0] == 0) {\n prev[i] = i / coins[0]; // Minimum coins required using only the first coin type\n }\n }\n\n // Fill the DP table for the remaining coins\n for (int ind = 1; ind < n; ind++) {\n for (int target = 1; target <= amount; target++) {\n long notTaken = prev[target]; // Not taking the current coin\n\n long taken = 1e9; // Default to large value\n if (coins[ind] <= target) taken = 1 + curr[target - coins[ind]]; // Taking the current coin\n\n curr[target] = min(notTaken, taken); // Minimum of taking and not taking the coin\n }\n prev = curr;\n }\n\n // If the ans is still 1e9, it means it's not possible to form the amount\n long ans = prev[amount];\n return (ans >= 1e9) ? -1 : ans;\n }\n};", "memory": "28248" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int f(int ind,int target, vector<int>& coins,vector<int> prev,vector<int> curr){\n int n=coins.size();\n \n for (int t = 0; t <= target; t++) {\n if (t % coins[0] == 0) {\n prev[t] = t / coins[0];\n }\n }\n \n for(int i=1;i<n;i++){\n for(int j=0;j<target+1;j++){\n \n int take=1e9;\n int nottake=prev[j];\n if(coins[i]<=j){\n take=1+curr[j-coins[i]];}\n curr[j]=min(take,nottake);\n \n\n }\n prev=curr;\n \n }\n return prev[target];\n \n\n }\n int coinChange(vector<int>& coins, int amount) {\n if(amount==0){return 0;}\n int n=coins.size();\n vector<int> prev(amount+1,1e9);\n vector<int> curr(amount+1,1e9);\n \n \n int ans= f(n-1,amount,coins,prev,curr);\n if(ans==1e9){return -1;}\n return ans;\n }\n};", "memory": "29101" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n deque<pair<int64_t,int64_t>> dq;\n dq.emplace_back(0LL,0);\n vector<bool> visit(amount+1);\n while (!dq.empty()) {\n auto [cur, step] = dq.front(); dq.pop_front();\n if (cur == amount) return step;\n for (auto c:coins) {\n if (cur+c <= amount && !visit[cur+c]) {\n visit[cur+c] = true;\n dq.emplace_back(cur+c, step+1);\n }\n }\n }\n return -1;\n }\n};", "memory": "29101" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& a, int k) \n {\n if(k==0)\n return 0;\n int n=a.size();\n sort(a.begin(),a.end());\n vector<long long int> dp(10005,INT_MAX);\n dp[0]=0;\n for(int i=0;i<10005;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(i<a[j])\n break;\n if(i==a[j])\n {\n dp[i]=1;\n break;\n }\n dp[i]=min(dp[i],(long long)1+dp[i-a[j]]);\n }\n } \n if(dp[k]==INT_MAX)\n return -1; \n return (int)dp[k];\n }\n};", "memory": "29955" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& a, int k) \n {\n if(k==0)\n return 0;\n int n=a.size();\n // sort(a.begin(),a.end());\n vector<long long int> dp(10005,INT_MAX);\n dp[0]=0;\n for(int i=0;i<10005;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(i<a[j])\n continue;\n if(i==a[j])\n {\n dp[i]=1;\n break;\n }\n dp[i]=min(dp[i],(long long)1+dp[i-a[j]]);\n }\n } \n if(dp[k]==INT_MAX)\n return -1; \n return (int)dp[k];\n }\n};", "memory": "29955" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution { \npublic:\n int coinChange(vector<int> &coins, int amount) \n {\n vector <int> prev(10001), now(10001);\n for(int am=0; am<=amount; am++)\n prev[am] = am%coins[0]==0 ? am/coins[0] : 1e9;\n for(int cur=1; cur<coins.size(); cur++)\n {\n for(int am=0; am<=amount; am++)\n {\n int doNotTakeCoin = 0 + prev[am];\n int takeCoin = INT_MAX;\n if(coins[cur] <= am) \n takeCoin = 1 + now[am-coins[cur]];\n now[am] = min(takeCoin, doNotTakeCoin);\n }\n prev = now;\n }\n return (prev[amount] >= 1e9) ? -1 : prev[amount];\n }\n};", "memory": "30809" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\n vector<long> dp;\npublic:\n Solution() {\n dp = vector<long>(10001, -1); \n }\npublic:\n int coinChange(vector<int>& coins, int amount) {\n //init(amount);\n long ans = numCoins(amount, coins);\n return (ans >= INT_MAX) ? -1 : ans;\n }\n\n long numCoins(int n, vector<int>& coins) {\n if (n == 0) {\n return 0;\n }\n\n if (dp[n - 1] != -1) {\n return dp[n - 1];\n }\n\n long ans = INT_MAX;\n for (int i = 0; i < coins.size(); ++i) {\n if ((n - coins[i]) >= 0) {\n ans = min(ans, numCoins(n - coins[i], coins) + 1);\n }\n }\n \n return dp[n - 1] = ans;\n }\n};", "memory": "30809" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0){\n return 0;\n }\n if(coins.size() == 1){\n if(amount % coins[0]){\n return -1;\n }\n else{\n return amount / coins[0];\n }\n }\n sort(coins.begin() , coins.end() , greater<int>());\n queue<pair<long , long>> q;\n int cnt = 1;\n vector<int> dp(amount + 1 , -1);\n dp[0] = 0;\n vector<int> possibleCoins;\n for(int i : coins){\n if(amount == i) return 1;\n if(amount > i){\n dp[i] = 1;\n q.push({i , i});\n possibleCoins.push_back(i);\n }\n }\n q.push({-1,-1});\n while(!q.empty()){\n while(q.front().first != -1){\n pair<long , long > curr = q.front();\n q.pop();\n for(int i : possibleCoins){\n int sum = curr.second + i;\n if( sum == amount) return cnt + 1;\n if(amount > sum && dp[sum] == -1){\n q.push({i , sum});\n dp[sum] = cnt + 1;\n }\n }\n }\n q.pop();\n if(!q.empty()){\n cnt++;\n q.push({-1,-1});\n }\n }\n return -1;\n }\n};", "memory": "31663" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0){\n return 0;\n }\n if(coins.size() == 1){\n if(amount % coins[0]){\n return -1;\n }\n else{\n return amount / coins[0];\n }\n }\n sort(coins.begin() , coins.end() , greater<int>());\n queue<pair<long , long>> q;\n int cnt = 1;\n vector<int> dp(amount + 1 , -1);\n dp[0] = 0;\n vector<int> possibleCoins;\n for(int i : coins){\n if(amount == i) return 1;\n if(amount > i){\n dp[i] = 1;\n q.push({i , i});\n possibleCoins.push_back(i);\n }\n }\n q.push({-1,-1});\n while(!q.empty()){\n while(q.front().first != -1){\n pair<long , long > curr = q.front();\n q.pop();\n for(int i : possibleCoins){\n int sum = curr.second + i;\n if( sum == amount) return cnt + 1;\n if(amount > sum && dp[sum] == -1){\n q.push({i , sum});\n dp[sum] = cnt + 1;\n }\n }\n }\n q.pop();\n if(!q.empty()){\n cnt++;\n q.push({-1,-1});\n }\n }\n return -1;\n }\n};", "memory": "32516" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if(amount == 0){\n return 0;\n }\n if(coins.size() == 1){\n if(amount % coins[0]){\n return -1;\n }\n else{\n return amount / coins[0];\n }\n }\n // sort(coins.begin() , coins.end() , greater<int>());\n queue<pair<long , long>> q;\n int cnt = 1;\n vector<int> dp(amount + 1 , -1);\n dp[0] = 0;\n vector<int> possibleCoins;\n for(int i : coins){\n if(amount == i) return 1;\n if(amount > i){\n dp[i] = 1;\n q.push({i , i});\n possibleCoins.push_back(i);\n }\n }\n q.push({-1,-1});\n while(!q.empty()){\n while(q.front().first != -1){\n pair<long , long > curr = q.front();\n q.pop();\n for(int i : possibleCoins){\n int sum = curr.second + i;\n if( sum == amount) return cnt + 1;\n if(amount > sum && dp[sum] == -1){\n q.push({i , sum});\n dp[sum] = cnt + 1;\n }\n }\n }\n q.pop();\n if(!q.empty()){\n cnt++;\n q.push({-1,-1});\n }\n }\n return -1;\n }\n};", "memory": "32516" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "// class Solution {\n// public:\n// int coinChange(vector<int>& coins, int amount) {\n// GetOptimal(coins, amount, 0);\n// const int num_coins = dp_[std::make_pair(0, amount)];\n// if (num_coins == INT_MAX) { return -1; }\n// return num_coins;\n// }\n// private:\n// int GetOptimal(vector<int>& coins, const int amount, const int idx) {\n// if (idx == coins.size() || amount < 0) { return INT_MAX; }\n// if (amount == 0) {\n// return 0;\n// }\n// if (auto search = dp_.find(std::make_pair(idx, amount)); search != dp_.end()) {\n// return search->second;\n// }\n// // const int min_coins = std::min(GetOptimal(coins, amount - coins[idx], idx) + 1,\n// // GetOptimal(coins, amount, idx + 1)\n// // );\n// int min_coins = INT_MAX;\n// const int coin_selected = GetOptimal(coins, amount - coins[idx], idx);\n// if (coin_selected != INT_MAX) {\n// min_coins = coin_selected + 1;\n// }\n// const int coin_not_selected = GetOptimal(coins, amount, idx + 1);\n// if (coin_not_selected != INT_MAX) {\n// min_coins = std::min(min_coins, coin_not_selected);\n// }\n\n// dp_[std::make_pair(idx, amount)] = min_coins;\n// return min_coins;\n// }\n\n// std::map<std::pair<int, int>, int> dp_;\n// };\n// // DP with a map of (coin, rem_amount) to num_coins.\n\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if (amount == 0) { return 0; }\n vector<int> dp{0};\n for (int amount_here = 1; amount_here <= amount; ++amount_here) {\n dp.push_back(INT_MAX);\n }\n vector<int> next_dp;\n for (int coin : coins) {\n next_dp.push_back(0);\n for (int amount_here = 1; amount_here <= amount; ++amount_here) {\n if (amount_here >= coin && next_dp[amount_here - coin] != INT_MAX) {\n next_dp.push_back(std::min(dp[amount_here], next_dp[amount_here - coin] + 1));\n } else {\n next_dp.push_back(dp[amount_here]);\n }\n }\n dp = next_dp;\n next_dp.clear();\n }\n if (dp[amount] == INT_MAX) { return -1; }\n return dp[amount];\n }\n};", "memory": "33370" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nint t[14][400001];\n int dp(vector<int>& coins, int n, int amount) {\n if(amount==0) return 0;\n if(n<0)\n {\n return 1e9;\n }\n if(t[n][amount]!=-1) return t[n][amount];\n int nottake=0+dp(coins,n-1,amount);\n int take=1e9;\n if(coins[n]<=amount) \n {\n take=1+dp(coins,n,amount-coins[n]);\n }\n return t[n][amount]=min(take,nottake);\n }\n int coinChange(vector<int>& coins, int amount) {\n int n=coins.size();\n memset(t,-1,sizeof(t));\n int ans=dp(coins,n-1,amount);\n \n return ans==1e9 ? -1:ans;\n }\n};", "memory": "34224" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n vector<int> dp(amount + 1, INT_MAX);\n vector<bool> visited(amount + 1, false);\n dp[amount] = 0;\n queue<int> q;\n q.push(amount);\n while (!q.empty()) {\n int front = q.front();\n q.pop();\n if (visited[front])\n continue;\n visited[front] = true;\n \n for (auto coin : coins) {\n if (front - coin >= 0) {\n dp[front - coin] = min(dp[front - coin], dp[front] + 1);\n if (!visited[front - coin])\n q.push(front - coin);\n }\n }\n\n if (dp[0] != INT_MAX)\n return dp[0];\n }\n\n return -1;\n }\n};", "memory": "35078" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n const unsigned tot_coins = coins.size();\n const unsigned max_amount = amount + 1;\n const unsigned arr_dimm = tot_coins * max_amount;\n int* arr = new int[arr_dimm];\n for (unsigned coin = 0; coin < tot_coins; coin++) {\n arr[coin * max_amount + 0] = 0;\n }\n\n for (unsigned coin = 0; coin < tot_coins; coin++) {\n int coin_val = coins[coin];\n for (unsigned amount = 1; amount < max_amount; amount++) {\n int idx = (coin * max_amount + amount);\n arr[idx] = -1;\n // Take one coin; reconsider\n if (amount >= coin_val) {\n int lookup = arr[coin * max_amount + amount - coin_val];\n if (lookup != -1) {\n arr[idx] = lookup + 1;\n }\n }\n // Skip coin; consider next\n if (coin > 0) {\n int lookup = arr[(coin - 1) * max_amount + amount];\n if (lookup != -1) {\n if (arr[idx] == -1) {\n arr[idx] = lookup;\n } else if (arr[idx] > lookup) {\n arr[idx] = lookup;\n }\n }\n }\n }\n }\n\n return arr[(tot_coins - 1) * max_amount + amount];\n }\n};", "memory": "35931" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "#define INVALID -1\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n const unsigned tot_coins = coins.size();\n const unsigned max_amount = amount + 1;\n const unsigned arr_dimm = tot_coins * max_amount;\n int* arr = new int[arr_dimm];\n for (unsigned coin = 0; coin < tot_coins; coin++) {\n arr[coin * max_amount + 0] = 0;\n }\n\n for (unsigned coin = 0; coin < tot_coins; coin++) {\n int coin_val = coins[coin];\n for (unsigned amount = 1; amount < max_amount; amount++) {\n int idx = (coin * max_amount + amount);\n int res = INVALID;\n // Take one coin; reconsider\n if (amount >= coin_val) {\n int lookup = arr[coin * max_amount + amount - coin_val];\n if (lookup != INVALID) {\n res = lookup + 1;\n }\n }\n // Skip coin; consider next\n if (coin > 0) {\n int lookup = arr[(coin - 1) * max_amount + amount];\n if (lookup != INVALID) {\n if (res == INVALID) {\n res = lookup;\n } else if (res > lookup) {\n res = lookup;\n }\n }\n }\n arr[idx] = res;\n }\n }\n\n return arr[(tot_coins - 1) * max_amount + amount];\n }\n};", "memory": "35931" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "#include<map>\n\nclass Solution {\npublic:\n int *arr;\n int dimx, dimy;\n int coinChange(vector<int>& coins, int amount) {\n dimx = coins.size();\n dimy = amount;\n arr = new int[dimx * dimy + 1];\n std::cout << dimx * dimy << \"\\n\";\n for (unsigned i = 0; i < dimx * dimy + 1; i++) {\n arr[i] = -2;\n }\n int answer = coinChangeHelp(coins, coins.size()-1, amount);\n return answer;\n }\n\n int coinChangeHelp(vector<int>& coins, int idx, int amount) {\n if (idx < 0) {\n return -1;\n }\n if (amount == 0) {\n return 0;\n }\n if (amount < 0) {\n return -1;\n }\n\n int key = idx * dimy + amount;\n int memo = arr[key];\n if (memo != -2) {\n return memo;\n }\n\n int choose_coin = coinChangeHelp(coins, idx, amount - coins[idx]);\n int dont_choose = coinChangeHelp(coins, idx - 1, amount);\n\n if (choose_coin == -1) {\n arr[key] = dont_choose;\n } else if (dont_choose == -1) { \n arr[key] = choose_coin + 1;\n } else {\n arr[key] = std::min(dont_choose, choose_coin + 1);\n }\n\n return arr[key];\n }\n};", "memory": "36785" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int *arr;\n int dimx, dimy;\n int coinChange(vector<int>& coins, int amount) {\n dimx = coins.size();\n dimy = amount;\n arr = new int[dimx * dimy + 1];\n for (unsigned i = 0; i < dimx * dimy + 1; i++) {\n arr[i] = -2;\n }\n int answer = coinChangeHelp(coins, coins.size()-1, amount);\n return answer;\n }\n\n int coinChangeHelp(vector<int>& coins, int idx, int amount) {\n if (idx < 0) {\n return -1;\n }\n if (amount == 0) {\n return 0;\n }\n if (amount < 0) {\n return -1;\n }\n\n int key = idx * dimy + amount;\n int memo = arr[key];\n if (memo != -2) {\n return memo;\n }\n\n int choose_coin = coinChangeHelp(coins, idx, amount - coins[idx]);\n int dont_choose = coinChangeHelp(coins, idx - 1, amount);\n\n if (choose_coin == -1) {\n arr[key] = dont_choose;\n } else if (dont_choose == -1) { \n arr[key] = choose_coin + 1;\n } else {\n arr[key] = std::min(dont_choose, choose_coin + 1);\n }\n\n return arr[key];\n }\n};", "memory": "36785" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if(amount==0){\n return 0;\n }\n vector<int> prev(amount+1, 0);\n prev[0] = 0;\n int n = coins.size();\n for(int j = 1;j<=amount;j++){\n if(coins[0]<=j && j%coins[0]==0){\n prev[j] = j/coins[0];\n }else{\n prev[j] = 1e9;\n }\n }\n for(int i = 1;i<n;i++){\n if(coins[i]>amount){\n continue;\n }\n vector<int> curr(amount+1, 0);\n for(int j=0;j<=amount;j++){\n int notTake = prev[j];\n int take = 1e9;\n if(coins[i]<=j){\n take = 1+ curr[j-coins[i]];\n }\n curr[j] = min(notTake, take);\n }\n prev = curr;\n }\n if(prev[amount]==1e9){\n return -1;\n }\n return prev[amount];\n }\n};", "memory": "37639" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int f(vector<int>&coins,int ind,int amt,vector<vector<int>>&dp)\n {\n //base case here\n if(ind==0)\n {\n if(amt%coins[ind]==0)\n return amt/coins[ind];\n return 1e9;\n }\n if(dp[ind][amt]!=-1)\n return dp[ind][amt];\n int take=INT_MAX;\n int nottake=f(coins,ind-1,amt,dp);\n if(amt>=coins[ind])\n take=1+f(coins,ind,amt-coins[ind],dp);\n return dp[ind][amt]=std::min(take,nottake);\n }\n int coinChange(vector<int>& coins, int amount) {\n if(amount<=0)\n return 0;\n int n=coins.size();\n vector<int>prev(amount+1,0);\n \n for(int i=0;i<=amount;i++)\n {\n if(i%coins[0]==0)\n prev[i]=i/coins[0];\n else\n prev[i]=1e9;\n }\n for(int i=1;i<n;i++)\n {\n vector<int>cur(amount+1,0);\n for(int j=0;j<=amount;j++)\n {\n int take=INT_MAX;\n int nottake=prev[j];\n if(j>=coins[i])\n take=1+cur[j-coins[i]];\n cur[j]=std::min(take,nottake); \n }\n prev=cur;\n }\n \n int ans=prev[amount]; \n if(ans>=1e9)\n return -1;\n return ans;\n }\n};", "memory": "38493" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n // int helper(int ind,int w,vector<int> &coins,vector<vector<int>> &dp){\n // if(w == 0) return dp[ind][w] = 0;\n // if(ind == 0){\n // if(coins[0] == w) return dp[ind][w] = 1;\n // else{\n // if(w % coins[0] == 0) return dp[ind][w] = w / coins[0];\n // return dp[ind][w] = -1;\n // } \n // }\n // if(dp[ind][w] != -2) return dp[ind][w];\n // if(coins[ind] > w){\n // return dp[ind][w] = helper(ind-1,w,coins,dp);\n // }\n // int pick_temp = helper(ind,w-coins[ind],coins,dp);\n // int notpick = helper(ind-1,w,coins,dp);\n // if(pick_temp == -1 && notpick == -1) return dp[ind][w] = -1;\n // if(notpick == -1) return dp[ind][w] = 1 + pick_temp;\n // if(pick_temp == -1) return dp[ind][w] = notpick;\n // int pick = 1 + pick_temp;\n // return dp[ind][w] = min(pick,notpick); \n // }\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<int> dp(amount+1,-2);\n //helper(n-1,amount,coins,dp);\n dp[0] = 0;\n //for(int i = 0; i < n; i++) dp[i][0] = 0;\n for(int i = 0; i <= amount ; i++){\n if(i % coins[0] == 0) dp[i] = i / coins[0];\n else dp[i] = -1;\n }\n for(int i = 1; i < n; i++){\n vector<int> temp(amount+1,-2);\n for(int j = 0; j <= amount; j++){\n if(coins[i] > j){\n temp[j] = dp[j];\n continue;\n }\n int pick_temp = temp[j-coins[i]];\n int notpick = dp[j];\n if(pick_temp == -1 && notpick == -1) temp[j] = -1;\n else if(notpick == -1) temp[j] = 1 + pick_temp;\n else if(pick_temp == -1) temp[j] = notpick;\n else {\n int pick = 1 + pick_temp;\n temp[j] = min(pick,notpick);\n }\n\n }\n dp = temp;\n }\n return dp[amount]; \n }\n};", "memory": "39346" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<pair<long long,long long>> queue;\n int coinChange(vector<int>& c, int amount) {\n if(amount==0)\n return 0;\n sort(c.begin(),c.end());\n reverse(c.begin(),c.end());\n \n int left = 0;\n queue.push_back({0,0});\n\n int dp[10002] = {0};\n while(left<queue.size()){\n long long currAmount = queue[left].first;\n long long numCoins = queue[left].second;\n \n \n for(int i = 0;i<c.size();i++){\n if(currAmount + c[i] >amount){\n continue;\n }\n if((currAmount + c[i])==amount)\n return numCoins+1;\n \n if(dp[currAmount + c[i]]){\n continue;\n }\n dp[currAmount + c[i]] = 1;\n queue.push_back({currAmount + c[i],numCoins + 1});\n \n }\n left++;\n }\n\n return -1;\n }\n\n};", "memory": "45323" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n=coins.size();\n vector<int> dp(amount+1,1e9);\n dp[0]=0;\n for(int i=0;i<n;i++){\n if(coins[i]>amount) continue;\n vector<int> temp(amount+1);\n for(int j=0;j<=amount;j++){\n if(j>=coins[i]){\n temp[j]=min(dp[j],temp[j-coins[i]]+1);\n }\n else temp[j]=dp[j];\n }\n dp=temp;\n }\n for(auto it:dp) cout<<it<<\" \";\n if(dp[amount]>=1e9) return -1;\n else return dp[amount];\n }\n};", "memory": "46176" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<vector<int>> dp(n, vector<int>(amount + 1, 0));\n for (int j = 0; j < amount + 1; j++) {\n if (j % coins[0] == 0) dp[0][j] = j / coins[0];\n else dp[0][j] = 1e9;\n }\n for (int i = 1; i < n; i++) {\n for (int j = 0; j < amount + 1; j++) {\n dp[i][j] = dp[i - 1][j];\n if (j >= coins[i]) dp[i][j] = min(dp[i][j], 1 + dp[i][j - coins[i]]);\n }\n }\n int res = dp[n - 1][amount];\n return res >= 1e9 ? -1 : res;\n }\n};", "memory": "46176" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n\n int n=coins.size();\n vector<vector<int>> dp(n,vector<int>(amount+1,0));\n\n for (int i = 0; i <= amount; i++) \n {\n if (i % coins[0] == 0)\n dp[0][i] = i / coins[0];\n else\n dp[0][i] = 1e9; // Set it to a very large value if not possible\n\n }\n for(int i=1;i<n;i++)\n {\n for(int j=0;j<=amount;j++)\n {\n int nottake=dp[i-1][j];\n \n int take=1e9;\n if(coins[i]<=j)\n {\n take=1+dp[i][j-coins[i]];\n }\n dp[i][j]=min(take,nottake);\n }\n }\n return dp[n-1][amount]>=1e9?-1:dp[n-1][amount];\n \n }\n};", "memory": "47030" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int solveRec(int ind,vector<int> &coins,int amount){\n if(ind == 0){\n if(amount % coins[ind] == 0){\n return (amount/coins[ind]);\n }\n else{\n return 1e9;\n }\n }\n \n int notPick = 0 + solveRec(ind-1,coins,amount);\n int pick = INT_MAX;\n if(coins[ind] <= amount){\n pick = 1 + solveRec(ind,coins,amount-coins[ind]);\n }\n\n return min(pick,notPick);\n }\n\n\n int solveMem(int ind,vector<int> &coins,int amount,vector<vector<int> > &dp){\n if(ind == 0){\n if(amount % coins[ind] == 0){\n return (amount/coins[ind]);\n }\n else{\n return 1e9;\n }\n }\n if(dp[ind][amount] != -1){\n return dp[ind][amount];\n }\n \n int notPick = 0 + solveMem(ind-1,coins,amount,dp);\n int pick = INT_MAX;\n if(coins[ind] <= amount){\n pick = 1 + solveMem(ind,coins,amount-coins[ind],dp);\n }\n\n return dp[ind][amount] = min(pick,notPick);\n }\n\n\n int solveTab(vector<int> &coins,int amount){\n int n = coins.size();\n vector<vector<int> > dp(n,vector<int> (amount+1,0));\n\n for(int i=0 ; i<=amount ; i++){\n if(i % coins[0] == 0){\n dp[0][i] = i/coins[0];\n }\n else{\n dp[0][i] = 1e9;\n }\n }\n\n for(int i=1 ; i<n ; i++){\n for(int T=0 ; T<=amount ; T++){\n int notPick = 0 + dp[i-1][T];\n int pick = INT_MAX;\n if(T >= coins[i]){\n pick = 1 + dp[i][T - coins[i]];\n }\n dp[i][T] = min(pick,notPick);\n }\n }\n return dp[n-1][amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n // int ans = solveRec(n-1,coins,amount);\n // if(ans >= 1e9){\n // return -1;\n // }\n // return ans;\n\n // vector<vector<int> > dp(n,vector<int> (amount+1,-1));\n // int ans = solveMem(n-1,coins,amount,dp);\n // if(ans >= 1e9){\n // return -1;\n // }\n // return ans;\n\n int ans = solveTab(coins,amount);\n if(ans >= 1e9){\n return -1;\n }\n return ans;\n }\n};", "memory": "47030" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "// class Solution {\n// private:\n// int fl(vector<int>& arr, int ind, int t, vector<vector<int>>& dp){\n// if (ind == 0) {\n// if (t % arr[0] == 0){\n// return t / arr[0];\n// }\n// else {\n// return 1e9; \n// }\n// }\n\n// if (dp[ind][t] != -1) return dp[ind][t];\n\n// int notTaken = 0+fl(arr, ind - 1, t, dp); \n\n// int taken = INT_MAX;\n// if (arr[ind] <= t) { \n// taken = 1 + fl(arr, ind, t - arr[ind], dp); \n// }\n\n// return dp[ind][t] = min(notTaken, taken); \n// }\n\n// public:\n// int coinChange(vector<int>& coins, int amount) {\n// int n = coins.size();\n// vector<vector<int>> dp(n, vector<int>(amount + 1, -1)); \n// int a = fl(coins, n - 1, amount, dp);\n// if(a >= 1e9) {\n // return -1;\n // }\n// return a;\n// }\n// };\n\n\n\nclass Solution{\n private:\n\nint minimumElementsUtil(vector<int>& arr, int ind, int T, vector<vector<int>>& dp){\n\n \n if(ind == 0){\n \n if(T % arr[0] == 0)\n return T / arr[0]; \n else\n return 1e9; \n }\n \n \n if(dp[ind][T] != -1)\n return dp[ind][T];\n \n int notTaken = 0 + minimumElementsUtil(arr, ind - 1, T, dp);\n \n\n int taken = 1e9;\n if(arr[ind] <= T)\n taken = 1 + minimumElementsUtil(arr, ind, T - arr[ind], dp);\n \n \n return dp[ind][T] = min(notTaken, taken);\n}\npublic:\n int coinChange(vector<int>& coins, int amount){\n \n int n = coins.size();\n \n vector<vector<int>> dp(n, vector<int>(amount + 1, -1));\n \n int ans = minimumElementsUtil(coins, n - 1, amount, dp);\n \n if(ans >= 1e9)\n return -1;\n return ans; \n}\n};", "memory": "47884" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\n private:\n int fl(vector<int>& arr, int ind, int t, vector<vector<int>>& dp){\n if (ind == 0) {\n if (t % arr[0] == 0){\n return t / arr[0];\n }\n else {\n return 1e9; \n }\n }\n\n if (dp[ind][t] != -1) return dp[ind][t];\n\n int notTaken = 0+fl(arr, ind - 1, t, dp); \n\n int taken = INT_MAX;\n if (arr[ind] <= t) { \n taken = 1 + fl(arr, ind, t - arr[ind], dp); \n }\n\n return dp[ind][t] = min(notTaken, taken); \n }\n\n public:\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<vector<int>> dp(n, vector<int>(amount + 1, -1)); \n int a = fl(coins, n - 1, amount, dp);\n if(a >= 1e9) {\n return -1;\n }\n return a;\n }\n};\n\n\n\n// class Solution{\n// private:\n\n// int minimumElementsUtil(vector<int>& arr, int ind, int T, vector<vector<int>>& dp){\n\n \n// if(ind == 0){\n \n// if(T % arr[0] == 0)\n// return T / arr[0]; \n// else\n// return 1e9; \n// }\n \n \n// if(dp[ind][T] != -1)\n// return dp[ind][T];\n \n// int notTaken = 0 + minimumElementsUtil(arr, ind - 1, T, dp);\n \n\n// int taken = 1e9;\n// if(arr[ind] <= T)\n// taken = 1 + minimumElementsUtil(arr, ind, T - arr[ind], dp);\n \n \n// return dp[ind][T] = min(notTaken, taken);\n// }\n// public:\n// int coinChange(vector<int>& coins, int amount){\n \n// int n = coins.size();\n \n// vector<vector<int>> dp(n, vector<int>(amount + 1, -1));\n \n// int ans = minimumElementsUtil(coins, n - 1, amount, dp);\n \n// if(ans >= 1e9)\n// return -1;\n// return ans; \n// }\n// };", "memory": "47884" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp; \n \n int solve(int ind, int amount, vector<int> &coins) {\n if (amount == 0)\n return 0; \n \n if (ind == coins.size())\n return 1e9;\n \n if (dp[ind][amount] != -1)\n return dp[ind][amount]; \n \n int ans = solve(ind+1, amount, coins); \n if (coins[ind] <= amount)\n ans = min(ans, 1+solve(ind, amount-coins[ind], coins)); \n \n return dp[ind][amount] = ans; \n \n }\n \n int coinChange(vector<int>& coins, int amount) {\n sort(coins.rbegin(), coins.rend()); \n dp.resize(coins.size(), vector<int>(amount+1, -1)); \n \n int ans= solve(0, amount, coins); \n return ans > amount ? -1 : ans; \n }\n};", "memory": "48738" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.rbegin(),nums.rend());\n\n vector<vector<int>> dp(n,vector<int>(k+1,-1));\n\n auto give = [&] (auto&& give,int i,int sum) -> int {\n if(sum<0) return 1e8;\n if(!sum) return 0;\n if(i==n) return 1e8;\n if(dp[i][sum] != -1) return dp[i][sum];\n\n return dp[i][sum] = min(give(give,i+1,sum),1+give(give,i,sum-nums[i]));\n };\n\n int ans = give(give,0,k);\n if(ans == 1e8) return -1;\n return ans;\n }\n};", "memory": "48738" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int idx,int amount,vector<int> &coins,vector<vector<int>> &dp){\n if(amount == 0) return 0;\n if(idx == 0){\n if(amount % coins[idx] == 0) return amount/coins[idx];\n return 1e9;\n }\n if(dp[idx][amount] != -1) return dp[idx][amount];\n int pick1 = 1e9,pick2 = 1e9;\n if(amount >= coins[idx]){\n pick1 = 1+f(idx-1,amount-coins[idx],coins,dp);\n pick2 = 1+f(idx,amount-coins[idx],coins,dp);\n }\n int notPick = f(idx-1,amount,coins,dp);\n return dp[idx][amount] = min({pick1,pick2,notPick});\n }\n int coinChange(vector<int> &coins,int amount){\n int n = coins.size();\n vector<vector<int>> dp(n,vector<int>(amount+1,-1));\n return f(n-1,amount,coins,dp) == 1e9 ? -1 : f(n-1,amount,coins,dp);\n }\n};", "memory": "49591" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int inf = 1e4 + 5;\n int res(int x, int amount, vector<int>& coins, vector<vector<int>>& dp){\n // base case\n if(x == 0){\n if(amount%coins[x] == 0){\n return amount/coins[x];\n }\n else{\n return inf;\n }\n }\n // cache check\n if(dp[x][amount] != -1) return dp[x][amount];\n // transition\n int ans = res(x-1, amount, coins, dp);\n if(amount >= coins[x]){\n ans = min({ans, 1 + res(x, amount - coins[x], coins, dp), INT_MAX});\n }\n // save and return\n return dp[x][amount] = ans;\n }\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<vector<int>> dp(n, vector<int>(amount+1, -1));\n int ans = res(n-1,amount,coins,dp);\n return (ans == inf ? -1 : ans);\n }\n};", "memory": "49591" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n = (int) coins.size();\n vector<vector<int>> dp(n, vector<int> (amount + 1, -1));\n function<int(int, int)> f = [&] (int i, int amount) {\n if (amount < 0) return int(1e6);\n if (i < 0) {\n if (amount == 0) {\n return 0;\n } else {\n return int(1e6);\n }\n }\n if (dp[i][amount] != -1) return dp[i][amount];\n return dp[i][amount] = min(f(i - 1, amount), f(i, amount - coins[i]) + 1);\n };\n int ans = f(n - 1, amount);\n return ans == 1e6 ? -1 : ans;\n }\n};", "memory": "50445" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n = (int) coins.size();\n vector<vector<int>> dp(n, vector<int> (amount + 1, -1));\n function<int(int, int)> f = [&] (int i, int amount) {\n if (amount < 0) return int(1e9);\n if (i < 0) {\n if (amount == 0) {\n return 0;\n } else {\n return int(1e9);\n }\n }\n if (dp[i][amount] != -1) return dp[i][amount];\n return dp[i][amount] = min(f(i - 1, amount), f(i, amount - coins[i]) + 1);\n };\n int ans = f(n - 1, amount);\n return ans == 1e9 ? -1 : ans;\n }\n};", "memory": "50445" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n=coins.size();\n vector<vector<int>>t(n+1,vector<int>(amount+1));\n // t[0][0];\n for(int i=1;i<=n;i++)t[i][0]=0;\n for(int j=0;j<=amount;j++)t[0][j]=INT_MAX-1;\n \n for(int i=1;i<=n;i++){\n for(int j=1;j<=amount;j++){\n if(coins[i-1]<=j)\n t[i][j]=min(t[i][j-coins[i-1]]+1, t[i-1][j]);\n else \n t[i][j]=t[i-1][j];\n }\n }\n int ans=t[n][amount]==INT_MAX-1?-1:t[n][amount];\n return ans;\n }\n};", "memory": "51299" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fun(int ind, int rem, vector<int>& coins, vector<vector<int>>& mem){\n\n if(ind == 0){\n return rem == 0 ? 0 : INT_MAX-1;\n }\n\n if(mem[ind][rem] != -1)\n return mem[ind][rem];\n\n if(rem >= coins[ind-1]){\n return mem[ind][rem] = min(1 + fun(ind, rem-coins[ind-1], coins, mem), fun(ind-1, rem, coins, mem));\n }\n else{\n return mem[ind][rem] = fun(ind-1, rem, coins, mem);\n }\n }\n int coinChange(vector<int>& coins, int amount) {\n \n int m = coins.size();\n vector<vector<int>> dp(m+1, vector<int>(amount+1, INT_MAX-1));\n\n for(int i=0; i<=m; i++)\n dp[i][0] = 0;\n \n for(int i=1; i<=m; i++){\n for(int j=1; j<=amount; j++){\n if(j >= coins[i-1]){\n dp[i][j] = min(1+dp[i][j-coins[i-1]], dp[i-1][j]);\n }\n else{\n dp[i][j] = dp[i-1][j];\n }\n }\n }\n\n int ans = dp[m][amount];\n\n return ans == INT_MAX-1 ? -1 : ans;\n }\n};", "memory": "51299" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int findLowestCoins(vector<int>& coins, int index, int amount,\n vector<vector<int>>& dp) {\n if (index >= coins.size() || amount <= 0) {\n if (amount == 0) {\n return 0;\n }\n return 1e9;\n }\n if (dp[index][amount] != -1)\n return dp[index][amount];\n\n int doNotTakeCoin = 1e9;\n int takeCoin = 1e9;\n if (coins[index] > amount) {\n doNotTakeCoin = 0 + findLowestCoins(coins, index + 1, amount, dp);\n } else {\n takeCoin =\n 1 + findLowestCoins(coins, index, amount - coins[index], dp);\n doNotTakeCoin = 0 + findLowestCoins(coins, index + 1, amount, dp);\n }\n return dp[index][amount] = min(takeCoin, doNotTakeCoin);\n }\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n vector<vector<int>> dp(n + 1, vector<int>(amount + 1, -1));\n int index = 0;\n int res = findLowestCoins(coins, index, amount, dp);\n if (res >= 1e9) {\n return -1;\n }\n return res;\n }\n};", "memory": "52153" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fun(vector<int> &v,int ind,int n,int k,vector<vector<int>> &dp)\n {\n if(ind==n)\n {\n if(k==0) return 0;\n else return 1e9;\n }\n\n if(dp[ind][k]!=-1) return dp[ind][k];\n\n // notTake\n int notTake = 0 + fun(v,ind+1,n,k,dp);\n\n //Take\n int Take = 1e9;\n\n if(v[ind]<=k) Take = 1+fun(v,ind,n,k-v[ind],dp);\n\n return dp[ind][k] = min(Take,notTake);\n }\n int coinChange(vector<int>& v, int k) {\n int n = v.size();\n vector<vector<int>> dp(n+1, vector<int> (k+1, -1));\n int val = fun(v,0,n,k,dp);\n if(val>=1e9) return -1;\n else return val;\n }\n};", "memory": "52153" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int chk(int st,int en, vector<int>&coins, int a) {\n if(a==0)\n return 0;\n if(st>=en)\n return INT_MAX;\n if(dp[st][a]!=-1)\n return dp[st][a];\n if(a<coins[st])\n return dp[st][a] = INT_MAX;\n\n int u = chk(st,en,coins,a-coins[st]);\n int v = chk(st+1,en,coins,a-coins[st]);\n int w = chk(st+1,en,coins,a);\n\n if(u==INT_MAX && v==INT_MAX && w==INT_MAX)\n return dp[st][a] = INT_MAX;\n u = min(u,v);\n if(u==INT_MAX){\n dp[st][a] = w;\n return dp[st][a];\n }\n dp[st][a] = min(1+u,w);\n return dp[st][a];\n\n }\n int coinChange(vector<int>& coins, int amount) {\n sort(coins.begin(),coins.end());\n dp = vector<vector<int>>(coins.size()+1,vector<int>(amount+1,-1));\n int x = chk(0,coins.size(),coins,amount);\n if(x == INT_MAX)\n return -1;\n return x;\n }\n};", "memory": "53006" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int solve(int i,int n,vector<int> &coins,int a, vector<vector<int>> &dp){\n\n\n if(i == n){\n if(a == 0) return 0;\n return 1e9;\n }\n\n if(dp[i][a] != -1) return dp[i][a];\n\n int take = 1e9;\n\n if(coins[i] <= a){\n take = 1 + solve(i,n,coins,a-coins[i],dp);\n }\n\n int skip = solve(i+1,n,coins,a,dp);\n\n return dp[i][a] = min(take,skip);\n }\n int coinChange(vector<int>& coins, int amount) {\n \n sort(coins.rbegin(),coins.rend());\n int n = coins.size();\n\n vector<vector<int>> dp(n+1, vector<int> (amount+1,-1));\n\n if(amount == 0) return 0;\n\n int ans = solve(0,n,coins,amount,dp);\n\n return ans >= 1e9 ? -1 : ans;\n }\n};", "memory": "53006" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> memo;\n int getChanges(vector<int> &nums, int target, int idx) {\n if (target == 0) {\n return 0;\n }\n if (memo[idx][target] != -1) {\n return memo[idx][target];\n }\n if (idx >= nums.size() || target < 0) {\n return INT_MAX;\n }\n // take it \n int count1 = INT_MAX, count2 = INT_MAX;\n if (nums[idx] <= target) {\n int res = getChanges(nums, target - nums[idx], idx);\n if (res != INT_MAX) {\n count1 = 1 + res;\n }\n }\n \n count2 = getChanges(nums, target, idx + 1);\n return memo[idx][target] = min({ count1, count2 });\n }\n \n int coinChange(vector<int>& coins, int amount) {\n int l = coins.size();\n memo.assign(l+1, vector<int>(amount+1, -1));\n int ans = getChanges(coins, amount, 0);\n return ans == INT_MAX ? -1 : ans;\n }\n};", "memory": "53860" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int sz = size(coins);\n vector<vector<int>> dp(sz + 1, vector<int>(amount + 1, INT_MAX));\n function<int(int, int)> minCoins = [&](int i, int amount) {\n if(amount == 0)\n return 0;\n if(amount < 0 || i == coins.size())\n return 100000;\n if(dp[i][amount] != INT_MAX)\n return dp[i][amount];\n int minCount = minCoins(i, amount - coins[i]);\n if(minCount != 100000)\n minCount++;\n return dp[i][amount] = min(minCount, minCoins(i + 1, amount));\n };\n int minCount = minCoins(0, amount);\n return minCount != 100000 ? minCount : -1;\n }\n};", "memory": "53860" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/*\ncoins -> [1 to 2^31]\namount -> [0 to 10000] \n [2,5] amount = 13\n [5] amount => 8 len 5 \n [1,2,5] => 11\n [1,2,5] => 6\n 1. if amount == currSumOfCoins I have reached return len;\n 2. if idx > coins.size() return INT_MAX; \n\n prune sort the coins then if you know the currentLeftAmount<coins[idx] [idx,n)\n\n [5,5] len => 3\n*/\n\n\nclass Solution {\npublic:\n\n int minCoinsToSum(int index,vector<int> &coins,int amount,vector<vector<int>> &dp){\n if(amount == 0){\n return dp[index][amount] = 0;\n }\n\n if(index>=coins.size()){\n return dp[index][amount] = 1e9;\n }\n if(dp[index][amount]!=-1) return dp[index][amount];\n // check if I can take it or not\n if(amount<coins[index]){\n return dp[index][amount] = 1e9;\n }\n\n return dp[index][amount] = min({1+minCoinsToSum(index+1,coins,amount-coins[index],dp),minCoinsToSum(index+1,coins,amount,dp),1+minCoinsToSum(index,coins,amount-coins[index],dp)});\n\n }\n\n int coinChange(vector<int>& coins, int amount) {\n sort(coins.begin(),coins.end());\n\n vector<vector<int>> dp(coins.size()+1,vector<int>(amount+1,-1));\n\n int ans = minCoinsToSum(0,coins,amount,dp);\n if(ans>=1e9) return -1;\n return ans;\n }\n};", "memory": "54714" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec (long long i, vector<int>& coins, long long n, long long amount, long long curramt, vector<vector<int>>& dp){\n if(i>=n) return 1e9;\n if (curramt == amount) return 0;\n if(dp[i][curramt]!=-1) return dp[i][curramt];\n long long take = 1e9;\n long long take2 = 1e9;\n long long nottake = 1e9;\n if(curramt+coins[i]<=amount) {\n take = 1 + rec(i,coins,n,amount,curramt+coins[i],dp); \n take2 = rec(i+1,coins,n,amount,curramt,dp);\n }\n else nottake = rec(i+1,coins,n,amount,curramt,dp);\n return dp[i][curramt] = min({take,take2,nottake});\n \n }\n int coinChange(vector<int>& coins, int amount) {\n vector<vector<int>> dp(coins.size()+1,vector<int>(amount+1,-1));\n int res = rec(0, coins, coins.size(), amount, 0LL,dp);\n return (res == 1e9) ? -1 : res;\n }\n};", "memory": "54714" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>& coins,int amount,int idx,vector<vector<int>>& dp){\n\n if(idx==0){\n\n if(amount % coins[0]==0){\n\n return amount / coins[0];\n }\n\n else return INT_MAX;\n\n }\n\n if(dp[idx][amount]!=-1){\n\n return dp[idx][amount];\n }\n\n int take = INT_MAX;\n\n int nottake = solve(coins,amount,idx-1,dp);\n\n if(amount-coins[idx]>=0){\n\n take = solve(coins,amount-coins[idx],idx,dp);\n\n if(take!=INT_MAX){\n\n take = 1 + take;\n }\n }\n\n return dp[idx][amount] = min(take,nottake);\n\n }\n\n\n int coinChange(vector<int>& coins, int amount) {\n\n vector<vector<int>> dp(coins.size(),vector<int>(amount+1,0));\n\n vector<int> prev(amount+1,0) , curr(amount+1,0);\n\n // int ans = solve(coins,amount,coins.size()-1,dp);\n\n // if(ans==INT_MAX) return -1;\n\n // return ans;\n\n for(int i=0;i<=amount;i++){\n\n if(i % coins[0]==0) prev[i] = (i / coins[0]);\n else prev[i] = INT_MAX;\n\n }\n\n for(int idx=1;idx<coins.size();idx++){\n\n for(int target = 0;target<=amount;target++){\n\n int take = INT_MAX;\n\n int nottake = prev[target];\n\n if(target-coins[idx]>=0){\n\n take = curr[target-coins[idx]];\n\n if(take!=INT_MAX){\n\n take = 1 + take;\n }\n }\n\n curr[target] = min(take,nottake);\n }\n\n prev = curr;\n }\n\n \n if(prev[amount]==INT_MAX){\n\n return -1;\n }\n \n return prev[amount];\n \n\n\n }\n};", "memory": "55568" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n // //int dp[13][10001];\n // int help(int i, vector<int>& coins, int amount) {\n // if(i==coins.size()-1) {\n // if(amount%coins[i]==0) return amount/coins[i];\n // return 1e9;\n // }\n\n // if(dp[i][amount] != -1) return dp[i][amount];\n\n // int pick = 1e9, notPick=1e9;\n\n // if(amount >= coins[i]) {\n // pick = 1 + help(i, coins, amount-coins[i]);\n // }\n // notPick = help(i+1, coins, amount);\n // return dp[i][amount] = min(pick, notPick);\n // }\npublic:\n int coinChange(vector<int>& coins, int amount) {\n int n = coins.size();\n //memset(dp, -1, sizeof(dp));\n\n // int ans = help(0, coins, amount);\n // return ans>=1e9 ? -1 : ans;\n vector<vector<int>>dp(n+2, vector<int>(amount+2, 0));\n for(int i=0; i<=amount; ++i) {\n if(i % coins[n-1] == 0) dp[n-1][i] = i/coins[n-1];\n else dp[n-1][i] = 1e9;\n }\n\n for(int i=n-2; i>=0; --i) {\n for(int target=0; target<=amount; ++target) {\n int pick = 1e9;\n int notPick = dp[i+1][target];\n if(coins[i] <= target) {\n pick = 1 + dp[i][target-coins[i]];\n }\n dp[i][target] = min(pick, notPick);\n }\n }\n return dp[0][amount] >= 1e9 ? -1 : dp[0][amount];\n }\n};", "memory": "55568" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int s(int i, int amt, vector<int>&coins,vector<vector<int>> &dp){\n if(amt<0) return 1e9;\n if(amt==0) return 0;\n if(i>=coins.size()) return 1e9;\n if(dp[i][amt]!=-1) return dp[i][amt];\n \n int take = 1e9;\n int notTake = s(i+1, amt, coins,dp);\n \n if(amt>=coins[i]){\n take = 1+s(i, amt-coins[i], coins,dp);\n }\n \n return dp[i][amt] = min(take, notTake);\n }\n\n int coinChange(vector<int>& coins, int amount) {\n vector<vector<int>> dp(coins.size()+2, vector<int>(amount+1, -1));\n sort(coins.begin(),coins.end(), greater<int>());\n return s(0,amount, coins,dp)==1e9 ? -1:s(0,amount, coins,dp);\n }\n\n // int coinChange(vector<int>& coins, int amount) {\n // vector<vector<int>> dp(coins.size()+1, vector<int>(amount+1, 0));\n // for(int i=0;i<=coins.size();i++){\n // for(int j=0;j<=amount;j++){\n // if(j==0) dp[i][j]=0;\n // int take = 1e9;\n // int notTake = 1e9;\n // if(i>0) notTake = dp[i-1][j];\n // if(j>=coins[i]){\n // take = 1+ dp[i][j-coins[i]];\n // }\n // return dp[i][j] = min(take,notTake);\n // }\n // }\n\n // return dp[coins.size()][amount]>=1e9 ? -1:dp[coins.size()][amount];\n // }\n\n};\n\n\n// class Solution {\n// // private:\n// // int f(int index, int amount, vector<int>& coins){\n// // if(index==0){\n// // if(amount%coins[0]==0){\n// // return amount/coins[0];\n// // }\n// // else return 1e9;\n// // }\n// // int notTake = 0+f(index-1,amount,coins);\n// // int take = 1e9;\n// // if(coins[index]<=amount){\n// // take = 1+f(index, amount-coins[index],coins);\n// // } \n\n// // return min(take,notTake);\n// // }\n// // public:\n// // int coinChange(vector<int>& coins, int amount) {\n// // int ans = f(coins.size()-1,amount,coins);\n// // if (ans>=1e9) return -1;\n// // else return ans;\n// // }\n\n// // private:\n// // int f(int index, int amount, vector<int>& coins,vector<vector<int>> &dp){\n// // if(index==0){\n// // if(amount%coins[0]==0){\n// // return amount/coins[0];\n// // }\n// // else return 1e9;\n// // }\n// // if (dp[index][amount]!=-1) return dp[index][amount];\n// // int notTake = 0+f(index-1,amount,coins,dp);\n// // int take = 1e9;\n// // if(coins[index]<=amount){\n// // take = 1+f(index, amount-coins[index],coins,dp);\n// // } \n\n// // return dp[index][amount] = min(take,notTake);\n// // }\n// public:\n// int coinChange(vector<int>& coins, int amount) {\n// int n = coins.size(); \n// vector<vector<int>> dp(n,vector<int>(amount+1,-1));\n// for (int t = 0;t<=amount;t++){\n// if(t%coins[0]==0) dp[0][t] = t/coins[0];\n// else dp[0][t] = 1e9;\n// }\n// for(int i = 1;i<n;i++){\n// for(int t = 0;t<=amount;t++){\n// int notTake = 0+dp[i-1][t];\n// int take = 1e9;\n// if(coins[i]<=t){\n// take = 1+dp[i][t-coins[i]];\n// } \n// dp[i][t] = min(take,notTake);\n// }\n// }\n// int ans = dp[n-1][amount];\n// if (ans>=1e9) return -1;\n// else return ans;\n\n// }\n// };", "memory": "56421" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\nint solve(vector<int>& coins,int i,int amount,vector<vector<int>>&dp){\n\n\nif(amount==0) return 0;\nif(i==coins.size() || amount<0) return 1e9;\nif(dp[i][amount]!=-1) return dp[i][amount];\n\nint ans= min(solve(coins,i+1,amount-coins[i],dp)+1,min(solve(coins,i,amount-coins[i],dp)+1,solve(coins,i+1,amount,dp)));\n\n\nreturn dp[i][amount]=ans;\n}\n\n int coinChange(vector<int>& coins, int amount) {\nint n=coins.size();\nvector<vector<int>>dp(n+2,vector<int>(amount+2,-1));\nint c=solve(coins,0,amount,dp);\nif(c>=1e9) return -1;\nelse return c;\n\n\n\n\n }\n};", "memory": "56421" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int memo(vector<int> &coins, int amount, int i, vector<vector<int>> &dp) {\n if(amount==0) return 0;\n\n if(i==coins.size()-1) {\n if(amount%coins[i]==0) return amount/coins[i];\n return 1e9;\n }\n\n if(dp[i][amount]!=-1) return dp[i][amount];\n\n int notTake = memo(coins,amount,i+1,dp);\n int take = INT_MAX;\n if(coins[i]<=amount) take = 1 + memo(coins,amount-coins[i],i,dp);\n\n return dp[i][amount] = min(notTake,take);\n }\n\n int tab(vector<int>& coins, int amount) {\n vector<vector<int>> dp(coins.size()+1, vector<int> (amount+1,0)); \n int n = coins.size();\n\n for(int i=1;i<=amount;i++) {\n if(i%coins[n-1]==0) dp[n-1][i] = i/coins[n-1];\n else dp[n-1][i] = 1e9;\n }\n\n for(int i = n-2;i>=0;i--) {\n for(int t = 1;t<=amount;t++) {\n int notTake = dp[i+1][t];\n int take = INT_MAX;\n if(coins[i]<=t && t-coins[i]>=0) take = 1 + dp[i][t-coins[i]];\n dp[i][t] = min(notTake,take);\n }\n }\n\n return dp[0][amount];\n }\n\n int spaceOpt(vector<int>& coins, int amount) {\n vector<int> curr(amount+1,0); \n vector<int> next(amount+1,0); \n int n = coins.size();\n\n for(int i=1;i<=amount;i++) {\n if(i%coins[n-1]==0) next[i] = i/coins[n-1];\n else next[i] = 1e9;\n }\n\n for(int i = n-2;i>=0;i--) {\n for(int t = 1;t<=amount;t++) {\n int notTake = next[t];\n int take = 1e9;\n if(coins[i]<=t && t-coins[i]>=0) take = 1 + curr[t-coins[i]];\n curr[t] = min(notTake,take);\n }\n next = curr;\n }\n\n return next[amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n vector<vector<int>> dp(coins.size()+1, vector<int> (amount+1,-1));\n \n int ans = spaceOpt(coins,amount);\n if(ans==1e9) return -1;\n return ans; \n }\n};", "memory": "57275" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minCoins(vector<int>& coins, int amount, int idx, vector<vector<int>>& dp)\n {\n if(idx == 0)\n {\n if(amount % coins[idx] == 0)\n return amount / coins[idx]; \n return 1e9; \n }\n if(dp[idx][amount] != -1)\n return dp[idx][amount]; \n int notPick = 0 + minCoins(coins, amount, idx - 1, dp); \n int pick = INT_MAX; \n if(amount - coins[idx] >= 0)\n pick = 1 + minCoins(coins, amount - coins[idx], idx, dp); \n \n return dp[idx][amount] = min(pick, notPick);\n }\n\n int coinChange(vector<int>& coins, int amount) {\n vector<vector<int>> dp(coins.size() + 1, vector<int> (amount + 1, -1)); \n int n = coins.size(); \n // int ans = minCoins(coins, amount, n-1, dp);\n // if(ans > 1e4)\n // return -1; \n return tabulation(coins, amount); \n }\n int tabulation(vector<int>& coins, int amount)\n {\n // vector<vector<int>> dp(coins.size() + 1, vector<int> (amount + 1, 0)); \n vector<int> prev(amount + 1, 0), curr(amount + 1, 0); \n int n = coins.size(); \n for(int T=1; T<amount + 1; T++)\n {\n if(T % coins[0] == 0)\n {\n prev[T] = T / coins[0]; \n }\n else prev[T] = 1e9;\n }\n for(int i=1; i<n; i++)\n {\n for(int j=0; j < amount + 1; j++)\n {\n int notPick = prev[j];\n int pick = INT_MAX; \n if(j >= coins[i])\n {\n pick = 1 + curr[j - coins[i]]; \n }\n curr[j] = min(pick, notPick); \n }\n prev = curr; \n }\n if(prev[amount] >= 1e9)\n return -1; \n return prev[amount]; \n }\n};", "memory": "58129" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n//9,6,5,1 greedy: 9,1,1 dp:6,5\n//greedy fails as no unifromity or consistent difference\n int rec(vector<int> &coins, int amount, int i, int n, vector<vector<int>> &dp) {\n if(amount==0) return 0;\n if(amount<0) return INT_MAX;\n if(i==n-1) {\n if(amount%coins[i]==0) return amount/coins[i];\n return INT_MAX;\n }\n\n if(dp[i][amount]!=-1) return dp[i][amount];\n \n int notTake = rec(coins,amount,i+1,n,dp);\n int take = INT_MAX;\n if(coins[i]<=amount) {\n int d = rec(coins,amount-coins[i],i,n,dp);\n if(d!=INT_MAX) take = 1+d;\n }\n return dp[i][amount]=min(take,notTake);\n }\n\n int tab(vector<int> &coins, int amount, int n) {\n vector<vector<int>> dp(coins.size()+1, vector<int>(amount+1,0));\n \n for(int i=0;i<=amount;i++) {\n if(i%coins[n-1]==0) dp[n-1][i] = i/coins[n-1];\n else dp[n-1][i] = INT_MAX;\n }\n\n for(int i=n-2;i>=0;i--) {\n for(int target = 1;target<=amount;target++) {\n int notTake = dp[i+1][target];\n int take = INT_MAX;\n if(coins[i]<=target) {\n int d = dp[i][target-coins[i]];\n if(d!=INT_MAX) take = 1+d;\n }\n dp[i][target]=min(take,notTake);\n }\n }\n\n return dp[0][amount];\n }\n\n int spaceOpt(vector<int> &coins, int amount, int n) {\n vector<int> curr(amount+1,0);\n vector<int> next(amount+1,0);\n \n for(int i=0;i<=amount;i++) {\n if(i%coins[n-1]==0) next[i] = i/coins[n-1];\n else next[i] = INT_MAX;\n }\n\n for(int i=n-2;i>=0;i--) {\n for(int target = 1;target<=amount;target++) {\n int notTake = next[target];\n int take = INT_MAX;\n if(coins[i]<=target) {\n int d = curr[target-coins[i]];\n if(d!=INT_MAX) take = 1+d;\n }\n curr[target]=min(take,notTake);\n }\n next = curr;\n }\n\n return next[amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n vector<vector<int>> dp(coins.size()+1, vector<int>(amount+1,-1));\n int ans = spaceOpt(coins,amount,coins.size());\n if(ans==INT_MAX) return -1;\n return ans;\n }\n};", "memory": "58983" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int ind, int amt, vector<int>& coins) {\n\n if (amt == 0)\n return 0;\n\n if (ind < 0)\n return INT_MAX - 1;\n\n // if (amt % coins[ind] == 0)\n // return amt / coins[ind];\n int ans = INT_MAX - 1;\n if (coins[ind] <= amt) {\n ans = min(ans, 1 + f(ind, amt - coins[ind], coins));\n }\n ans = min(ans, f(ind - 1, amt, coins));\n\n return ans;\n }\n\n int coinChange(vector<int>& coins, int amt) {\n sort(coins.begin(), coins.end());\n int n = coins.size();\n vector<vector<int>> dp(n + 1, vector<int>(amt + 1, INT_MAX - 1));\n int off = 1;\n vector<int> prev(amt + 1, INT_MAX - 1);\n vector<int> curr(amt + 1);\n prev[0] = 0;\n for (int i = 0; i < n; i++) {\n fill(curr.begin() + 1, curr.end(), INT_MAX - 1);\n curr[0] = 0;\n for (int j = 1; j <= amt; j++) {\n if (coins[i] <= j) {\n curr[j] = min(1 + curr[j - coins[i]], curr[j]);\n }\n curr[j] = min(curr[j], prev[j]);\n }\n prev = curr;\n }\n\n return curr[amt] == INT_MAX - 1 ? -1 : curr[amt];\n }\n};", "memory": "59836" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int sol(int i,long s,int amount,vector<int>&coins,vector<vector<int>>&v)\n {\n int n = coins.size();\n if(v[i][s]!=-1) return v[i][s];\n if(i==n-1)\n {\n if((amount-s)%coins[i]==0)\n {\n return (amount-s)/coins[i];\n }\n return 9999;\n }\n\n // use pick non pick algorithm\n int take=INT_MAX,nonTake;\n\n s+=coins[i];\n if(s<=amount && coins[i]<=amount)\n {\n take = 1+sol(i,s,amount,coins,v);\n } \n\n s-=coins[i];\n nonTake = sol(i+1,s,amount,coins,v);\n\n return v[i][s]=min(take,nonTake);\n }\n\n int coinChange(vector<int>& coins, int amount) \n {\n if(amount == 0) return 0;\n vector<vector<int>> v(coins.size()+3,vector<int>(amount+5,-1));\n int ans = sol(0,0,amount,coins,v);\n if(ans == 9999) return -1;// this value is from base condition which\n //we return when we don't find the target amount\n return ans;\n }\n};", "memory": "59836" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int bfs(vector<int> &coins, int amount)\n {\n queue<pair<int,int>> q;\n\n vector<bool> isVisited(amount+1,false);\n\n for(int i=0;i<coins.size();i++)\n {\n if(coins[i]<=amount)\n {\n q.push({coins[i],1});\n }\n }\n\n while(!q.empty())\n {\n int cur = q.front().first;\n int cnt = q.front().second;\n q.pop();\n\n if(isVisited[cur]) continue;\n if(cur == amount) return cnt;\n\n isVisited[cur] = true;\n\n for(int i=0;i<coins.size();i++)\n { \n if(coins[i]>amount) continue;\n int next = coins[i] + cur;\n if(next<0||next>amount||isVisited[next]) continue;\n\n q.push({next,cnt+1});\n }\n }\n\n return -1;\n }\n int coinChange(vector<int>& coins, int amount) {\n \n sort(coins.begin(),coins.end());\n\n if(amount == 0) return 0;\n if(coins[0]>amount)\n {\n return -1;\n }\n\n return bfs(coins,amount);\n }\n};", "memory": "60690" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int ind, int amt, vector<int>& cns, vector<vector<int>>& dp) {\n if(amt==0){\n return 0;\n }\n if(ind>=cns.size()){\n return 1e9;\n }\n if(dp[ind][amt]!=-1){\n return dp[ind][amt];\n }\n if(amt>=cns[ind]){\n return dp[ind][amt]=min({1+solve(ind,amt-cns[ind],cns,dp) , 1+solve(ind+1,amt-cns[ind],cns,dp) , solve(ind+1,amt,cns,dp)});\n } else{\n return dp[ind][amt]=solve(ind+1,amt,cns,dp);\n }\n }\n int coinChange(vector<int>& cns, int amount) {\n if(amount ==0){\n return 0;\n }\n vector<vector<int>> dp(cns.size()+3, vector<int>(amount+3 , -1));\n int ta = solve(0,amount,cns,dp);\n return ta != 1e9?ta:-1;\n }\n};", "memory": "61544" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n pq.push({0,amount});\n unordered_set<int>st;\n st.insert(amount);\n while(!pq.empty()){\n int coin=pq.top().first;\n int amt=pq.top().second;\n // cout<<coin<<\" \"<<amt<<endl;\n pq.pop();\n if(amt==0)return coin;\n for(int j:coins){\n if(amt-j==0)return coin+1;\n else if(amt-j>0 && st.find(amt-j)==st.end()){\n pq.push({coin+1,amt-j});\n st.insert(amt-j);\n }\n }\n }\n return -1;\n }\n};", "memory": "62398" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n\npublic:\n\n int coinChange(vector<int>& coins, int amount) {\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n pq.push({0,amount});\n\n unordered_set<int>st;\n\n st.insert(amount);\n\n while(!pq.empty()){\n\n int coin=pq.top().first;\n\n int amt=pq.top().second;\n\n // cout<<coin<<\" \"<<amt<<endl;\n\n pq.pop();\n\n if(amt==0)return coin;\n\n for(int j:coins){\n\n if(amt-j==0)return coin+1;\n\n else if(amt-j>0 && st.find(amt-j)==st.end()){\n\n pq.push({coin+1,amt-j});\n\n st.insert(amt-j);\n\n }\n\n }\n\n }\n\n return -1;\n\n }\n\n};\n", "memory": "62398" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "unordered_map<int, pair<int, int>> store;\nclass Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n sort(coins.begin(), coins.end(), greater<int>());\n store.clear();\n\n return change(coins, amount, 0);\n }\n int change(vector<int>& coins, int amount, int index) {\n if(amount==0) return 0;\n if(amount<0) return -1;\n\n int mini=INT_MAX;\n bool flag=false;\n for(int i=index; i<coins.size(); i++){\n \n int out;\n if(store.find(amount-coins[i])!=store.end() && store[amount-coins[i]].second<=i){\n out=store[amount-coins[i]].first;\n }else{\n out= change(coins, amount-coins[i], i);\n store[amount-coins[i]]={out, i};\n }\n if(out==-1)continue;\n mini=min(out, mini);\n flag=true;\n }\n if(flag){\n return mini+1;\n }\n return -1;\n }\n};", "memory": "63251" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n\n unordered_map<int, unordered_map<int,int>> resMap;\n unordered_map<int, int> resMap2;\npublic:\n int coinChange(vector<int>& coins, int amount, int end) {\n\n \n if(amount == 0)\n {\n return 0;\n }\n\n if(amount < 0)\n {\n return -1;\n }\n\n if(end == -1 )\n {\n return -1;\n }\n\n if(resMap.find(end) != resMap.end() && resMap[end].find(amount) != resMap[end].end())\n {\n return resMap[end][amount];\n }\n\n // cout<<coins[end]<<endl;\n\n int result =0;\n if(amount%coins[end] == 0)\n {\n resMap[end][amount] =amount/coins[end];\n return amount/coins[end];\n }\n else{\n\n int minCoins = INT_MAX;\n for(int i = amount/coins[end]; i>=0; --i)\n {\n int nextCoins = coinChange(coins, amount - i*coins[end],end - 1);\n // cout<<coins[end]<<\"::\"<<i<<\"::\"<<nextCoins<<endl;\n if(nextCoins != -1 && minCoins > i+nextCoins)\n {\n minCoins = i+nextCoins;\n }\n\n }\n\n if ( minCoins == INT_MAX)\n {\n resMap[end][amount] =-1;\n return -1;\n }\n else\n {\n resMap[end][amount] =minCoins;\n return minCoins;\n }\n }\n\n\n \n }\n int coinChange2(vector<int>& coins, int amount){\n\n if(amount ==0 )\n {\n return 0;\n }\n\n if(amount < coins[0])\n {\n return -1;\n }\n\n if(resMap2.find(amount) != resMap2.end())\n {\n return resMap2[amount];\n }\n\n int mincoins = INT_MAX;\n for(int idx = coins.size() -1; idx >=0; idx-- )\n {\n int nextcoins = amount % coins[idx] == 0 ? amount/coins[idx] -1 : coinChange2(coins, amount - coins[idx]);\n\n if(nextcoins != -1 && mincoins > nextcoins+1 )\n {\n mincoins = nextcoins+1;\n }\n\n }\n\n if(mincoins == INT_MAX)\n {\n resMap2[amount] =-1;\n return -1;\n }\n else{\n resMap2[amount] = mincoins;\n return mincoins;\n }\n\n }\n int coinChange(vector<int>& coins, int amount) {\n \n sort(coins.begin(), coins.end());\n // return coinChange(coins, amount,coins.size()-1);\n return coinChange2(coins, amount);\n }\n};", "memory": "64105" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<double>dp;\n int helper(vector<int>& coins,int amount,double total){\n if(amount==total){\n return 0;\n }\n if(total>amount){\n return -1;\n }\n\n if(dp[total]!=-2){\n return dp[total];\n }\n else{\n int ans=INT_MAX;\n for(int i=0;i<coins.size();i++){\n int temp=helper(coins,amount,total+coins[i]);\n if(temp!=-1){\n ans=min(ans,temp+1);\n }\n }\n if(ans==INT_MAX){\n return dp[total]= (-1);\n }\n return dp[total]=ans;\n }\n }\n int coinChange(vector<int>& coins, double amount) {\n for(int i=0;i<10005;i++){\n dp.push_back(-2);\n }\n int ans=helper(coins,amount,0);\n return ans;\n }\n};", "memory": "64105" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n unordered_map<int, int> dp;\n dp[0] = 0;\n for (int i = 1; i < amount + 1; i++) {\n for(int coin : coins) {\n if (dp.find(i-coin) != dp.end()) {\n if (dp.find(i) != dp.end()) {\n dp[i] = min(dp[i], dp[i-coin] + 1);\n } else {\n dp[i] = dp[i-coin] + 1;\n }\n }\n }\n }\n if (dp.find(amount) != dp.end()) {\n return dp[amount];\n } else {\n return -1;\n }\n }\n};", "memory": "64959" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n unordered_map<int, int> dp;\n dp[0] = 0;\n sort(begin(coins), end(coins));\n for (int i = 1; i < amount + 1; i++) {\n for(int coin : coins) {\n if (i - coin < 0) break;\n if (dp.find(i-coin) != dp.end()) {\n if (dp.find(i) != dp.end()) {\n dp[i] = min(dp[i], dp[i-coin] + 1);\n } else {\n dp[i] = dp[i-coin] + 1;\n }\n }\n }\n }\n if (dp.find(amount) != dp.end()) {\n return dp[amount];\n } else {\n return -1;\n }\n }\n};", "memory": "64959" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n // return dp(coins, amount);\n return bfs(coins, amount);\n }\nprivate:\n int dp(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n\n vector<int> dp(amount+1, amount+1);\n dp[0] = 0;\n\n for (int i = 1; i <= amount; ++i) {\n for (int coin : coins) {\n if (i >= coin) {\n dp[i] = min(dp[i], dp[i - coin] + 1);\n }\n }\n }\n if (dp[amount] == amount + 1) return -1;\n return dp[amount];\n }\n\n int bfs(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n \n // vector<bool> seen(amount + 1, false);\n unordered_set<int> seen;\n queue<int> q;\n int count = 0;\n \n q.push(0);\n \n while (!q.empty()) {\n int size = q.size();\n count++;\n \n for (int i = 0; i < size; i++) {\n int curr = q.front();\n q.pop();\n \n for (int coin : coins) {\n int next = curr + coin;\n if (next == amount) return count;\n // if (next < amount && !seen[next]) {\n if (next < amount && seen.count(next) == 0) {\n seen.insert(next);\n q.push(next);\n }\n }\n }\n }\n return -1;\n }\n};\n", "memory": "65813" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n\n queue<int> q;\n q.push(0);\n unordered_set<int> discovered;\n int level = 1;\n\n while (!q.empty()) {\n int sz = q.size();\n for (int i = 0; i < sz; i++) {\n int u = q.front();\n q.pop();\n\n for (int coin : coins) {\n int v = u + coin;\n if (v == amount) return level;\n\n if (v < amount && !discovered.count(v)) {\n q.push(v);\n discovered.insert(v);\n }\n }\n }\n level++;\n }\n return -1;\n }\n};", "memory": "65813" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n unordered_map<int, int> dp;\n dp[0] = 0;\n \n for (const auto& coin : coins){\n for (int x = coin; x <= amount; x++){\n if (dp.find(x) == dp.end()){\n dp[x] = INT_MAX;\n }\n \n if (dp.find(x-coin) == dp.end() || dp[x-coin] == INT_MAX){\n dp[x-coin] = INT_MAX;\n }\n else {\n dp[x] = min(dp[x], dp[x - coin] + 1); \n }\n }\n }\n return (dp.find(amount) == dp.end() || dp[amount] == INT_MAX)? -1 : dp[amount];\n }\n};", "memory": "71789" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int coinChange(vector<int>& coins, int amount) {\n queue<pair<int,int>>q;\n q.push({amount,0});\n unordered_set<int>s;\n s.insert(amount);\n\n while(!q.empty()){\n auto [sum,num]=q.front();\n q.pop();\n if(sum==0)return num;\n\n for(int i=0;i<coins.size();i++){\n if(s.find(sum-coins[i])==s.end() && sum-coins[i]>=0){\n q.push({sum-coins[i],num+1});\n s.insert(sum-coins[i]);\n }\n }\n }\n return -1;\n \n }\n};", "memory": "71789" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int getCoins(vector<int>& coins, int amount,unordered_map<int,int>& mp) {\n if(amount == 0)\n return 0;\n if(mp[amount] != 0)\n return mp[amount];\n else {\n mp[amount] = INT_MAX;\n }\n for(int i = 1;i<= amount;i++) {\n mp[i] = INT_MAX;\n for(int coin: coins) {\n if(i-coin >= 0){\n int val = getCoins(coins,i-coin,mp)+1;\n if(val == 0)\n val = INT_MAX;\n mp[i]= min(val,mp[i]);\n }\n }\n if(mp[i] == INT_MAX)\n mp[i] = -1;\n\n }\n return mp[amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n std::sort(coins.rbegin(),coins.rend());\n unordered_map<int,int>mp;\n mp[0] = 0;\n return getCoins(coins,amount,mp);\n\n }\n};", "memory": "72643" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int getCoins(vector<int>& coins, int amount,unordered_map<int,int>& mp) {\n if(amount == 0)\n return 0;\n if(mp[amount] != 0)\n return mp[amount];\n else {\n mp[amount] = INT_MAX;\n }\n for(int i = 1;i<= amount;i++) {\n mp[i] = INT_MAX;\n for(int coin: coins) {\n if(i-coin >= 0){\n int val = getCoins(coins,i-coin,mp)+1;\n if(val == 0)\n val = INT_MAX;\n mp[i]= min(val,mp[i]);\n }\n }\n if(mp[i] == INT_MAX)\n mp[i] = -1;\n\n }\n return mp[amount];\n }\n\n int coinChange(vector<int>& coins, int amount) {\n std::sort(coins.rbegin(),coins.rend());\n unordered_map<int,int>mp;\n mp[0] = 0;\n return getCoins(coins,amount,mp);\n\n }\n};", "memory": "72643" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<int,int> cache;\n\n int solve(vector<int>& coins, int amount){\n if (amount ==0) return 0;\n if (amount < coins.back()) return -1;\n\n auto it = cache.find(amount);\n if (it != cache.end()) return it->second;\n\n int ans = -1;\n for(int c : coins){\n if (amount ==c) {\n ans = 0;\n break;\n }\n if (amount >c){\n int subAns = solve(coins, amount-c);\n if (subAns >= 0){\n if (ans <0 || ans >subAns){\n ans = subAns;\n }\n }\n }\n }\n\n if (ans >=0) ans ++;\n\n cache[amount] = ans;\n return ans;\n }\n\n int coinChange(vector<int>& coins, int amount) {\n sort(coins.begin(), coins.end(), greater<int>());\n return solve(coins, amount);\n }\n};", "memory": "73496" }
322
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p> <p>You may assume that you have an infinite number of each kind of coin.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> coins = [1,2,5], amount = 11 <strong>Output:</strong> 3 <strong>Explanation:</strong> 11 = 5 + 5 + 1 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> coins = [2], amount = 3 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> coins = [1], amount = 0 <strong>Output:</strong> 0 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 12</code></li> <li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n map<int, int> store;\npublic:\n int coinChange(vector<int>& coins, int amount) {\n if (amount == 0) return 0;\n\n bool notPossible = true;\n for (auto c : coins) {\n notPossible = notPossible && (c > amount);\n }\n if (notPossible) return -1;\n\n if(store.count(amount)) return store[amount];\n\n int minCount = 10000000;\n for (auto c : coins) {\n if (amount - c >= 0) {\n int curr = coinChange(coins, amount - c);\n if(curr != -1) minCount = minCount > 1 + curr ? 1 + curr : minCount; \n }\n }\n if (minCount == 10000000) {\n store[amount] = -1;\n }\n else{\n store[amount] = minCount;\n }\n return store[amount];\n \n }\n};", "memory": "73496" }