|
| 1 | +/** |
| 2 | + * @param {number[]} status |
| 3 | + * @param {number[]} candies |
| 4 | + * @param {number[][]} keys |
| 5 | + * @param {number[][]} containedBoxes |
| 6 | + * @param {number[]} initialBoxes |
| 7 | + * @return {number} |
| 8 | + */ |
| 9 | +var maxCandies = function(status, candies, keys, containedBoxes, initialBoxes) { |
| 10 | + let foundOpenable = true; |
| 11 | + let totalCandies = 0; |
| 12 | + while (initialBoxes.length > 0 && foundOpenable) { |
| 13 | + foundOpenable = false; |
| 14 | + let nextBoxes = []; |
| 15 | + for (let boxId of initialBoxes) { |
| 16 | + if (status[boxId]) { |
| 17 | + foundOpenable = true; |
| 18 | + nextBoxes.push(...containedBoxes[boxId]); |
| 19 | + for (let keyId of keys[boxId]) status[keyId] = 1; |
| 20 | + totalCandies += candies[boxId]; |
| 21 | + } else { |
| 22 | + nextBoxes.push(boxId); |
| 23 | + } |
| 24 | + } |
| 25 | + initialBoxes = nextBoxes; |
| 26 | + } |
| 27 | + return totalCandies; |
| 28 | +}; |
0 commit comments