Maximum Candies You Can Get from Boxes - Complete Solution Guide
Maximum Candies You Can Get from Boxes is LeetCode problem 1298, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You have n boxes labeled from 0 to n - 1 . You are given four arrays: status , candies , keys , and containedBoxes where: status[i] is 1 if the i th box is open and 0 if the i th box is closed, candies[i] is the number of candies in the i th box, keys[i] is a list of the labels of the boxes you can open after opening the i th box. containedBoxes[i] is a list of the boxes you found inside the i th box. You are given an integer array initialBoxes that contains the labels of the boxes you initially
Detailed Explanation
The problem asks us to find the maximum number of candies we can collect from a set of boxes. We are given information about each box: whether it's initially open or closed, the number of candies it contains, the keys it provides (allowing us to open other boxes), and the boxes it contains. We start with a set of initial boxes. We can only collect candies from open boxes. Opening a box also gives us access to its keys and contained boxes. The goal is to maximize the total candies collected by strategically opening boxes and using keys.
Solution Approach
The solution uses a Breadth-First Search (BFS) approach. We maintain a queue of open boxes whose candies we can collect. We also use boolean arrays to track which boxes we *have* (contained or initially owned) and which boxes have been *processed* (candies collected and keys/contained boxes utilized). We iterate through the queue, collecting candies, unlocking new boxes using keys, and adding new boxes to our inventory. A key aspect is that if a key is obtained that opens a box we already have, we add that box to the queue if it hasn't already been processed.
Step-by-Step Algorithm
- Step 1: Initialize data structures: a queue for BFS, a boolean array `have_box` to track which boxes we possess, and a boolean array `processed` to track which boxes have been processed.
- Step 2: Add the initial boxes to the `have_box` array. If an initial box is open, add it to the queue and mark it as processed.
- Step 3: While the queue is not empty, dequeue a box.
- Step 4: Add the candies from the current box to the total candies collected.
- Step 5: Iterate through the keys found in the current box. Open the corresponding boxes by setting their status to 1. If we have that box and it hasn't been processed yet, enqueue it and mark it as processed.
- Step 6: Iterate through the boxes contained in the current box. Add these boxes to our inventory by setting `have_box[new_box] = true`. If the box is open and hasn't been processed yet, enqueue it and mark it as processed.
- Step 7: Repeat steps 3-6 until the queue is empty.
- Step 8: Return the total number of candies collected.
Key Insights
- Insight 1: This is a graph traversal problem where boxes are nodes, and keys unlock access to other nodes. We can use Breadth-First Search (BFS) to explore the boxes.
- Insight 2: We need to keep track of which boxes we *have* (contained) and which boxes we can *access* (open). A boolean array is suitable for this.
- Insight 3: We need to handle cases where we have a box but it's closed, and we may get the key later. We need to revisit these boxes when new keys are acquired.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Breadth-First Search, Graph.
Companies
Asked at: Airbnb, Lyft.