1196. Minimum Cost Tree from Leaf Values

MediumDynamic ProgrammingInterval DP

Given arr, build a binary tree whose leaves are arr in order (in-order). Each internal node's value is the product of the largest leaf in its left subtree and the largest leaf in its right subtree. Return the minimum possible sum of all internal node values. The input is JSON {arr}.

Input: JSON {arr}.

Output: Integer — the minimum sum of internal node values.

Examples

Example 1
Input: {"arr":[6,2,4]}
Output: 32
Explanation: An optimal tree sums to 32.
Example 2
Input: {"arr":[1,2,3,4]}
Output: 20
Explanation: Optimal internal sum.
Example 3
Input: {"arr":[7,12]}
Output: 84
Explanation: Single internal node.

Constraints

Asked by

Google
Solve this problem in the editor →