The TransCompilator

Solved LeetCode program · 846 of 1,000

#1130 · Minimum Cost Tree From Leaf Values

Medium Array chunk-17.md

Original English source

The leaves is a list of 6, 2, and 4. The count is 3. The costs is a list of 9 zeros. The peaks is a list of 9 zeros.
For every spot from 0 to 2.
Set item (spot times count plus spot) of peaks to item spot of leaves.

For every span from 2 to count.
For every start from 0 to count minus span.
The end is start plus span minus 1. The best is 100000000. The tallpair is 0.
For every split from start to end minus 1.
The leftpeak is item (start times count plus split) of peaks. The rightpeak is item ((split plus 1) times count plus end) of peaks. The pair is leftpeak times rightpeak. The cost is item (start times count plus split) of costs plus item ((split plus 1) times count plus end) of costs plus pair. If cost is less than best, set best to cost, set tallpair to the larger of leftpeak and rightpeak. Set item (start times count plus end) of costs to best. Set item (start times count plus end) of peaks to tallpair.

Show item (count minus 1) of costs.

Open in Playground Browse all 1,000 problems

Expected stdout

32