Trains the technique from
LeetCode 1298Maximum Candies You Can Get from BoxesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A relief depot holds n crates labelled 0 through n - 1. For crate i:
unlocked[i] is 1 when its clasp is already free and 0 when the clasp is sealed;rations[i] is how many ration packs are inside it;keycards[i] lists the labels of the crates whose clasps the keycards inside crate i will free;nested[i] lists the labels of the crates packed inside crate i.You arrive holding exactly the crates listed in startCrates. You may open a crate you are holding when its clasp is free, either because it started that way or because you have picked up a keycard for it. Opening a crate hands you all of its ration packs, all of its keycards and all of the crates packed inside it, and those crates are then in your hands too.
A keycard is never used up: once you hold the keycard for a crate, that crate's clasp counts as free from then on, including for a crate that has been sitting in your hands unopened. You may open crates in any order you like and you take nothing out of a crate you never open.
Return the largest number of ration packs you can end up with.
Example 1
Crate 1 arrives with a free clasp, so opening it banks 2 packs and hands over a keycard for crate 0. Crate 0 is already in hand, and with its clasp now free it opens for 5 more packs. Crate 2 is never in hand, so its 9 packs stay in the depot.
Example 2
Crate 0 opens for 1 pack and yields crate 1, whose clasp is sealed and for which no keycard exists anywhere, so crate 1 is never opened and crates 2 and 3 packed below it are never in hand.
Example 3
You arrive holding no crates at all, so there is nothing to open and no packs to collect.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def max_rations(unlocked: list[int], rations: list[int], keycards: list[list[int]], nested: list[list[int]], startCrates: list[int]) -> int:public int maxRations(int[] unlocked, int[] rations, int[][] keycards, int[][] nested, int[] startCrates)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.