Skip to content

Commit 11db1e9

Browse files
committed
feat: Implement change foreign currency
- Facebook prep
1 parent f714b7b commit 11db1e9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

recursion/change-foreign-currency.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Facebook prep: Change in a Foreign Currency
3+
* Given a list of the available denominations, determine if it's possible to
4+
* receive exact change for an amount of money targetMoney. Both the
5+
* denominations and target amount will be given in generic units of that
6+
* currency.
7+
*
8+
* Constraints:
9+
* 1. 1 <= |denominations| <= 100
10+
* 2. 1 <= denominations[i] <= 10000
11+
* 3. 1 <= targetMoney <= 1000000
12+
*/
13+
14+
/**
15+
*
16+
* @param {number} targetMoney - The target money to check if there's an exact change for
17+
* @param {number[]} denominations - All possible denomination changes sorted in ascending order
18+
*/
19+
function canGetExactChange(targetMoney, denominations) {
20+
for (let i = 0; i < denominations.length; i++) {
21+
const denomination = denominations[i];
22+
if (targetMoney < denomination) {
23+
return false;
24+
}
25+
26+
const remaining = targetMoney % denomination;
27+
if (remaining === 0 || canGetExactChange(remaining, denominations)) {
28+
return true;
29+
}
30+
}
31+
return false;
32+
}

0 commit comments

Comments
 (0)