Skip to content

Commit 7593186

Browse files
committed
Add solution #1298
1 parent c0f19e9 commit 7593186

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,213 LeetCode solutions in JavaScript
1+
# 1,214 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -981,6 +981,7 @@
981981
1295|[Find Numbers with Even Number of Digits](./solutions/1295-find-numbers-with-even-number-of-digits.js)|Easy|
982982
1296|[Divide Array in Sets of K Consecutive Numbers](./solutions/1296-divide-array-in-sets-of-k-consecutive-numbers.js)|Medium|
983983
1297|[Maximum Number of Occurrences of a Substring](./solutions/1297-maximum-number-of-occurrences-of-a-substring.js)|Medium|
984+
1298|[Maximum Candies You Can Get from Boxes](./solutions/1298-maximum-candies-you-can-get-from-boxes.js)|Hard|
984985
1304|[Find N Unique Integers Sum up to Zero](./solutions/1304-find-n-unique-integers-sum-up-to-zero.js)|Easy|
985986
1309|[Decrypt String from Alphabet to Integer Mapping](./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
986987
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 1298. Maximum Candies You Can Get from Boxes
3+
* https://leetcode.com/problems/maximum-candies-you-can-get-from-boxes/
4+
* Difficulty: Hard
5+
*
6+
* You have n boxes labeled from 0 to n - 1. You are given four arrays: status, candies, keys, and
7+
* containedBoxes where:
8+
* - status[i] is 1 if the ith box is open and 0 if the ith box is closed,
9+
* - candies[i] is the number of candies in the ith box,
10+
* - keys[i] is a list of the labels of the boxes you can open after opening the ith box.
11+
* - containedBoxes[i] is a list of the boxes you found inside the ith box.
12+
*
13+
* You are given an integer array initialBoxes that contains the labels of the boxes you initially
14+
* have. You can take all the candies in any open box and you can use the keys in it to open new
15+
* boxes and you also can use the boxes you find in it.
16+
*
17+
* Return the maximum number of candies you can get following the rules above.
18+
*/
19+
20+
/**
21+
* @param {number[]} status
22+
* @param {number[]} candies
23+
* @param {number[][]} keys
24+
* @param {number[][]} containedBoxes
25+
* @param {number[]} initialBoxes
26+
* @return {number}
27+
*/
28+
var maxCandies = function(status, candies, keys, containedBoxes, initialBoxes) {
29+
let totalCandies = 0;
30+
const queue = [...initialBoxes];
31+
const availableKeys = new Set();
32+
const unopenedBoxes = new Set();
33+
34+
while (queue.length) {
35+
const currentBox = queue.shift();
36+
37+
if (status[currentBox]) {
38+
totalCandies += candies[currentBox];
39+
for (const key of keys[currentBox]) availableKeys.add(key);
40+
for (const box of containedBoxes[currentBox]) queue.push(box);
41+
} else {
42+
unopenedBoxes.add(currentBox);
43+
}
44+
45+
for (const box of unopenedBoxes) {
46+
if (availableKeys.has(box)) {
47+
status[box] = 1;
48+
unopenedBoxes.delete(box);
49+
queue.push(box);
50+
}
51+
}
52+
}
53+
54+
return totalCandies;
55+
};

0 commit comments

Comments
 (0)