-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (25 loc) · 956 Bytes
/
Copy pathindex.js
File metadata and controls
33 lines (25 loc) · 956 Bytes
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
function canCarry(capacity, trip) {
const minPos = Math.min(...trip.map((t) => t[1]))
const maxPos = Math.max(...trip.map((t) => t[2]))
const gifsPerStop = [ ...Array(maxPos - minPos).keys() ]
.map(index => {
const stop = index + minPos
return trip
.filter(([ _, ini, end ]) => stop >= ini && stop < end)
.reduce((acc, [ regalos ]) => acc + regalos, 0)
})
return gifsPerStop.every(num => num <= capacity)
}
function canCarryAlt(capacity, trip) {
let pendingSteps = []
for (let index = 0; index < trip.length; index++) {
const currentStep = trip[index]
if (currentStep[0] > capacity) return false
pendingSteps = pendingSteps.filter(step => step[2] > currentStep[1])
const pendingGifts = pendingSteps.reduce((acc, step) => acc + step[0], 0)
if (currentStep[0] + pendingGifts > capacity) return false
pendingSteps.push(currentStep)
}
return true
}
module.exports = canCarry