File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments