-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (46 loc) · 1.63 KB
/
index.js
File metadata and controls
62 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Complete the function below.
* DONOT MODIFY anything outside this function!
*/
function calculateRemainders(proportions, availableGrams) {
// if one portion not available return availableGrams
if (proportions.length>availableGrams.length || (Math.min(...availableGrams) < 0)) {
return availableGrams;
}
//smallest edge should be less or equal max value of availableGrams
let smallest = Math.max(...availableGrams);
// compute smallest edge
availableGrams.map((available, i) => {
if (proportions[i]) {
let itemCount = Math.floor(available / proportions[i]);
smallest = itemCount < smallest ? itemCount : smallest;
}
});
// compute remaining grams
let results = availableGrams.map((gram, i) => {
return proportions[i] ?
gram - (proportions[i] * smallest)
: gram;
});
return results;
}
module.exports = calculateRemainders;
/////////////// Do not modify anything below this line ////////////////////
// process.stdin.resume();
// process.stdin.setEncoding('ascii');
// let raw_input = '';
// process.stdin.on('data', function (data) {
// raw_input += data;
// });
// process.stdin.on('end', () => {
// return process.stdout.write(
// calculateRemainders.apply(null, preparedData()).toString()
// );
// });
// function preparedData() {
// const
// input = raw_input.split('\n'),
// proportions = input[0].split(' ').map((i) => parseInt(i, 10)),
// availableGrams = input[1].split(' ').map((i) => parseInt(i, 10));
// return [proportions, availableGrams];
// }