File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * BFS 遍历
3
+ * @param {number[] } coins
4
+ * @param {number } amount
5
+ * @return {number }
6
+ */
7
+ var coinChange = function ( coins , amount ) {
8
+ const set = new Set ( [ amount ] ) ;
9
+ let queue = [ [ amount , 0 ] ] ;
10
+ while ( queue . length ) {
11
+ const [ a , b ] = queue . shift ( ) ;
12
+ if ( a === 0 ) {
13
+ return b ;
14
+ }
15
+ for ( const c of coins ) {
16
+ if ( a - c >= 0 && ! set . has ( a - c ) ) {
17
+ set . add ( a - c ) ;
18
+ queue . push ( [ a - c , b + 1 ] ) ;
19
+ }
20
+ }
21
+ }
22
+ return - 1 ;
23
+ } ;
24
+
25
+ /**
26
+ * 动态规划
27
+ * @param {number[] } coins
28
+ * @param {number } amount
29
+ * @return {number }
30
+ */
31
+ /*
32
+ var coinChange = function(coins, amount) {
33
+ const dp = new Array(amount + 1).fill(Number.MAX_SAFE_INTEGER);
34
+ dp[0] = 0;
35
+ for (let i = 1; i <= amount; i++) {
36
+ for (let j = 0; j < coins.length; j++) {
37
+ if (i - coins[j] >= 0) {
38
+ dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
39
+ }
40
+ }
41
+ }
42
+ return dp[amount] === Number.MAX_SAFE_INTEGER ? -1 : dp[[amount]];
43
+ };
44
+ */
You can’t perform that action at this time.
0 commit comments