322:零钱兑换
This commit is contained in:
parent
14d254b497
commit
7deb06da05
103
src/main/java/leetcode/editor/cn/CoinChange.java
Normal file
103
src/main/java/leetcode/editor/cn/CoinChange.java
Normal file
@ -0,0 +1,103 @@
|
||||
//给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。
|
||||
//
|
||||
// 计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 -1 。
|
||||
//
|
||||
// 你可以认为每种硬币的数量是无限的。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1, 2, 5], amount = 11
|
||||
//输出:3
|
||||
//解释:11 = 5 + 5 + 1
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:coins = [2], amount = 3
|
||||
//输出:-1
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1], amount = 0
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1], amount = 1
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:coins = [1], amount = 2
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= coins.length <= 12
|
||||
// 1 <= coins[i] <= 231 - 1
|
||||
// 0 <= amount <= 104
|
||||
//
|
||||
// Related Topics 广度优先搜索 数组 动态规划
|
||||
// 👍 1399 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
//322:零钱兑换
|
||||
public class CoinChange {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new CoinChange().new Solution();
|
||||
System.out.println(solution.coinChange(new int[]{1, 2, 5}, 11));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int coinChange(int[] coins, int amount) {
|
||||
if (amount < 1) {
|
||||
return 0;
|
||||
}
|
||||
return coinChange(coins, amount, new HashMap());
|
||||
}
|
||||
|
||||
private int coinChange(int[] coins, int rem, Map<Integer, Integer> map) {
|
||||
if (rem < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (rem == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (map.containsKey(rem)) {
|
||||
return map.get(rem);
|
||||
}
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int coin : coins) {
|
||||
int count = coinChange(coins, rem - coin, map);
|
||||
if (count >= 0 && count < min) {
|
||||
min = 1 + count;
|
||||
}
|
||||
}
|
||||
min = (min == Integer.MAX_VALUE) ? -1 : min;
|
||||
map.put(rem, min);
|
||||
return min;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
52
src/main/java/leetcode/editor/cn/CoinChange.md
Normal file
52
src/main/java/leetcode/editor/cn/CoinChange.md
Normal file
@ -0,0 +1,52 @@
|
||||
<p>给你一个整数数组 <code>coins</code> ,表示不同面额的硬币;以及一个整数 <code>amount</code> ,表示总金额。</p>
|
||||
|
||||
<p>计算并返回可以凑成总金额所需的 <strong>最少的硬币个数</strong> 。如果没有任何一种硬币组合能组成总金额,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>你可以认为每种硬币的数量是无限的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = <code>[1, 2, 5]</code>, amount = <code>11</code>
|
||||
<strong>输出:</strong><code>3</code>
|
||||
<strong>解释:</strong>11 = 5 + 5 + 1</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = <code>[2]</code>, amount = <code>3</code>
|
||||
<strong>输出:</strong>-1</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = [1], amount = 0
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = [1], amount = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>coins = [1], amount = 2
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= coins.length <= 12</code></li>
|
||||
<li><code>1 <= coins[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>0 <= amount <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>动态规划</li></div></div>\n<div><li>👍 1399</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user