Skip to content

1298. Maximum Candies You Can Get from Boxes #1762

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to determine the maximum number of candies we can collect by opening boxes according to specific rules. The boxes can be opened if they are initially open or if we have the corresponding key. When a box is opened, we collect its candies, obtain any keys inside it, and gain access to any boxes contained within it. The challenge is to efficiently traverse all possible boxes we can open starting from the initial set of boxes, considering dependencies between keys and boxes.

Approach

  1. Initialization: We maintain three arrays to track:

    • hasBox: Indicates which boxes we currently possess.
    • hasKey: Indicates which keys we have obtained.
    • visited: Marks boxes that have been processed (open…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Jun 3, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Jun 3, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants