Problem List

Maximum Candies You Can Get From Boxes

June 03, 2025Go array, bredth first search, grapheasy

Problem

Approach

ex: status = [1,0,1,0]
candies = [7,5,4,100] 
keys = [[],[],[1],[]] 
containedBoxes = [[1,2],[3],[],[]]
initialBoxes = [0]

- start of at status[0] because thats the index of inital boxes given
- status 0 is now visited
- inside of status[0] we get candies[0] (7), no keys, and boxes 1 and 2.
- try to open status[1], but cannot because we didnt get a key for it and its locked, but its still not visited.
- freely open status[2], because theres no key needed, status[2] is now visited
- inside of status 2, we get 4 more candies, and the key to box 1.
- open box two now because we have a key.
- in box 1, we get 5 more candies, and the key to 3.
- in box three, we get 100 more candies, but no more keys, so we can exit.

observations
1. need to keep track of which boxes have been visited to avoid possible loops
2. if initialBoxes is empty, we cannot visit any of the boxes
3. cannot use a loop because its not garunteed to find a key/box be open
    a. need to get a reference from a chest to open a box
4. we are done when all of the boxes have been visited/no more references to new boxes
5. the contained boxes tell us what we get to open next, thats what gets added to the dq
6. need to keep track of available keys
7. you can open a box if you either have the key for it or its open.

solutions to observations
1. make an array of booleans the same length as the status and mark the index we start visiting as true 
2. check if len(initialBoxes) == 0 return 0
3. bfs with queue to track which boxes are up for search
4. for loop checks for len(boxes) > 0

Reflections

This is my second hard problem. So having completed this with most of the proper core functionality in just over 45 minutes, I'm pretty happy with it. I understand after the fact that looping through the deque to find the next openable box isn't omptimal and I'd love to revisit this problem at some point.

Go Solution
LeetCode Problem Link