Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d349491

Browse files
Into the Dynamic Programming
1 parent 38c7de2 commit d349491

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

‎coin-change.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
process.stdin.resume();
6+
process.stdin.setEncoding('utf-8');
7+
8+
let inputString = '';
9+
let currentLine = 0;
10+
11+
process.stdin.on('data', function (inputStdin) {
12+
inputString += inputStdin;
13+
});
14+
15+
process.stdin.on('end', function () {
16+
inputString = inputString.split('\n');
17+
18+
main();
19+
});
20+
21+
function readLine() {
22+
return inputString[currentLine++];
23+
}
24+
25+
/*
26+
* Complete the 'getWays' function below.
27+
*
28+
* The function is expected to return a LONG_INTEGER.
29+
* The function accepts following parameters:
30+
* 1. INTEGER n
31+
* 2. LONG_INTEGER_ARRAY c
32+
*/
33+
34+
function getWays(n, c) {
35+
// Write your code here
36+
return count(c, c.length, n);
37+
}
38+
function readLine() {
39+
return inputString[currentLine++];
40+
}
41+
let memory = {}
42+
function count(S, m, n) {
43+
// If n is 0 then there is 1 solution
44+
// (do not include any coin)
45+
if (n === 0)
46+
return 1;
47+
48+
// If n is less than 0 then no
49+
// solution exists
50+
if (n < 0)
51+
return 0;
52+
53+
// If there are no coins and n
54+
// is greater than 0, then no
55+
// solution exist
56+
if (m <= 0 && n >= 1)
57+
return 0;
58+
59+
// count is sum of solutions (i)
60+
// including S[m-1] (ii) excluding S[m-1]
61+
// return count(S, m - 1, n) + count(S, m, n - S[m - 1]);
62+
63+
let leftsubtree = m - 1 + '-' + n;
64+
let rightsubtree = m + '-' + (n - S[m - 1]);
65+
66+
if (typeof memory[leftsubtree] === 'undefined')
67+
memory[leftsubtree] = count(S, m - 1, n);
68+
69+
if (typeof memory[rightsubtree] === 'undefined')
70+
memory[rightsubtree] = count(S, m, n - S[m - 1]);
71+
72+
return memory[leftsubtree] + memory[rightsubtree];
73+
}
74+
75+
function main() {
76+
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
77+
78+
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
79+
80+
const n = parseInt(firstMultipleInput[0], 10);
81+
82+
const m = parseInt(firstMultipleInput[1], 10);
83+
84+
const c = readLine().replace(/\s+$/g, '').split(' ').map(cTemp => parseInt(cTemp, 10));
85+
86+
// Print the number of ways of making change for 'n' units using coins having the values given by 'c'
87+
88+
const ways = getWays(n, c);
89+
90+
ws.write(ways + '\n');
91+
92+
ws.end();
93+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /