823. Polygon Triangulation

Medium3.5 points estimatedArrayDynamic Programming3 hints

A surveyor must split a convex plot into triangular parcels. The plot has values.length corner posts listed in order around the boundary, post i carrying the label values[i]. Draw non-crossing diagonals between posts until the whole plot consists of triangles.

A triangle whose corners carry labels a, b and c is charged a*b + b*c + c*a. Return the minimum possible sum of charges over all ways to split the plot. A plot with fewer than three posts has no parcels and costs 0; a plot with exactly three posts is already one triangle.

Aim for time O(n^3) and space O(n^2).

Example 1

Input:
values = [4,9,3,7]
Output:
136
Explanation:

Splitting along the diagonal between the first and third corner gives triangles scoring 75 and 61, total 136; the other diagonal gives 238.

Example 2

Input:
values = [5,2,8,3,6]
Output:
134
Explanation:

Draw both diagonals from the post labelled 2: the three triangles are charged 46, 36 and 52, total 134, the cheapest of the five fans.

Constraints

0 ≤ values.length ≤ 300

1 ≤ values[i] ≤ 100

The answer fits in a 32-bit signed integer.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n^3)
Space
O(n^2)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…