id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int hehe(int i,int j,string &s,vector<vector<int>>& dp){\n if(j<i) return 0;\n if(i==j) return 1;\n\n if(dp[i][j]!=-1) return dp[i][j];\n if(s[i]==s[j]){\n return 2+hehe(i+1,j-1,s,dp);\n }\n else{\n return dp[i][j]=max(hehe(i+1,j,s,dp),hehe(i,j-1,s,dp));\n }\n }\n\n int longestPalindromeSubseq(string s) {\n int n=s.size();\n vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n return hehe(0,n-1,s,dp);\n }\n};",
"memory": "74400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int find(string &s, vector<vector<int>> &dp, int start, int end) {\n if(start == end) return 1;\n if(start>end) return 0;\n if(dp[start][end] != -1) return dp[start][end];\n if(s[start] == s[end]) {\n dp[start][end] = 2+find(s, dp, start+1, end-1);\n } else {\n dp[start][end] = max(find(s, dp, start+1, end), find(s, dp, start, end-1));\n }\n return dp[start][end];\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n vector<vector<int>> dp(n, vector<int> (n, -1));\n return find(s, dp, 0, n-1);\n }\n};",
"memory": "74400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n /*\n from/to c b b d\n c 1 1 2 2\n b 1 2 2\n b 1 1\n d 1\n \n \n */\n int n = s.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for(int i = n-1; i>=0; i--) {\n for(int j = i; j<n; j++) {\n if(i==j) dp[i][j] = 1;\n else if(i+1==j) dp[i][j] = s.at(i)==s.at(j)? 2 : 1;\n else if(s.at(i)==s.at(j)) dp[i][j] = 2 + dp[i+1][j-1];\n else dp[i][j] = max(dp[i+1][j], dp[i][j-1]);\n }\n }\n return dp[0].back();\n }\n};",
"memory": "74500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n //dp[i][j] : lens of subsequence\n int f(int i,int j,string& s)\n {\n if(i == j)//only one char\n return 1;\n else if(j - i == 1) // two char\n return ((s[i] == s[j]) ? 2 : 1);\n else if(dp[i][j] != -1)\n return dp[i][j];\n \n else \n {\n int mx = -1;// more than two\n mx = f(i+1,j-1,s) + 2 * (s[i] == s[j]);\n mx = max(mx,max(f(i+1,j,s),f(i,j-1,s)));\n return dp[i][j] = mx;\n }\n }\n int longestPalindromeSubseq(string s) {\n int len = s.size();\n dp.resize(len);\n for(int i = 0;i < len;i++)\n {\n dp[i].resize(len);\n for(int j = 0;j < len;j++)\n dp[i][j] = -1;\n }\n int max_len = 1;\n for(int i = 0;i < len;i++)\n {\n for(int j = i;j < len;j++)\n {\n max_len = max(max_len,f(i,j,s));\n }\n }\n return max_len;\n }\n};",
"memory": "74500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int solve(string &s, int i, int j){\n if(i>j) return 0;\n if(i == j) return 1;\n\n int ans = 0;\n if(s[i] == s[j]){\n ans = 2 + solve(s, i+1,j-1);\n }\n else{\n ans = max(solve(s, i+1, j), solve(s, i, j-1));\n }\n\n return ans;\n }\n\n int solveByMem(string &s, int i, int j, vector<vector<int>> &dp){\n if(i>j) return 0;\n if(i == j) return 1;\n\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n \n int ans = 0;\n if(s[i] == s[j]){\n ans = 2 + solveByMem(s, i+1,j-1, dp);\n }\n else{\n ans = max(solveByMem(s, i+1, j, dp), solveByMem(s, i, j-1, dp));\n }\n\n dp[i][j] = ans;\n return dp[i][j];\n }\n\n int longestPalindromeSubseq(string s) {\n int n = s.length();\n vector<vector<int>> dp(n+1, vector<int>(n+1, -1));\n int ans = solveByMem(s, 0, n-1, dp);\n return ans;\n }\n};",
"memory": "74600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string t = s;\n reverse(t.begin(),t.end());\n\n vector<vector<int>>dp(n+1,vector<int>(n+1,0));\n\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(s[i-1]==t[j-1])\n dp[i][j] = 1+dp[i-1][j-1];\n else\n dp[i][j] = max(dp[i-1][j],dp[i][j-1]);\n }\n }\n\n return dp[n][n];\n }\n};",
"memory": "74600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int memo(string &s1, string &s2, int i, int j, vector<vector<int>> &dp){\n if(i<0 || j<0) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n if(s1[i] == s2[j]) return dp[i][j] = 1 + memo(s1, s2, i-1, j-1, dp);\n return dp[i][j] = max(memo(s1, s2, i-1, j, dp), memo(s1, s2, i, j-1, dp));\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string s2 = s; reverse(s2.begin(), s2.end());\n vector<vector<int>> dp(n, vector<int> (n, -1));\n return memo(s, s2, n-1, n-1, dp);\n }\n};",
"memory": "74700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int f(int i,int j,string &s,vector<vector<int>>& dp){\n if(j<i) return 0;\n if(i==j) return 1;\n\n if(dp[i][j]!=-1) return dp[i][j];\n if(s[i]==s[j]){\n return 2+f(i+1,j-1,s,dp);\n }\n else{\n return dp[i][j]=max(f(i+1,j,s,dp),f(i,j-1,s,dp));\n }\n }\n\n int longestPalindromeSubseq(string s) {\n int n=s.size();\n vector<vector<int>> dp(n+1,vector<int>(n+1,-1));\n return f(0,n-1,s,dp);\n }\n};",
"memory": "74700"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution \n{\npublic:\n int findMinMoves(vector<int>& machines) \n {\n int sum = 0;\n int n = machines.size();\n int difference = 0;\n for (int a : machines) sum+=a;\n if (sum % n != 0) return -1;\n int goal = sum/n;\n cout << \"goal: \" << goal << endl; \n int longest_to_fill = 0;\n for (int a : machines) \n {\n difference = difference + (goal-a);\n cout << difference << endl;\n longest_to_fill = max(longest_to_fill, abs(difference));\n longest_to_fill = max(longest_to_fill, max(difference,a-goal));\n }\n return longest_to_fill;\n }\n};",
"memory": "15400"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int findMinMoves(vector<int>& machines) {\n int sum=0;\n for(auto ele:machines)sum+=ele;\n int n=machines.size();\n if(sum%n!=-0)return -1;\n int z=sum/n;\n int ans=0;\n int temp=0;\n for(auto ele:machines){\n temp+=ele-z;\n ans=max(ans,abs(temp));\n cout<<temp<<endl;\n }\n for(auto ele:machines){\n if(ele>z)ans=max(ans,abs(ele-z));\n }\n return ans;\n }\n};",
"memory": "15500"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int findMinMoves(vector<int>& machines) {\n int sum=0;\n for(auto ele:machines)sum+=ele;\n int n=machines.size();\n if(sum%n!=-0)return -1;\n int z=sum/n,ans=0,temp=0;\n for(auto ele:machines){\n temp+=ele-z;\n ans=max(ans,abs(temp));\n if(ele>z)ans=max(ans,abs(ele-z));\n }\n return ans;\n }\n};",
"memory": "15500"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinMoves(vector<int>& ms) {\n int n = ms.size();\n int sum = 0;\n for (int &i : ms) sum += i;\n if (sum % n) return -1;\n int t = sum / n;\n int ans = 0, toRight = 0;\n for (int &i : ms) {\n toRight = toRight + i - t;\n ans = max(ans, max(abs(toRight), i - t));\n }\n return ans;\n }\n};",
"memory": "15600"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n int findMinMoves(vector<int>& machines) {\n const int dresses = accumulate(machines.begin(), machines.end(), 0);\n if (dresses % machines.size() != 0)\n return -1;\n\n int ans = 0;\n int inout = 0;\n const int average = dresses / machines.size();\n\n for (const int dress : machines) {\n inout += dress - average;\n ans = max({ans, abs(inout), dress - average});\n }\n\n return ans;\n }\n};",
"memory": "15700"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinMoves(vector<int>& machines) {\n int total = accumulate(machines.begin(), machines.end(), 0);\n int n = machines.size();\n \n // Если общее количество платьев не делится на количество машин, решение невозможно\n if (total % n != 0) return -1;\n \n int target = total / n; // Целевая средняя нагрузка на каждую машину\n int result = 0, runningSum = 0;\n \n // Пробегаем по всем машинам и вычисляем минимальные перемещения\n for (int dresses : machines) {\n // Считаем разницу текущей машины с целевым значением\n int diff = dresses - target;\n runningSum += diff; // Считаем кумулятивное количество перемещений\n result = max(result, max(abs(runningSum), diff));\n }\n \n return result;\n }\n};\n",
"memory": "15700"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMinMoves(vector<int>& a) {\n int n = a.size();\n int sum = 0;\n for(int i =0;i<n;++i) sum+=a[i];\n if(sum%n!=0) return -1;\n int avg = sum/n;\n int ans = 0,cum=0,curr=0;\n for(int i = 0;i<n;++i){\n curr = a[i] - avg;\n cum += curr;\n ans = max({ans,abs(cum),curr});\n }\n return ans;\n }\n};",
"memory": "15800"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMinMoves(vector<int>& machines) {\n\n int sum = 0;\n for(int x: machines){\n sum += x;\n }\n if(sum % machines.size() != 0) return -1;\n\n int target = sum / machines.size();\n int left = 0;\n vector<int> diff(machines.size());\n for(int i = 0; i < machines.size(); i++){\n left += machines[i];\n int tomove = left - (i+1)*target;\n diff[i] = tomove;\n }\n\n auto ld = [&diff](int i){\n if(i == 0) return 0;\n else return diff[i-1];\n };\n\n auto rd = [&diff](int i){\n return diff[i];\n };\n\n int moves = 0;\n for(int i = 0; i < machines.size(); i++){\n int val = 0;\n if(machines[i] > target){\n val = abs(ld(i) - rd(i));\n }\n else{\n val = max(abs(ld(i)), abs(rd(i)));\n }\n moves = max(moves, val);\n } \n \n return moves;\n }\n};",
"memory": "15900"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMinMoves(vector<int>& machines) {\n int n=machines.size();\n int total=accumulate(machines.begin(),machines.end(),0);\n if(total%n!=0) return -1;\n vector<int> prefix(n+1,0);\n for(int i=0;i<n;i++) prefix[i+1]=machines[i]+prefix[i];\n int avg=total/n;\n int ans=0;\n for(int i=0;i<n;i++){\n int l=i*avg-prefix[i];\n int r=(n-i-1)*avg-(prefix[n]-prefix[i]-machines[i]);\n if(l>0 && r>0) ans=max(ans,abs(l)+abs(r));\n else ans=max(ans,max(abs(l),abs(r)));\n }\n return ans;\n }\n};",
"memory": "16000"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int max_sub(vector<int> &arr){\n int n = arr.size();\n vector<int> pref(n+1, 0);\n vector<int> suf(n+1, 0);\n int maxp=INT_MIN,maxs=INT_MIN,minp=INT_MAX,mins=INT_MAX;\n\n for(int i=0;i<n;i++){\n pref[i+1] = pref[i]+arr[i];\n if(abs(pref[i+1]) > maxp)maxp = abs(pref[i+1]);\n }\n for(int i=n-1;i>=0;i--){\n suf[i] = suf[i+1] + arr[i];\n if(abs(suf[i]) > maxs)maxs = abs(suf[i]);\n }\n cout << maxp << \" \" << maxs << endl;\n return(max(maxp,maxs));\n }\n\n int findMinMoves(vector<int>& mach) {\n int n = mach.size();\n if(n == 1)return(0);\n int sm = accumulate(mach.begin(), mach.end(), 0);\n if(sm%n != 0)return(-1);\n int target = sm/n;\n\n int ans = INT_MIN;\n vector<int> arr;\n bool isneg = mach[0] < target ? 1 : 0;\n int cur = abs(mach[0] - target);\n\n for(auto &t:mach){\n int c = t-target;\n if(c>=0)ans = max(ans,c);\n else ans = max(ans, (abs(c)+1)/2);\n }\n\n for(int i=1;i<n;i++){\n if(mach[i] < target && isneg){\n cur += target - mach[i];\n ans = max(ans, (cur+1)/2);\n }\n else if(mach[i] < target && !isneg){\n arr.push_back(cur);\n cur = target - mach[i];\n ans = max(ans, (cur+1)/2);\n isneg = 1;\n }\n else if(mach[i] >= target && !isneg){\n cur -= target - mach[i];\n ans = max(ans, (cur+1)/2);\n }\n else{\n arr.push_back(-cur);\n cur = mach[i] - target;\n ans = max(ans, (cur+1)/2);\n isneg = 0;\n }\n }\n if(isneg)arr.push_back(-cur);\n else arr.push_back(cur);\n\n return(max(ans, max_sub(arr)));\n }\n};",
"memory": "16100"
} |
517 | <p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> machines = [1,0,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,3,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> machines = [0,2,0]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
It's impossible to make all three washing machines have the same number of dresses.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == machines.length</code></li>
<li><code>1 <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMinMoves(vector<int>& machines) {\n int n = machines.size();\n int sum = 0;\n for(int i=0;i<n;i++){\n sum += machines[i];\n }\n if(sum%n != 0){\n return -1;\n }\n\n int x = sum/n;\n\n vector<int> left(n+2),right(n+2);\n for(int i=0;i<n;i++){\n left[i+1] = left[i] + machines[i];\n }\n\n for(int i=n-2;i>=0;i--){\n right[i] = right[i+1] + machines[i+1];\n }\n\n int ans = 0;\n for(int i=0;i<n;i++){\n int l=0, r=0;\n if(x*i - left[i] > 0){\n l = x*i - left[i];\n }\n if(x*(n-i-1) - right[i] > 0){\n r= x*(n-i-1) - right[i];\n }\n\n ans =max(ans,l+r);\n }\n return ans;\n\n\n }\n};",
"memory": "16100"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n /* space optimized dp */\n\n int dp[amount + 1];\n memset(dp, 0, sizeof dp);\n\n dp[0] = 1;\n\n for(auto& coin : coins)\n for(int sum = coin; sum <= amount; sum++)\n dp[sum] += dp[sum - coin];\n\n return dp[amount];\n }\n};",
"memory": "8245"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\n int dp[5003];\npublic:\n int change(int amount, vector<int>& coins) {\n dp[0] = 1;\n\n for(auto coin : coins) {\n for(int i=1;i<=amount;i++) {\n if (coin <= i) {\n dp[i] += dp[i - coin];\n }\n }\n }\n\n return dp[amount];\n }\n};",
"memory": "8245"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) \n {\n vector<int> dp(amount+1, 0);\n dp[0]=1;\n \n for(auto& coin: coins)\n {\n for(int i=coin;i<=amount;i++)\n dp[i]+=dp[i-coin];\n }\n\n return dp[amount];\n }\n};",
"memory": "8535"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n \n vector<int> dp(amount + 1, 0);\n dp[0] = 1;\n\n for(int num : coins){\n for(int i = 1; i <= amount; i++){\n if(i - num < 0) continue;\n dp[i] += dp[i - num];\n }\n }\n\n return dp[amount];\n }\n};",
"memory": "8535"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n vector<int> dp(amount+1);\n dp[0]=1;\n for(auto it:coins)\n {\n for(int i=it;i<=amount;i++)\n {\n dp[i]+=dp[i-it];\n }\n }\n\n return dp[amount];\n }\n};",
"memory": "8825"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n std::sort(coins.begin(), coins.end());\n vector<int> dp(amount+1, 0);\n dp[0] = 1;\n\n for (const int &i : coins) {\n\n for (int x = i; x <= amount; x++) {\n dp[x] += dp[x-i];\n }\n }\n /*\n -0 1 2 3 4 5-\n [1 0 0 0 0 0]\n [1 1 1 1 1 1]\n [1 1 2 2 3 3]\n [1 1 2 2 3 4]\n */\n return dp[amount];\n }\n};",
"memory": "8825"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int memo(int idx, int sum, int amount, vector<int>& coins, vector<vector<int>>& dp){\n if(idx == 0) return !((amount - sum) % coins[0]);\n if(dp[idx][sum] != -1) return dp[idx][sum];\n\n int take = 0, notTake = memo(idx - 1, sum, amount, coins, dp);\n if(sum + coins[idx] <= amount) \n take = memo(idx, sum + coins[idx], amount, coins, dp);\n\n return dp[idx][sum] = take + notTake;\n }\n\n int change(int amount, vector<int>& coins) {\n ios::sync_with_stdio(0); cin.tie(0);\n int n = coins.size();\n // vector<vector<int>> dp(n, vector<int>(amount + 1, -1));\n // for(auto& vec: dp) vec[amount] = 1;\n // return memo(n - 1, 0, amount, coins, dp);\n\n vector<int> prev(amount + 1, 0), cur(amount + 1, 0);\n for(int i = 0; i <= amount; i++) \n prev[i] = !((amount - i) % coins[0]);\n\n for(int idx = 1; idx < n; idx++){\n for(int sum = amount; sum >= 0; sum--){\n int take = 0, notTake = prev[sum];\n if(sum + coins[idx] <= amount) \n take = cur[sum + coins[idx]];\n\n cur[sum] = take + notTake;\n }\n prev = cur;\n }\n\n return prev[0];\n }\n};",
"memory": "9115"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int memo(int idx, int sum, int amount, vector<int>& coins, vector<vector<int>>& dp){\n if(idx == 0) return !((amount - sum) % coins[0]);\n if(dp[idx][sum] != -1) return dp[idx][sum];\n\n int take = 0, notTake = memo(idx - 1, sum, amount, coins, dp);\n if(sum + coins[idx] <= amount) \n take = memo(idx, sum + coins[idx], amount, coins, dp);\n\n return dp[idx][sum] = take + notTake;\n }\n\n int change(int amount, vector<int>& coins) {\n ios::sync_with_stdio(0); cin.tie(0);\n int n = coins.size();\n // vector<vector<int>> dp(n, vector<int>(amount + 1, -1));\n // for(auto& vec: dp) vec[amount] = 1;\n // return memo(n - 1, 0, amount, coins, dp);\n\n vector<int> prev(amount + 1, 0), cur(amount + 1, 0);\n for(int i = 0; i <= amount; i++) \n prev[i] = !((amount - i) % coins[0]);\n\n for(int idx = 1; idx < n; idx++){\n for(int sum = amount; sum >= 0; sum--){\n int take = 0, notTake = prev[sum];\n if(sum + coins[idx] <= amount) \n take = cur[sum + coins[idx]];\n\n cur[sum] = take + notTake;\n }\n prev = cur;\n }\n\n return prev[0];\n }\n};",
"memory": "9115"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n /*\n DP[i][a]: # of combos to get to a that uses coins[<=i].coins[i] may or may not in there. \n \n - Combos that have coins[i], we remove it\n if (a - coins[i] >= 0)\n = DP[i][a - coins[i]], after removal, i may still be in there.\n +\n - Combos that doesnt have coins[i]\n = DP[i - 1][a]\n\n base cases\n DP[0][*] = 1 if * % coins[0] == 0\n */\n\n ios_base::sync_with_stdio(0);\n cin.tie(nullptr);\n int DP[5001] = {0};\n\n for (int a = 0; a <= amount;a++) {\n if (a % coins[0] == 0) {\n DP[a] = 1;\n }\n }\n\n size_t s = coins.size();\n\n for (int i = 1;i < s;i++) {\n for (int a = 0;a <= amount;a++) {\n if (a - coins[i] >= 0) {\n DP[a] += DP[a - coins[i]];\n }\n }\n }\n\n return DP[amount];\n }\n};",
"memory": "9405"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> coins;\n int n;\n\n vector<vector<int>> memo;\n\n long long c(int i, int target) {\n if (target == 0) return 1;\n if (i == n) return 0;\n if (memo[i][target] != -1) return memo[i][target];\n\n long long res = 0;\n res += c(i + 1, target);\n if (coins[i] <= target) res += c(i, target - coins[i]);\n return memo[i][target] = res;\n }\n\n long long bottomUp(int amount) {\n vector<vector<long long>> c(n + 1, vector<long long>(amount + 1, -1));\n for (int i = n; i >= 0; --i) {\n for (int target = 0; target <= amount; ++target) {\n if (target == 0) {\n c[i][target] = 1;\n continue;\n }\n if (i == n) {\n c[i][target] = 0;\n continue;\n }\n\n long long res = 0;\n res += c[i + 1][target];\n if (coins[i] <= target) res += c[i][target - coins[i]];\n c[i][target] = res; \n }\n }\n return c[0][amount];\n } \n\n long long bottomUpSpaceOptimized(int amount) {\n // c -> c[i]\n // cprev -> c[i + 1]\n vector<long long> c(amount + 1, -1);\n vector<long long> cprev(amount + 1, -1);\n for (int i = n; i >= 0; --i) {\n for (int target = 0; target <= amount; ++target) {\n if (target == 0) {\n c[target] = 1;\n continue;\n }\n if (i == n) {\n c[target] = 0;\n continue;\n }\n\n long long res = 0;\n res += cprev[target];\n if (coins[i] <= target) res += c[target - coins[i]];\n c[target] = res; \n }\n // shift pointers\n cprev = c;\n }\n return c[amount];\n } \n\n\n int change(int target, vector<int>& coins) {\n this->coins = coins;\n n = coins.size(); \n\n\n // memo = vector<vector<int>>(n, vector<int>(target + 1, -1));\n // return c(0, target);\n\n // return bottomUp(target);\n return bottomUpSpaceOptimized(target);\n\n }\n};",
"memory": "9405"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n auto const max_amount = 5000;\n std::vector<uint64_t> ways(max_amount + 1, 0);\n\n ways[0] = 1;\n\n for(auto const& coin: coins) {\n for(int i=0; i<=max_amount-coin; ++i) {\n ways[coin+i] += ways[i];\n }\n }\n\n return ways[amount];\n }\n};",
"memory": "9695"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int t, vector<int>& v) {\n\n vector<long>dp(5001,0);\n dp[0]=1;\n for(auto i: v){\n for(int j=0;j<=5000;j++){\n if(j+i<=5000) dp[j+i]+=dp[j];\n }\n }\n return dp[t];\n \n }\n};",
"memory": "9985"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int dp[1000000]; //set dp mean amount=i have dp[i] kinds of combination;\n memset(dp,0,sizeof(dp));\n dp[0] =1;\n\n for(auto c: coins){\n for(int i=1;i<=amount;i++){\n if(i-c>=0)\n dp[i] += dp[i-c];\n }\n }\n return dp[amount];\n }\n};\n",
"memory": "9985"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) \n {\n int n = coins.size();\n int dp[n+1][amount+1];\n // if amount is 0\n for(int i=0; i<=n; i++)\n {\n dp[i][0] = 1;\n }\n // if coins are zero except (0,0)\n for(int j=1; j<=amount; j++)\n {\n dp[0][j]=0;\n }\n\n for(int i=1; i<=n; i++)\n {\n for(int j=1; j<=amount; j++)\n {\n dp[i][j]=dp[i-1][j]; // not included - ignore last coin\n if(coins[i-1]<=j)\n {\n dp[i][j]+=dp[i][j-coins[i-1]]; // include in\n }\n }\n }\n return dp[n][amount];\n }\n};",
"memory": "10275"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int dp[coins.size()][amount+1];\n memset(dp,0,sizeof(dp));\n for(int i=0;i<=amount;i++) {\n if(i%coins[0]==0){\n dp[0][i]=1;\n }\n }\n for(int i=1;i<coins.size();i++){\n for(int j=0;j<=amount;j++){\n long notTaken=dp[i-1][j];\n long taken=0;\n if(coins[i]<=j){\n taken=dp[i][j-coins[i]];\n } \n dp[i][j]=taken+notTaken;\n }\n }\n return dp[coins.size()-1][amount];\n }\n \n};",
"memory": "10565"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins){\n int number_of_coins = coins.size();\n int dp[number_of_coins+1][amount+1];\n\n for(int i=0; i<=number_of_coins; i++){\n for (int j=0; j<=amount; j++){\n if (i==0 && j==0){\n dp[i][i]=0;\n }\n else if (i==0){\n dp[i][j]=0;\n }\n else if (j==0){\n dp[i][j]=1;\n }\n else if (coins[i-1]<=j){\n dp[i][j] = 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 return dp[number_of_coins][amount];\n}\n};",
"memory": "11725"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n vector<int> dp(amount+1,0);\n dp[0]=1;\n // for(int j=0;j<coins.size();j++){\n // dp[coins[j]]=1;\n // }\n int n=coins.size();\n int t[n+1][amount+1];\n\n for(int i=0;i<=n;i++){\n for(int j=0;j<=amount;j++){\n if(i==0){\n t[i][j]=0;\n }\n if(j==0){\n t[i][j]=1;\n }\n }\n }\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]=t[i][j-coins[i-1]]+t[i-1][j];\n }\n else{\n t[i][j]=t[i-1][j];\n }\n\n }\n }\n return t[n][amount];\n // for(int i=0;i<=amount;i++){\n // int sum=0;\n // for(auto coin:coins){\n // if(coin<=i){\n // dp[i]+=(dp[i-coin]);\n // }\n // }\n // //dp[i]+=sum;\n // cout<<dp[i]<<\" \"<<i<<endl;\n // }\n\n }\n};",
"memory": "12015"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n vector<int> dp(amount+1,0);\n dp[0]=1;\n // for(int j=0;j<coins.size();j++){\n // dp[coins[j]]=1;\n // }\n int n=coins.size();\n int t[n+1][amount+1];\n\n for(int i=0;i<=n;i++){\n for(int j=0;j<=amount;j++){\n if(i==0){\n t[i][j]=0;\n }\n if(j==0){\n t[i][j]=1;\n }\n }\n }\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]=t[i][j-coins[i-1]]+t[i-1][j];\n }\n else{\n t[i][j]=t[i-1][j];\n }\n\n }\n }\n return t[n][amount];\n // for(int i=0;i<=amount;i++){\n // int sum=0;\n // for(auto coin:coins){\n // if(coin<=i){\n // dp[i]+=(dp[i-coin]);\n // }\n // }\n // //dp[i]+=sum;\n // cout<<dp[i]<<\" \"<<i<<endl;\n // }\n\n }\n};",
"memory": "12015"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "int dp[302][5002];\n\n// int rec(int level,int left,vector<int>& coins){\n\n// if(level == n){\n// if(left == 0) return 1;\n// else return 0;\n// }\n\n// if(dp[level][left]!=-1) return dp[level][left];\n// int take = 0;\n// if(left >= coins[level]){\n// take = rec(level,left-coins[level],coins);\n// }\n// int dt = rec(level+1,left,coins);\n\n// return dp[level][left] = take + dt;\n// }\nclass Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n // vector<vector<int>> dp(n,vector<int>(amount+1,0));\n\n for(int level=n;level>=0;level--){\n for(int left =0 ;left<=amount;left++){\n \n if(level == n){\n if(left == 0) dp[level][left] = 1;\n else dp[level][left] = 0;\n }\n else{\n int take = 0;\n if(left >= coins[level]){\n take = dp[level][left-coins[level]];\n }\n int dt = dp[level+1][left];\n\n dp[level][left] = take + dt;\n }\n \n }\n }\n return dp[0][amount];\n }\n};",
"memory": "12305"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "const int MAX_N = 300 + 1;\nconst int MAX_T = 5000 + 1;\nint memory[MAX_N][MAX_T];\n\n\n\nclass Solution {\n vector<int> numbers;\npublic:\n\n int change_rec(int amount, vector<int> &coins) {\n numbers = coins;\n memset(memory, -1, sizeof(memory));\n return change(amount, coins);\n }\n\n int answer(int idx, int target) {\n if (target < 0)\n return 0;\n\n if (target == 0)\n return 1; // one valid path\n\n if (idx == -1)\n return 0; // can't find combination\n\n assert(memory[idx][target] != -1);\n return memory[idx][target];\n }\n\n int change(int amount, vector<int> &numbers) {\n memset(memory, -1, sizeof(memory));\n\n int sz = numbers.size();\n for (int idx = 0; idx < sz; ++idx) {\n\n for (int target = 0; target <= amount; ++target) {\n\n int leave = answer(idx - 1, target);\n int take = answer(idx, target - numbers[idx]);\n\n memory[idx][target] = leave + take;\n }\n }\n return memory[numbers.size() - 1][amount];\n }\n};\n\n\n",
"memory": "12595"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "int t[301][5002];\nclass Solution {\npublic:\n Solution()\n {\n memset(t,-1,sizeof(t));\n }\n int getans(vector<int>&coins,int amount,int n)\n {\n if(amount==0)\n return 1;\n if(n==0||amount<0)\n return 0;\n if(t[n][amount]!=-1)\n return t[n][amount];\n \n return t[n][amount]=((getans(coins,amount-coins[n-1],n))+getans(coins,amount,n-1));\n }\n int change(int amount, vector<int>& coins) \n {\n int n=coins.size();\n return getans(coins,amount,n);\n \n }\n};",
"memory": "12885"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "int dp[301][5001];\n\nint rec(int level,int left,vector<int>& coins){\n\n if(level < 0){\n if(left == 0) return 1;\n else return 0;\n }\n\n if(dp[level][left]!=-1) return dp[level][left];\n int take = 0;\n if(left >= coins[level]){\n take = rec(level,left-coins[level],coins);\n }\n int dt = rec(level-1,left,coins);\n\n return dp[level][left] = take + dt;\n}\nclass Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n memset(dp,-1,sizeof(dp));\n int n = coins.size();\n return rec(n-1,amount,coins);\n }\n};",
"memory": "13175"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "int dp[301][5001];\nclass Solution {\npublic:\nint fun(int id,int amount,vector<int>&coins)\n{\n if(amount==0)\n {\n return 1;\n }\n if(id>=coins.size()||amount<0)\n {\n return 0;\n }\n if(dp[id][amount]!=-1)\n {\n return dp[id][amount];\n }\n int nottake=fun(id+1,amount,coins);\n int take=0;\n if(amount-coins[id]>=0)\n {\n take=fun(id,amount-coins[id],coins);\n }\n return dp[id][amount]=take+nottake;\n}\n int change(int amount, vector<int>& coins) {\n memset(dp,-1,sizeof(dp));\n return fun(0,amount,coins);\n }\n};",
"memory": "13465"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint dp[300][5001];\n int change(int amount, vector<int>& coins) {\n memset(dp,-1,sizeof(dp));\n return func(0,amount,coins);\n }\n\n int func(int i,int amount, vector<int>& coins)\n {\n if(amount==0) return 1;\n if(i==coins.size()) return 0;\n if(dp[i][amount]!=-1) return dp[i][amount];\n int ans=0;\n int coin=coins[i];\n for(int j=0;j*coin<=amount;j++)\n {\n ans+=func(i+1,amount-j*coin,coins);\n }\n return dp[i][amount]=ans;\n }\n};",
"memory": "13755"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int dp[5001][301] = {0}, n = coins.size();\n\n dp[0][0] = 1;\n\n for (int i = 0; i <= amount; i++) {\n for (int j = 1; j <= n; ++j) {\n dp[i][j] = dp[i][j - 1];\n int left = i - coins[j - 1];\n if (left >= 0)\n dp[i][j] += dp[left][j];\n }\n }\n\n return dp[amount][n] ;\n }\n};",
"memory": "13755"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int dp[301][5001] = {0}, n = coins.size();\n\n dp[0][0] = 1;\n\n for (int i = 0; i <= amount; i++) {\n for (int j = 1; j <= n; ++j) {\n dp[j][i] = dp[j - 1][i];\n int left = i - coins[j - 1];\n if (left >= 0)\n dp[j][i] += dp[j][left];\n }\n }\n\n return dp[n][amount] ;\n }\n};",
"memory": "14045"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n\n int dp[300+1][5000+1];\n memset(dp, 0, sizeof(dp));\n\n for(int i =0; i <= coins.size(); i++)\n {\n for(int j =0; j <= amount;j++)\n {\n if(j==0)\n dp[i][j] = 1;\n //this means if amount is 0, then the only way is to skip the coin\n }\n }\n\n for(int i = 1; i <= coins.size(); i++)\n {\n for(int j = 1; j <= amount; j++)\n {\n if(coins[i-1] > j)\n {\n //skip for sure\n dp[i][j] = dp[i-1][j];\n }\n else\n {\n //sum of take and skip\n dp[i][j] = dp[i-1][j] + dp[i][j - coins[i-1]];\n }\n }\n }\n\n return dp[coins.size()][amount];\n\n }\n};",
"memory": "14335"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint dp[305][5005];\nint fn(int k,vector<int>&a,int i){\n if(i<=0||k<0)return 0;\n if(dp[i][k]!=-1)return dp[i][k];\n if(k==0)return 1;\n int p=fn(k-a[i-1],a,i);\n int q=fn(k,a,i-1);\n return dp[i][k]=p+q;\n}\n int change(int k, vector<int>& a) {\n memset(dp,-1,sizeof(dp));\n return fn(k,a,a.size());\n }\n};",
"memory": "14625"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int dp[5001][301];\n\n int helper(int amount,int pos, vector<int> &coins){\n if(amount<0) return 0;\n if(pos==0){\n return !(amount%coins[pos]);\n }\n if(dp[amount][pos]!=-1) return dp[amount][pos];\n int skip = helper(amount,pos-1,coins);\n int take = helper(amount-coins[pos],pos,coins);\n dp[amount][pos]=skip+take;\n return dp[amount][pos];\n }\n\n int change(int amount,vector<int>& coins) {\n memset(dp,-1,sizeof(dp));\n return helper(amount,coins.size()-1,coins);\n\n }\n};",
"memory": "14915"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int n;\n int memo[301][5001];\n int changeUtil(vector<int> &coins, int amount, int idx){\n\n if(idx == n-1){\n if(amount % coins[idx] == 0){\n return 1;\n } return 0;\n }\n\n if(memo[idx][amount] != -1) return memo[idx][amount];\n\n int notTake = changeUtil(coins, amount, idx + 1);\n\n int take = 0;\n\n if(amount - coins[idx] >= 0){\n take = changeUtil(coins, amount - coins[idx], idx);\n }\n\n return memo[idx][amount] = take + notTake;\n }\n int change(int amount, vector<int>& coins) {\n n = coins.size();\n memset(memo, -1, sizeof(memo));\n return changeUtil(coins, amount, 0);\n }\n};",
"memory": "15205"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[5001][301];\n int rec(vector<int>& coins,int amount,int n,int i){\n if(i>=n){\n if(amount==0) return 1;\n return 0;\n }\n if(dp[amount][i]!=-1) return dp[amount][i];\n int take =0;\n if(coins[i]<=amount){\n take = rec(coins,amount-coins[i],n,i);\n }\n int ntake=0;\n ntake = rec(coins,amount,n,i+1);\n return dp[amount][i]=take+ntake;\n }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n memset(dp,-1,sizeof(dp));\n return rec(coins,amount,n,0);\n }\n};",
"memory": "15495"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int n;\n int t[301][5001];\n \n int solve(int i, vector<int>& coins, int amount) {\n \n if(amount == 0)\n return t[i][amount] = 1;\n \n if(i == n || amount < 0)\n return 0;\n \n if(t[i][amount] != -1)\n return t[i][amount];\n \n if(coins[i] > amount)\n return t[i][amount] = solve(i+1, coins, amount);\n \n int take = solve(i, coins, amount-coins[i]);\n int skip = solve(i+1, coins, amount);\n \n return t[i][amount] = take+skip;\n \n }\n \n int change(int amount, vector<int>& coins) {\n n = coins.size();\n memset(t, -1, sizeof(t));\n return solve(0, coins, amount);\n }\n};\n",
"memory": "15495"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[310][5010];\n int rec(int i,int rem,vector<int>& coins){\n if(rem==0) return 1;\n if(i==coins.size()){\n if(rem==0) return 1;\n return 0;\n }\n if(dp[i][rem]!=-1) return dp[i][rem];\n int ans=0;\n ans=ans+(rec(i+1,rem,coins));\n if(rem-coins[i]>=0){\n ans=ans+rec(i,rem-coins[i],coins);\n }\n return dp[i][rem]=ans;\n }\n\n int change(int amount, vector<int>& coins) {\n memset(dp,-1,sizeof(dp));\n int ans=rec(0,amount,coins);\n return ans;\n }\n};",
"memory": "15785"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[310][5010];\n int rec(int i,int rem,vector<int>& coins){\n if(rem==0) return 1;\n if(i==coins.size()){\n if(rem==0) return 1;\n return 0;\n }\n if(dp[i][rem]!=-1) return dp[i][rem];\n int ans=0;\n ans=ans+(rec(i+1,rem,coins));\n if(rem-coins[i]>=0){\n ans=ans+rec(i,rem-coins[i],coins);\n }\n return dp[i][rem]=ans;\n }\n\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n memset(dp,-1,sizeof(dp));\n int ans=rec(0,amount,coins);\n return ans;\n vector<vector<int>> dp(n+1,vector<int>(amount+1,0));\n for(int i=0;i<=n;i++){\n dp[i][0]=1;\n }\n //if(coins[n-1]<=amount) dp[n-1][coins[n-1]]=1;\n\n for(int i=n-1;i>=0;i--){\n for(int rem=0;rem<=amount;rem++){\n dp[i][rem]=0;\n dp[i][rem]+=dp[i+1][rem];\n if(rem-coins[i]>=0) dp[i][rem]+=dp[i][rem-coins[i]];\n }\n }\n return dp[0][amount];\n }\n};",
"memory": "15785"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\n int d[5555][330];\npublic:\n int change(int amount, vector<int>& coins) {\n memset(d,-1,sizeof(d));\n return dp(amount,coins.size()-1,coins);\n }\n int dp(int am,int ind,vector<int>& coins){\n if(am==0){\n return 1;\n }\n if(ind<0){\n return 0;\n }\n if(d[am][ind]!=-1){\n return d[am][ind];\n }\n int way=0;\n for(int i=0;i<=ind;i++){\n if(am>=coins[i]){\n way+=dp(am-coins[i],i,coins);\n }\n }return d[am][ind]=way;\n\n }\n};",
"memory": "16075"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int memo[5001][301];\n memset(memo, -1, sizeof(memo));\n function<int(int, int)> dfs = [&](int amount, int index){\n if(amount == 0) return 1;\n if(amount < 0) return 0;\n if(index == coins.size()) return 0;\n if(memo[amount][index] != -1) return memo[amount][index];\n int ans = dfs(amount, index + 1);\n ans += dfs(amount - coins[index], index);\n return memo[amount][index] = ans;\n };\n return dfs(amount, 0);\n }\n};",
"memory": "16365"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int memo[5001][301];\n memset(memo, -1, sizeof(memo));\n function<int(int, int)> dfs = [&](int amount, int index){\n if(amount == 0) return 1;\n if(amount < 0) return 0;\n if(index == coins.size()) return 0;\n if(memo[amount][index] != -1) return memo[amount][index];\n int ans = dfs(amount, index + 1);\n ans += dfs(amount - coins[index], index);\n return memo[amount][index] = ans;\n };\n return dfs(amount, 0);\n }\n};",
"memory": "16655"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int req;\n vector<int>a;\n int dp[400][5002];\n int n;\n int solve(int i,int sum){\n if(i >= n){\n if(sum == req){\n return 1;\n }\n return 0;\n }\n if(dp[i][sum] != -1)return dp[i][sum];\n int ans = solve(i+1,sum);\n int val = a[i];\n int temp = sum;\n while(temp+a[i] <= req){\n ans += solve(i+1,temp+a[i]);\n temp += a[i];\n }\n return dp[i][sum] = ans;\n }\n int change(int amount, vector<int>& coins) {\n a = coins;\n req = amount;\n n = coins.size();\n for(int i = 0;i<=n;i++){\n for(int j=0;j<=req;j++){\n dp[i][j] = -1;\n }\n }\n return solve(0,0);\n }\n};",
"memory": "16945"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n if(amount == 0) {\n return 1;\n }\n int sz = coins.size();\n vector<int> prev(amount + 1);\n \n \n for(auto i : coins) {\n if(i > amount) {\n continue;\n }\n vector<int> cur(amount +1);\n \n \n cur[i] = 1;\n for(int j = 0; j <= amount; ++j) {\n cur[j] += prev[j];\n \n int n = j - i;\n if(n > 0) {\n cur[j] += cur[n];\n }\n }\n prev = cur;\n }\n // for(auto k : prev) {\n // cout << k << \" \";\n // }\n // cout << endl;\n \n return prev[amount];\n }\n};",
"memory": "17235"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n if(amount == 0) {\n return 1;\n }\n int sz = coins.size();\n vector<int> prev(amount + 1);\n \n \n for(auto i : coins) {\n if(i > amount) {\n continue;\n }\n vector<int> cur(amount +1);\n \n \n cur[i] = 1;\n for(int j = 0; j <= amount; ++j) {\n cur[j] += prev[j];\n \n int n = j - i;\n if(n > 0) {\n cur[j] += cur[n];\n }\n }\n prev = cur;\n }\n // for(auto k : prev) {\n // cout << k << \" \";\n // }\n // cout << endl;\n \n return prev[amount];\n }\n};",
"memory": "17235"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n if(amount == 0) {\n return 1;\n }\n int sz = coins.size();\n vector<int> prev(amount + 1);\n \n \n for(auto i : coins) {\n if(i > amount) {\n continue;\n }\n vector<int> cur = prev;\n ++cur[i];\n for(int j = 0; j <= amount; ++j) {\n int n = j - i;\n if(n > 0) {\n cur[j] += cur[n];\n }\n }\n prev = cur;\n }\n // for(auto k : prev) {\n // cout << k << \" \";\n // }\n // cout << endl;\n \n return prev[amount];\n }\n};",
"memory": "17525"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n if(amount == 0) {\n return 1;\n }\n int sz = coins.size();\n vector<int> prev(amount + 1);\n \n \n for(auto i : coins) {\n if(i > amount) {\n continue;\n }\n vector<int> cur = prev;\n ++cur[i];\n for(int j = 0; j <= amount; ++j) {\n int n = j - i;\n if(n > 0) {\n cur[j] += cur[n];\n }\n }\n prev = cur;\n }\n // for(auto k : prev) {\n // cout << k << \" \";\n // }\n // cout << endl;\n \n return prev[amount];\n }\n};",
"memory": "17525"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n long long dp[n+1][amount+1];\n memset(dp,0,sizeof(dp));\n dp[0][0]=1;\n for(int j=0;j<n;j++){\n for(int i=0;i<=amount;i++){\n dp[j+1][i]+=dp[j][i];\n if(i>=coins[j])\n dp[j+1][i]+=dp[j+1][i-coins[j]];\n }\n }\n return dp[n][amount];\n }\n};",
"memory": "17815"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp; \n int solve(int ind, int amount, vector<int> &c) {\n if (amount == 0)\n return 1;\n if (ind == c.size() || amount < c[ind])\n return 0; \n if (dp[ind][amount] != -1)\n return dp[ind][amount]; \n \n int without = solve(ind+1, amount, c), with = solve (ind, amount - c[ind], c), ans = 0; \n \n return dp[ind][amount] = with + without; \n }\n \n int change(int amount, vector<int>& coins) {\n vector<int> fc;\n for (auto &c: coins)\n if (c <= amount)\n fc.push_back(c); \n sort(fc.begin(), fc.end()); \n dp.resize(fc.size(), vector<int>(amount+1, -1)); \n return solve(0, amount, fc); \n }\n};",
"memory": "18105"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp; \n int solve(int ind, int amount, vector<int> &c) {\n // Whenever total ways are asked, return 1 if condition is satisfied, else 0 \n if (amount == 0)\n return 1;\n if (ind == c.size() || amount < c[ind])\n return 0; \n if (dp[ind][amount] != -1)\n return dp[ind][amount]; \n \n int without = solve(ind+1, amount, c), with = solve (ind, amount - c[ind], c), ans = 0; \n \n return dp[ind][amount] = with + without; \n }\n \n int change(int amount, vector<int>& coins) {\n vector<int> fc;\n for (auto &c: coins)\n if (c <= amount)\n fc.push_back(c); \n sort(fc.begin(), fc.end()); \n dp.resize(fc.size(), vector<int>(amount+1, -1)); \n return solve(0, amount, fc); \n }\n};",
"memory": "18105"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp; \n int maxi = INT_MAX; \n int solve(int ind, int amount, vector<int> &c) {\n if (amount == 0)\n return 1;\n if (ind == c.size() || amount < c[ind])\n return maxi; \n if (dp[ind][amount] != -1)\n return dp[ind][amount]; \n \n int without = solve(ind+1, amount, c), with = solve (ind, amount - c[ind], c), ans = 0;\n \n if (without < maxi)\n ans += without; \n if (with < maxi)\n ans += with; \n \n return dp[ind][amount] = ans; \n }\n \n int change(int amount, vector<int>& coins) {\n vector<int> fc;\n for (auto &c: coins)\n if (c <= amount)\n fc.push_back(c); \n sort(fc.begin(), fc.end()); \n dp.resize(fc.size(), vector<int>(amount+1, -1)); \n return solve(0, amount, fc); \n }\n};",
"memory": "18395"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n long long dp[n+1][amount+1];\n\n dp[0][0] = 1;\n for(int i = 1; i <= n; i++){\n dp[i][0] = 1;\n }\n\n for(int j = 1; j <=amount; j++){\n dp[0][j] = 0;\n }\n\n for(int i = 1; i <=n; i++){\n for(int j = 1; j <= amount; j++){\n if(j >= coins[i-1]){\n dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]];\n }else{\n dp[i][j] = dp[i-1][j];\n }\n }\n }\n\n return dp[n][amount];\n }\n};",
"memory": "18395"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int a[400][6000]={0},i,j;\n for(i=0;i<=amount;i++){\n if(i%coins[0]==0)\n a[0][i]=1;\n }\n for(i=1;i<coins.size();i++){\n for(j=0;j<=amount;j++){\n if(j<coins[i]){\n a[i][j]=a[i-1][j];}\n else{\n a[i][j]=a[i-1][j]+a[i][j-coins[i]];\n }\n }\n }\n return a[coins.size() - 1][amount];\n }\n};",
"memory": "18685"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int a[400][6000]={0},i,j;\n for(i=0;i<coins.size();i++)\n a[i][0]=1;\n for(i=1;i<=amount;i++){\n if(i%coins[0]==0)\n a[0][i]=1;\n }\n for(i=1;i<coins.size();i++){\n for(j=1;j<=amount;j++){\n j<coins[i] ? a[i][j]=a[i-1][j] : a[i][j]=a[i-1][j]+a[i][j-coins[i]]; \n }\n }\n return a[coins.size() - 1][amount];\n }\n};",
"memory": "18685"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n //Method - 01 (Memoization)\n int helper(int amount, int idx, vector<int>& coins, vector<vector<int>>& dp){\n if(amount == 0)\n return 1;\n if(idx < 0)\n return 0;\n if(amount < 0)\n return 0;\n if(dp[idx][amount] != -1)\n return dp[idx][amount];\n int notTake = helper(amount, idx-1, coins, dp);\n int take = helper(amount-coins[idx], idx, coins, dp);\n return dp[idx][amount] = notTake + take;\n }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n\n //Method - 02 (Tabulation)\n\n // vector<vector<int>> dp(n, vector<int>(amount+1, 0)); \n //dp[i][j] => no of ways of getting amount j up to index i\n // for(int i=0; i<n; i++)\n // dp[i][0] = 1;\n // for(int i=0; i<n; i++){\n // for(int j=1; j<=amount; j++){\n // if(j-coins[i] >= 0)\n // dp[i][j] = dp[i][j-coins[i]];\n // if(i-1 >= 0)\n // dp[i][j] += dp[i-1][j];\n // }\n // }\n // return dp[n-1][amount];\n\n // Method-03(Space Optimization)\n vector<int> dp(amount+1, 0); \n dp[0] = 1;\n for(int i=1; i<=amount; i++){\n if(i-coins[0] >= 0)\n dp[i] = dp[i-coins[0]];\n }\n for(int i=1; i<n; i++){\n vector<int> temp(amount+1, 0);\n temp[0] = 1;\n for(int j=1; j<=amount; j++){\n if(j-coins[i] >= 0)\n temp[j] = temp[j-coins[i]];\n temp[j] += dp[j];\n }\n dp = temp;\n }\n return dp[amount];\n }\n};",
"memory": "18975"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n// int f(int ind, int amount, vector<int>& coins, vector<vector<int>>& dp){\n// if(ind == 0){\n// if(amount % coins[0] == 0) return 1;\n// return 0;\n// }\n// if(dp[ind][amount] != -1) return dp[ind][amount];\n// int nottake = f(ind-1, amount, coins, dp);\n// int take = 0;\n// if(coins[ind] <= amount){\n// take = f(ind, amount-coins[ind], coins, dp);\n// }\n// return dp[ind][amount] = take + nottake;\n// }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<int> prev(amount+1, 0);\n for(int i = 0 ; i <= amount; i++){\n if(i%coins[0] == 0) prev[i] = 1;\n }\n for(int i = 1; i < n; i++){\n vector<int> curr(amount+1, 0);\n for(int j = 0; j <= amount; j++){\n int nottake = prev[j];\n int take = 0;\n if(coins[i] <= j){\n take = curr[j-coins[i]];\n }\n curr[j] = take + nottake;\n }\n prev = curr;\n }\n // int ans = dp[n-1][amount];\n return prev[amount];\n }\n\n};",
"memory": "19265"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<int> prev(amount+1,0);\n\n for(int i=0 ; i<= amount ; i++){\n if(i%coins[0] == 0){\n prev[i] = 1;\n }\n }\n\n for(int index = 1 ; index <n ; index++){\n vector<int> curr(amount+1,0);\n curr[0] = 1;\n for(int sum = 0 ; sum<= amount ; sum++){\n\n int not_pick = prev[sum];\n int pick = 0;\n if(coins[index] <= sum){\n pick = curr[sum-coins[index]];\n }\n curr[sum]= pick + not_pick;\n\n }\n prev = curr;\n }\n return prev[amount];\n }\n};",
"memory": "19265"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n// int f(int ind, int amount, vector<int>& coins, vector<vector<int>>& dp){\n// if(ind == 0){\n// if(amount % coins[0] == 0) return 1;\n// return 0;\n// }\n// if(dp[ind][amount] != -1) return dp[ind][amount];\n// int nottake = f(ind-1, amount, coins, dp);\n// int take = 0;\n// if(coins[ind] <= amount){\n// take = f(ind, amount-coins[ind], coins, dp);\n// }\n// return dp[ind][amount] = take + nottake;\n// }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<int> prev(amount+1, 0);\n for(int i = 0 ; i <= amount; i++){\n if(i%coins[0] == 0) prev[i] = 1;\n }\n for(int i = 1; i < n; i++){\n vector<int> curr(amount+1, 0);\n for(int j = 0; j <= amount; j++){\n int nottake = prev[j];\n int take = 0;\n if(coins[i] <= j){\n take = curr[j-coins[i]];\n }\n curr[j] = take + nottake;\n }\n prev = curr;\n }\n // int ans = dp[n-1][amount];\n return prev[amount];\n }\n\n};",
"memory": "19555"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int solve(vector<vector<int>>&dp, vector<int>&coins, int amount, int index)\n {\n if(amount==0)\n return 1;\n\n if(index<0)\n return 0;\n\n if(dp[index][amount] != -1)\n return dp[index][amount];\n\n int take = 0;\n\n if(amount >= coins[index])\n take = solve(dp,coins, amount-coins[index], index);\n\n int ntake = solve(dp,coins, amount, index-1);\n\n return dp[index][amount] = take+ntake;\n }\n\n int change(int amount, vector<int>& coins) {\n\n vector<vector<int>>dp(coins.size(), vector<int>(amount+1, -1));\n \n return solve(dp,coins, amount, coins.size()-1);\n }\n};",
"memory": "20425"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int ways(int i, vector<int>& coins, int amt, vector<vector<int>>& dp) {\n\n if (i < 0)\n return 0;\n if (amt == 0)\n return 1;\n\n if (dp[i][amt] != -1)\n return dp[i][amt];\n\n int pick = 0;\n if (amt >= coins[i])\n pick = ways(i, coins, amt - coins[i], dp);\n\n int notpick = ways(i - 1, coins, amt, dp);\n\n return dp[i][amt] = pick + notpick;\n }\n\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<vector<int>> dp(n, vector<int>(amount + 1, -1));\n\n return ways(n - 1, coins, amount, dp);\n }\n};",
"memory": "20425"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int idx,int amt, vector<int>&coins,vector<vector<int>>&dp)\n {\n if(idx==coins.size()-1)\n {\n return (amt%coins[idx]==0);\n }\n\n if(dp[idx][amt]!=-1){\n return dp[idx][amt];\n }\n\n int not_pick=solve(idx+1,amt,coins,dp);\n int pick=0;\n if(coins[idx]<=amt)\n {\n pick=solve(idx,amt-coins[idx],coins,dp);\n }\n\n return dp[idx][amt]=pick+not_pick;\n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n vector<vector<int>> dp(n+1,vector<int>(amount+1,-1));\n\n // int sum=0;\n // for(int i=0;i<n;i++)\n // {\n // sum=sum+coins[i];\n //}\n // if(sum<amount)\n // {\n // return 0;\n // }\n\n return solve(0,amount,coins,dp);\n }\n};",
"memory": "20715"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int idx,int amt, vector<int>&coins,vector<vector<int>>&dp)\n {\n if(idx==coins.size()-1)\n {\n return (amt%coins[idx]==0);\n }\n\n if(dp[idx][amt]!=-1){\n return dp[idx][amt];\n }\n\n int not_pick=solve(idx+1,amt,coins,dp);\n int pick=0;\n if(coins[idx]<=amt)\n {\n pick=solve(idx,amt-coins[idx],coins,dp);\n }\n\n return dp[idx][amt]=pick+not_pick;\n }\n int change(int amount, vector<int>& coins) {\n int n=coins.size();\n vector<vector<int>> dp(n+1,vector<int>(amount+1,-1));\n\n // int sum=0;\n // for(int i=0;i<n;i++)\n // {\n // sum=sum+coins[i];\n //}\n // if(sum<amount)\n // {\n // return 0;\n // }\n\n return solve(0,amount,coins,dp);\n }\n};",
"memory": "20715"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "int dp[10001][301]; \n \nclass Solution {\n\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n }\n int solve(int i, int a, vector<int>& coins, int n, int k) {\n if (i == a)\n return 1;\n if (i > a)\n return 0;\n if (dp[i][k] != -1)\n return dp[i][k];\n\n int ans = 0;\n for (int j = k; j < n; j++) {\n ans += solve(i + coins[j], a, coins, n, j);\n }\n\n return dp[i][k] = ans;\n }\n\n int change(int amount, vector<int>& coins) {\n memset(dp, -1, sizeof(dp));\n return solve(0, amount, coins, coins.size(), 0);\n }\n};\n",
"memory": "21005"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(int amount, vector<int>& coins,int i,vector<vector<int>> &dp)\n { \n \n if(i>=coins.size())\n { if(amount==0)\n {\n return 1;\n }\n return 0;\n\n }\n if(dp[i][amount]!=-1)\n {\n return dp[i][amount];\n }\n \n\n int include=0,exclude=0;\n\n if(amount>=coins[i])\n {\n include=solve(amount-coins[i],coins,i,dp);\n\n } \n exclude=solve(amount,coins,i+1,dp);\n \n return dp[i][amount]=include + exclude;\n \n\n\n\n\n }\n\n\n\n int change(int amount, vector<int>& coins) {\n vector<vector<int>> dp(coins.size()+3,vector<int>(amount+3,-1));\n return solve(amount,coins,0,dp); \n }\n};",
"memory": "21005"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int getans( int n , int sum , vector<int> &coins , vector<vector<int>> &dp )\n {\n if( sum == 0 && n == 0 )\n {\n return 1 ;\n }\n if( n == 0 )\n {\n return 0 ; \n }\n\n if( dp[n][sum] != -1 )\n {\n return dp[n][sum] ;\n }\n\n int a = 0 , b = 0 ; \n if( coins[n-1] <= sum )\n {\n a = getans( n , sum - coins[n-1] , coins , dp ) ;\n }\n b = getans( n - 1 , sum , coins , dp ) ;\n return dp[n][sum] = a+b ;\n }\n\n int change(int amt, vector<int>& coins) \n { \n vector<vector<int>> dp( coins.size() + 4 , vector<int>(amt + 4 , -1 ) ) ;\n return getans( coins.size() , amt , coins , dp ) ;\n }\n};",
"memory": "21295"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int f[3001][5001];\n f[0][0] = 1;\n for (int j = 1; j <= coins.size(); ++j) {\n f[j][0] = 1;\n for (int i = 1; i <= amount; ++i) {\n if (i < coins[j-1])\n f[j][i] = f[j-1][i];\n else\n f[j][i] = f[j-1][i] + f[j][i-coins[j-1]];\n }\n }\n return f[coins.size()][amount];\n }\n};",
"memory": "21295"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int t;\n int length;\n\n int rec(int level, long long taken, vector<int>& coins,vector<vector<int>>& dp) {\n\n // pruning\n if (taken > t || level == length)\n return 0;\n\n // base case\n if (taken == t)\n return 1;\n\n // cache\n if (dp[level][taken] != -1)\n return dp[level][taken];\n\n int ans = 0;\n // compute\n ans += rec(level, taken + coins[level], coins, dp);\n ans += rec(level + 1, taken, coins, dp);\n\n // return\n return dp[level][taken] = ans;\n }\n\n int change(int amount, vector<int>& coins) {\n\n t = amount;\n length = coins.size();\n vector<vector<int>>dp(length+5,vector<int>(t+5,-1));\n return rec(0,0,coins,dp); \n\n }\n};",
"memory": "21585"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n long long dp[301][5001];\n long long rec(int level,int left,vector<int>& c){\n if(left < 0) return 0;\n \n if(level == c.size()){\n if(left == 0) return 1;\n else return 0;\n }\n\n if(dp[level][left] != -1) return dp[level][left];\n\n long long ans = 0;\n \n\n ans = ans + rec(level,left-c[level],c);\n ans = ans%mod;\n\n ans = ans + rec(level+1,left,c);\n ans = ans%mod;\n\n return dp[level][left] = ans;\n }\n int change(int target, vector<int>& c) {\n long long dp[2][target+2];\n\n long long mod = 1e17;\n memset(dp,0,sizeof(dp));\n int n = c.size();\n // dp[i][j] = dp[i][j-c[i]] + dp[i+1][j]\n for(int j = 0;j <= target;j++){\n if(j == 0){\n dp[(n)%2][j] = 1;\n }\n else{\n dp[(n)%2][j] = 0;\n }\n }\n for(int i = n-1;i >= 0;i--){\n for(int j = 0;j <= target;j++){\n dp[(i%2)][j] = 0;\n }\n\n for(int j = 0;j <= target;j++){\n long long x = dp[((i+1)%2)][j];\n x = x%mod;\n if(j - c[i] >= 0){\n x = x + dp[(i%2)][j-c[i]];\n x = x%mod;\n }\n dp[(i%2)][j] = x%mod;\n }\n }\n\n long long p = dp[0][target]%mod;\n\n return p;\n }\n};",
"memory": "21585"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n std::vector<std::vector<int>> memo(n, std::vector<int>(amount + 1, -1));\n\n std::function<int(int, int)> dfs = [&](int remain, int i) -> int {\n if (remain == 0) {\n return 1;\n }\n if (i < 0 || remain < 0) {\n return 0;\n }\n if (memo[i][remain] != -1) {\n return memo[i][remain];\n }\n\n int pick = dfs(remain - coins[i], i);\n int notPick = dfs(remain, i - 1);\n return memo[i][remain] = pick + notPick;\n };\n\n return dfs(amount, n - 1);\n }\n};",
"memory": "21875"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n std::vector<std::vector<int>> memo(n, std::vector<int>(amount + 1, -1));\n\n std::function<int(int, int)> dfs = [&](int remain, int i) -> int {\n if (remain == 0) {\n return 1;\n }\n if (i < 0 || remain < 0) {\n return 0;\n }\n if (memo[i][remain] != -1) {\n return memo[i][remain];\n }\n\n int pick = dfs(remain - coins[i], i);\n int notPick = dfs(remain, i - 1);\n return memo[i][remain] = pick + notPick;\n };\n\n return dfs(amount, n - 1);\n }\n};",
"memory": "21875"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[310][10010];\n \n int func(int ind, int amount, vector<int> & coins){\n if(amount == 0) return 1;\n if(ind < 0) return 0;\n if(dp[ind][amount] != -1) return dp[ind][amount];\n \n int ways = 0;\n for(int coin_amount = 0; coin_amount <= amount; coin_amount += coins[ind]){\n ways += func(ind-1, amount-coin_amount, coins);\n }\n \n return dp[ind][amount] = ways;\n }\n int change(int amount, vector<int>& coins) {\n memset(dp,-1, sizeof(dp));\n return func(coins.size() -1, amount , coins);\n }\n};",
"memory": "22165"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "\n\n\n\nstruct combinations {\npublic:\n\tinline static int get(std::vector<int> const &coins, int target) {\n\t\tint const n = coins.size();\n\t\tstd::vector<std::vector<int>> dp(n + 1, std::vector<int>(target + 1, -1));\n\t\tstd::function<int(int const&, int const&)> rx = [&](int const &index, int const &required) {\n\t\t\tif(index == n) { return 0; }\n\t\t\tif(required == target) { return 1; }\n\t\t\tif(required > target) { return 0; }\n\t\t\tif(dp[index][required] != -1) { return dp[index][required]; }\n\t\t\t\n\t\t\treturn dp[index][required] = rx(index + 1, required) + rx(index, required + coins[index]);\n\t\t};\n\n\t\treturn rx(0, 0);\n\t}\n};\n\nclass Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n return combinations::get(coins, amount);\n }\n};",
"memory": "22165"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int dp[310][10010];\n memset(dp, -1, sizeof(dp));\n int n = coins.size();\n return solve(coins, amount, n, dp);\n }\n\n int solve(vector<int>& coins, int amount, int n, int dp[][10010]) {\n int cnt = 0;\n if(n<0) return 0;\n if(amount == 0) return 1;\n if(dp[n][amount] != -1) return dp[n][amount];\n for(int j = n-1;j>=0;j--) {\n if(amount>=coins[j])\n cnt += solve(coins, amount-coins[j], j+1, dp);\n }\n return dp[n][amount] = cnt;\n }\n};",
"memory": "22455"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n int dp[310][10010];\n memset(dp, -1, sizeof(dp));\n int n = coins.size();\n return solve(coins, amount, n-1, dp);\n }\n\n int solve(vector<int>& coins, int amount, int n, int dp[][10010]) {\n int cnt = 0;\n if(amount == 0) return 1;\n if(dp[n][amount] != -1) return dp[n][amount];\n for(int j = n;j>=0;j--) {\n if(amount>=coins[j])\n cnt += solve(coins, amount-coins[j], j, dp);\n }\n return dp[n][amount] = cnt;\n }\n};",
"memory": "22455"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n // if i - coins[j] >= 0:\n // coins[j] can be used(exclude or include)\n \n // if i - coins[j] < 0\n // coins[j] cannot be used(exclude)\n vector<vector<int>>dp(amount + 1, vector<int>(coins.size(), 0));\n \n for(int i = 0; i < coins.size(); i++)\n {\n dp[0][i] = 1;\n }\n \n for(int i = 1; i <= amount; i++)\n {\n for(int j = 0; j < coins.size(); j++)\n {\n if (i - coins[j] >= 0)\n {\n if (j > 0)\n {\n dp[i][j] = dp[i - coins[j]][j] + dp[i][j - 1];\n }\n else\n {\n dp[i][j] = dp[i - coins[j]][j];\n }\n }\n else\n {\n if (j > 0)\n // cannot use coins[j] to make amount i\n dp[i][j] = dp[i][j - 1];\n }\n }\n }\n return dp[amount][coins.size() - 1];\n }\n};",
"memory": "22745"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int helper(vector<int>& coins, int amount, int idx) {\n int n = coins.size();\n vector<vector<int>> dp(amount + 1, vector<int>(n, 0));\n\n for (int tar = 0; tar <= amount; tar++) {\n for(int i = 0; i < n; i++) {\n if (tar == 0) {\n dp[tar][i] = 1;\n continue;\n }\n if (tar - coins[i] >= 0) {\n dp[tar][i] = dp[tar - coins[i]][i];\n }\n dp[tar][i] += (i - 1 >= 0) ? dp[tar][i - 1] : 0;\n }\n }\n\n return dp[amount][n - 1];\n }\n\n int change(int amount, vector<int>& coins) {\n return helper(coins, amount, 0);\n }\n};",
"memory": "22745"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int countways(int amount,vector<int>&coins,int n,vector<vector<int>>&dp){\n if(amount==0)return 1;\n if(n<0)return 0;\n if(dp[amount][n]!=-1)return dp[amount][n];\n int take=0;\n if(amount>=coins[n])take=countways(amount-coins[n],coins,n,dp);\n int not_take=countways(amount,coins,n-1,dp);\n return dp[amount][n]=take+not_take;\n }\n int change(int amount, vector<int>& coins) {\n vector<vector<int>>dp(amount+1,vector<int>(coins.size(),0));\n // return countways(amount,coins,coins.size()-1,dp);\n for(int i=0;i<coins.size();i++)dp[0][i]=1;\n for(int i=1;i<=amount;i++){\n for(int j=0;j<coins.size();j++){\n int take=0;\n if(i-coins[j]>=0 && amount>=coins[j])take=dp[i-coins[j]][j];\n int not_take=j-1>=0?dp[i][j-1]:0;\n dp[i][j]=take+not_take;\n }\n }\n return dp[amount][coins.size()-1];\n }\n};",
"memory": "23035"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int helper(vector<int>& coins, int amount, int idx) {\n int n = coins.size();\n vector<vector<int>> dp(amount + 1, vector<int>(n, 0));\n\n for (int tar = 0; tar <= amount; tar++) {\n for(int i = 0; i < n; i++) {\n if (tar == 0) {\n dp[tar][i] = 1;\n continue;\n }\n if (tar - coins[i] >= 0) {\n dp[tar][i] = dp[tar - coins[i]][i];\n }\n dp[tar][i] += (i - 1 >= 0) ? dp[tar][i - 1] : 0;\n }\n }\n\n return dp[amount][n - 1];\n }\n\n int change(int amount, vector<int>& coins) {\n return helper(coins, amount, 0);\n }\n};",
"memory": "23035"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int solve(int amount, vector<int>&coins, int index) {\n if (amount == 0) return 1;\n if (amount < 0) return 0;\n if (index >= coins.size()) return 0;\n int res = 0;\n if (dp[amount][index] != -1) return dp[amount][index];\n int n_amount = amount;\n while(n_amount >= 0) {\n res += solve(n_amount, coins, index+1);\n n_amount -= coins[index];\n }\n dp[amount][index] = res;\n return res;\n }\n\n int change(int amount, vector<int>& coins) {\n dp = vector<vector<int>>(amount+5, vector<int>(coins.size()+2, -1));\n sort(coins.begin(), coins.end());\n int res = solve(amount, coins, 0);\n return res;\n }\n};",
"memory": "23325"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int countways(int amount,vector<int>&coins,int n,vector<vector<int>>&dp){\n if(amount==0)return 1;\n if(n<0)return 0;\n if(dp[amount][n]!=-1)return dp[amount][n];\n int take=0;\n if(amount>=coins[n])take=countways(amount-coins[n],coins,n,dp);\n int not_take=countways(amount,coins,n-1,dp);\n return dp[amount][n]=take+not_take;\n }\n int change(int amount, vector<int>& coins) {\n vector<vector<int>>dp(amount+1,vector<int>(coins.size(),-1));\n return countways(amount,coins,coins.size()-1,dp);\n }\n};",
"memory": "23325"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int Count(std::vector<int>& coins, int total, int idx, std::vector<std::vector<int>>* const dp) {\n if (total == 0) {\n return 1;\n }\n if (idx == coins.size() || total < 0) {\n return 0;\n }\n\n if ((*dp)[total][idx] == -1) {\n int cnt = 0;\n for (int i = idx; i < coins.size(); ++i) {\n if (coins[i] > total) {\n break;\n }\n\n cnt += Count(coins, total - coins[i], i, dp);\n }\n\n (*dp)[total][idx] = cnt;\n }\n\n return (*dp)[total][idx];\n }\n\n int change(int amount, vector<int>& coins) {\n std::sort(coins.begin(), coins.end());\n\n std::vector<std::vector<int>> dp(amount + 1, std::vector<int>(coins.size(), -1));\n return Count(coins, amount, 0, &dp);\n }\n};",
"memory": "23615"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(vector<int>& coins, vector<vector<int>>& dp, int i, int amt){\n // if(i==coins.size()){\n // if(amt == 0) return 1;\n // else return 0;\n // }\n if(i>=coins.size()) return 0;\n if(amt == 0) return 1;\n if(amt<0) return 0;\n if(dp[amt][i]!=-1){\n return dp[amt][i];\n }\n return dp[amt][i] = solve(coins, dp, i, amt-coins[i]) + solve(coins, dp, i+1, amt);\n }\n int change(int amount, vector<int>& coins) {\n int n = coins.size();\n vector<vector<int>> dp(amount+1, vector<int>(n+1, -1));\n return solve(coins, dp, 0, amount);\n }\n};",
"memory": "23615"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int changeHelper(int amount, vector<int>& coins, map<pair<int, int>, int> &dp_memo, int index)\n {\n if(amount == 0)\n return 1;\n if(amount < 0)\n return 0;\n\n pair<int, int> d = make_pair(amount, index);\n if(dp_memo.find(d) != dp_memo.end())\n return dp_memo[d];\n\n int result = 0;\n for(unsigned int i=index;i<coins.size(); i++)\n {\n const int &currCoin = coins[i];\n result += changeHelper(amount-currCoin, coins, dp_memo, i);\n }\n\n //if(dp_memo.find(d) != dp_memo.end())\n //{\n //if(dp_memo[d] != result)\n //{\n // cout<<amount<<\" : \"<<dp_memo[d]<<\" != \"<<result<<\" i \"<<index<<endl;\n //}\n //}\n //else\n {\n //cout<<\"\\t +\"<<amount<<\" : \"<<result<<\" i \"<<index<<endl;\n dp_memo[d] = result;\n }\n return result;\n }\n \n \n\n int change(int amount, vector<int>& coins) \n {\n map<pair<int, int>, int> dp_memo;\n sort(coins.rbegin(), coins.rend());\n return changeHelper(amount, coins, dp_memo, 0);\n }\n};",
"memory": "23905"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int change(int amount, vector<int>& coins) {\n auto memo = vector(amount + 1, vector(coins.size() + 1, -1));\n function<int(int, int)> f = [&](int s, int k) {\n assert(s >= 0);\n assert(k >= 0);\n if (k == 0)\n return s == 0 ? 1 : 0;\n auto& r = memo[s][k];\n if (r != -1) return r;\n int res = 0;\n for (int i = 0; i * coins[k - 1] <= s; ++i) {\n res += f(s - i * coins[k - 1], k - 1);\n }\n return r = res;\n };\n return f(amount, coins.size());\n }\n};",
"memory": "23905"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int changeHelper(int amount, const vector<int>& coins, const int &coinCount, map<pair<int, int>, int> &dp_memo, int index)\n {\n if(amount == 0)\n return 1;\n if(amount < 0)\n return 0;\n\n pair<int, int> d = make_pair(amount, index);\n\n if(dp_memo.find(d) != dp_memo.end())\n return dp_memo[d];\n int result = 0;\n for(unsigned int i=index;i<coinCount; i++)\n {\n const int &currCoin = coins[i];\n result += changeHelper(amount-currCoin, coins, coinCount, dp_memo, i);\n }\n dp_memo[d] = result;\n return result;\n }\n \n \n\n int change(int amount, vector<int>& coins) \n {\n map<pair<int, int>, int> dp_memo;\n sort(coins.rbegin(), coins.rend());\n return changeHelper(amount, coins, coins.size(), dp_memo, 0);\n }\n};",
"memory": "24195"
} |
518 | <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 number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> amount = 5, coins = [1,2,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> amount = 3, coins = [2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> amount = 10, coins = [10]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= coins.length <= 300</code></li>
<li><code>1 <= coins[i] <= 5000</code></li>
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
<li><code>0 <= amount <= 5000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n\n int changeHelper(int amount, const vector<int>& coins, const int &coinCount, map<pair<int, int>, int> &dp_memo, int index)\n {\n if(amount == 0)\n return 1;\n if(amount < 0)\n return 0;\n\n pair<int, int> d = make_pair(amount, index);\n\n if(dp_memo.find(d) != dp_memo.end())\n return dp_memo[d];\n int result = 0;\n for(unsigned int i=index;i<coinCount; i++)\n {\n const int &currCoin = coins[i];\n result += changeHelper(amount-currCoin, coins, coinCount, dp_memo, i);\n }\n dp_memo[d] = result;\n return result;\n }\n \n \n\n int change(int amount, vector<int>& coins) \n {\n map<pair<int, int>, int> dp_memo;\n sort(coins.rbegin(), coins.rend());\n return changeHelper(amount, coins, coins.size(), dp_memo, 0);\n }\n};",
"memory": "24195"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.