-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (45 loc) · 1.38 KB
/
index.js
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
const util = require("util")
function maximumProfit(inventory, order) {
const suppliers = {}
const maxPrice = Math.max(...inventory)
inventory.forEach((element, index) => {
const index2 = index
if (!suppliers[`sup-${index2}`]) {
suppliers[`sup-${index2}`] = {}
}
suppliers[`sup-${index2}`]["inventory"] = element
suppliers[`sup-${index2}`]["boxes"] = []
})
const totales = []
Object.keys(suppliers).forEach((obj, suppIndex) => {
const supp = suppliers[obj]
const inventory = supp["inventory"]
const boxes = supp["boxes"]
for (let index = 1; index <= maxPrice; index++) {
if (index <= inventory) {
suppliers[`sup-${suppIndex}`]["boxes"].push(index)
} else {
suppliers[`sup-${suppIndex}`]["boxes"].push(0)
}
}
})
Object.keys(suppliers).forEach((obj, suppIndex) => {
const supp = suppliers[obj]
suppliers[`sup-${suppIndex}`]["boxes"] = supp["boxes"].reverse()
})
let orderCompleted = 0
let profit = 0
for (let index = 0; index < maxPrice; index++) {
Object.keys(suppliers).forEach((obj, suppIndex) => {
const supp = suppliers[obj]
const price = supp["boxes"][index]
if (price > 0 && orderCompleted < order) {
profit += price
orderCompleted++
}
})
}
console.log(util.inspect(suppliers))
return profit
}
exports.maximumProfit = maximumProfit