The TransCompilator

Solved LeetCode program · 934 of 1,000

#1298 · Maximum Candies You Can Get from Boxes

Hard Array chunk-19.md

Original English source

Keep a shared status holding 1, 0, 1, and 0. Keep a shared candies holding 7, 5, 4, and 100. Keep a shared keyfrom as an empty list of whole numbers. Place 2 into keyfrom. Keep a shared keybox as an empty list of whole numbers. Place 1 into keybox. Keep a shared boxstarts holding 0, 1, 2, and 2. Keep a shared boxcounts holding 2, 1, 0, and 0. Keep a shared boxitems holding 1, 2, and 3. Keep a shared held as a list of 4 zeros. Keep a shared opened as a list of 4 zeros. Keep a shared havekey as a list of 4 zeros. Keep a shared queue as an empty list of whole numbers. Keep a shared tally of 0.

To unlock from box (a whole number).
For every key from 0 to the last position of keyfrom.
If item key of keyfrom is box.
The door is item key of keybox. Set item door of havekey to 1. If item door of held is 1 and item door of opened is 0, Place door into queue, add 1 to tally.

To offer from box (a whole number). The start is item box of boxstarts. The many is item box of boxcounts.
If many is more than 0.
For every offset from 0 to many minus 1.
The inner is item (start plus offset) of boxitems. Set item inner of held to 1. If item inner of status is 1 or item inner of havekey is 1, Place inner into queue, add 1 to tally.

The total is 0. Set item 0 of held to 1. Place 0 into queue. Set tally to 1. The head is 0.
Repeat while head is less than tally.
The box is item head of queue. Add 1 to head.
If item box of opened is 0.
Set item box of opened to 1. Increase total by item box of candies. Unlock from box. Offer from box.

Show total.

Open in Playground Browse all 1,000 problems

Expected stdout

16