Skip to content

Commit c12fda4

Browse files
Merge pull request ignacio-chiazzo#5 from ignacio-chiazzo/coinchange
Added coinchange
2 parents d69a857 + a64bb35 commit c12fda4

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

CoinChange.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
3+
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/
4+
5+
862. Shortest Subarray with Sum at Least K
6+
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
7+
8+
If there is no non-empty subarray with sum at least K, return -1.
9+
10+
11+
12+
Example 1:
13+
14+
Input: A = [1], K = 1
15+
Output: 1
16+
Example 2:
17+
18+
Input: A = [1,2], K = 4
19+
Output: -1
20+
Example 3:
21+
22+
Input: A = [2,-1,2], K = 3
23+
Output: 3
24+
25+
26+
Note:
27+
28+
1 <= A.length <= 50000
29+
-10 ^ 5 <= A[i] <= 10 ^ 5
30+
1 <= K <= 10 ^ 9
31+
*/
32+
33+
34+
/*
35+
Tree for solution
36+
37+
38+
[pos, countOfNumbers, K]
39+
[pos + 1, countOfNumbers, K] [pos + 1, countOfNumbers + 1, K - nums[pos]] [pos, countOfNumbers + 1, K - nums[pos]]
40+
...
41+
42+
[0, 0, 11] // [pos, countOfNumbers, K]
43+
/ | \
44+
[1, 0, 11] [1, 1, 10] [0, 1, 10]
45+
/ | \ / | \ / | \
46+
[2, 0, 11] [2, 1, 9] [1, 1, 9] [2, 0, 10] [2, 2, 8] [1, 2, 8] [1, 1, 10] [1, 2, 9] [0, 2, 9]
47+
...
48+
*/
49+
50+
// Solution 3
51+
var coinChange3 = function(coins, amount) {
52+
var memo = [];
53+
54+
for(var i = 0; i <= amount; i++) {
55+
memo[i] = Number.POSITIVE_INFINITY;
56+
}
57+
memo[0] = 0;
58+
59+
for(var i = 0; i < coins.length; i++) {
60+
const coin = coins[i];
61+
for(var j = coin; j < memo.length; j++) {
62+
memo[j] = min2(memo[j], memo[j - coin] + 1);
63+
}
64+
}
65+
66+
return (memo[amount] == Number.POSITIVE_INFINITY) ? -1 : memo[amount];
67+
};
68+
69+
var min2 = function(a, b) {
70+
return (a < b) ? a : b;
71+
}
72+
73+
74+
// Solution 2
75+
var buildMemoKey = function(position, amount) {
76+
return position + "-" + amount;
77+
}
78+
79+
var coinChange2 = function(coins, amount) {
80+
var memo = {};
81+
var solution = coinChangeAux2(coins, amount, 0, memo, Number.POSITIVE_INFINITY);
82+
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
83+
};
84+
85+
var coinChangeAux2 = function(coins, amount, pos, memo) {
86+
var key = buildMemoKey(pos, amount);
87+
if(memo[key]) {
88+
return memo[key];
89+
}
90+
if(amount < 0) {
91+
return Number.POSITIVE_INFINITY;
92+
} else if(amount == 0) {
93+
return 0;
94+
} else if(pos >= coins.length) {
95+
return Number.POSITIVE_INFINITY;
96+
}
97+
98+
var left = coinChangeAux2(coins, amount, pos + 1, memo);
99+
var middle = 1 + coinChangeAux2(coins, amount - coins[pos], pos + 1, memo);
100+
var right = 1 + coinChangeAux2(coins, amount - coins[pos], pos, memo);
101+
102+
var solution = min(left, middle, right);
103+
memo[key] = solution;
104+
return solution;
105+
}
106+
107+
// Solution 1 naive
108+
var coinChange1 = function(coins, amount) {
109+
var solution = coinChangeAux1(coins, amount, 0);
110+
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
111+
};
112+
113+
var coinChangeAux1 = function(coins, amount, pos) {
114+
if(amount < 0) {
115+
return Number.POSITIVE_INFINITY;
116+
} else if(amount == 0) {
117+
return 0;
118+
} else if(pos >= coins.length) {
119+
return Number.POSITIVE_INFINITY;
120+
}
121+
122+
var left = coinChangeAux1(coins, amount, pos + 1);
123+
var middle = 1 + coinChangeAux1(coins, amount - coins[pos], pos + 1);
124+
var right = 1 + coinChangeAux1(coins, amount - coins[pos], pos);
125+
126+
var partialSol = min(left, middle, right);
127+
return partialSol;
128+
}
129+
130+
var min = function(a, b, c) {
131+
if(a < b) { return (a < c) ? a : c };
132+
return (b < c) ? b : c;
133+
}
134+
135+
136+
function main() {
137+
console.log("-------------");
138+
console.log("Approach 1")
139+
console.log(coinChange1([], 3));
140+
console.log(coinChange1([2], 3));
141+
console.log(coinChange1([1, 2, 5], 11));
142+
console.log(coinChange1([3,7,405,436], 8839));
143+
// console.log(coinChange1([370,417,408,156,143,434,168,83,177,280,117], 9953)); takes forever
144+
145+
console.log("-------------");
146+
console.log("Approach 2")
147+
console.log(coinChange2([], 3));
148+
console.log(coinChange2([2], 3));
149+
console.log(coinChange2([1, 2, 5], 11));
150+
console.log(coinChange2([3,7,405,436], 8839));
151+
console.log(coinChange2([370,417,408,156,143,434,168,83,177,280,117], 9953));
152+
153+
console.log("-------------");
154+
console.log("Approach 3")
155+
console.log(coinChange3([], 3));
156+
console.log(coinChange3([2], 3));
157+
console.log(coinChange3([1, 2, 5], 11));
158+
console.log(coinChange3([3,7,405,436], 8839));
159+
console.log(coinChange3([370,417,408,156,143,434,168,83,177,280,117], 9953));
160+
}
161+
main();

Main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js");
33
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
44
var subsets = require("./Subsets.js");
5+
var coinChange = require("./CoinChange.js");
56
var nQueens = require("./NQueens.js");
67
var uniquePaths = require("./UniquePaths.js");
78
var floodFill = require("./FloodFill.js")
@@ -11,6 +12,7 @@ var floodFill = require("./FloodFill.js")
1112
// permutationWithoutDuplicates.main();
1213
// permutationWithDuplicates.main();
1314
// subsets.main();
15+
coinChange.main();
1416
// nQueens.main();
1517
// uniquePaths.main();
1618
// floodFill.main();

0 commit comments

Comments
 (0)