Problem

You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex in clockwise order.

Polygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in n - 2 triangles.

You will triangulate the polygon. For each triangle, the weight of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all n - 2 triangles.

Return the minimum possible score that you can achieve with some triangulation of the polygon.

Examples

Example 1

1
2
3
Input: values = [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.

Example 2

1
2
3
4
Input: values = [3,7,4,5]
Output: 144
Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
The minimum score is 144.

Example 3

1
2
3
Input: values = [1,3,1,4,1,5]
Output: 13
Explanation: The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.

Constraints

  • n == values.length
  • 3 <= n <= 50
  • 1 <= values[i] <= 100

Solution

Method 1 – Dynamic Programming (Interval DP)

Intuition

We can break the polygon into smaller sub-polygons and solve for each interval. The minimum triangulation for an interval is the minimum over all possible ways to split it by choosing a third vertex.

Approach

  1. Let dp[i][j] be the minimum score to triangulate the polygon from vertex i to j.
  2. For each interval length from 3 to n, compute dp[i][j] by trying all possible k between i and j.
  3. The answer is dp[0][n-1].

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
public:
    int minScoreTriangulation(vector<int>& values) {
        int n = values.size();
        vector<vector<int>> dp(n, vector<int>(n));
        for (int l = 3; l <= n; ++l) {
            for (int i = 0; i + l <= n; ++i) {
                int j = i + l - 1;
                dp[i][j] = INT_MAX;
                for (int k = i + 1; k < j; ++k)
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i]*values[j]*values[k]);
            }
        }
        return dp[0][n-1];
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func minScoreTriangulation(values []int) int {
    n := len(values)
    dp := make([][]int, n)
    for i := range dp {
        dp[i] = make([]int, n)
    }
    for l := 3; l <= n; l++ {
        for i := 0; i+l <= n; i++ {
            j := i + l - 1
            dp[i][j] = 1 << 30
            for k := i + 1; k < j; k++ {
                v := dp[i][k] + dp[k][j] + values[i]*values[j]*values[k]
                if v < dp[i][j] { dp[i][j] = v }
            }
        }
    }
    return dp[0][n-1]
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    public int minScoreTriangulation(int[] values) {
        int n = values.length;
        int[][] dp = new int[n][n];
        for (int l = 3; l <= n; ++l) {
            for (int i = 0; i + l <= n; ++i) {
                int j = i + l - 1;
                dp[i][j] = Integer.MAX_VALUE;
                for (int k = i + 1; k < j; ++k)
                    dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + values[i]*values[j]*values[k]);
            }
        }
        return dp[0][n-1];
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    fun minScoreTriangulation(values: IntArray): Int {
        val n = values.size
        val dp = Array(n) { IntArray(n) }
        for (l in 3..n) {
            for (i in 0..n-l) {
                val j = i + l - 1
                dp[i][j] = Int.MAX_VALUE
                for (k in i+1 until j)
                    dp[i][j] = minOf(dp[i][j], dp[i][k] + dp[k][j] + values[i]*values[j]*values[k])
            }
        }
        return dp[0][n-1]
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
    def minScoreTriangulation(self, values: list[int]) -> int:
        n = len(values)
        dp = [[0]*n for _ in range(n)]
        for l in range(3, n+1):
            for i in range(n-l+1):
                j = i + l - 1
                dp[i][j] = float('inf')
                for k in range(i+1, j):
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i]*values[j]*values[k])
        return dp[0][n-1]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
impl Solution {
    pub fn min_score_triangulation(values: Vec<i32>) -> i32 {
        let n = values.len();
        let mut dp = vec![vec![0; n]; n];
        for l in 3..=n {
            for i in 0..=n-l {
                let j = i + l - 1;
                dp[i][j] = i32::MAX;
                for k in i+1..j {
                    dp[i][j] = dp[i][j].min(dp[i][k] + dp[k][j] + values[i]*values[j]*values[k]);
                }
            }
        }
        dp[0][n-1]
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    minScoreTriangulation(values: number[]): number {
        const n = values.length;
        const dp = Array.from({length: n}, () => Array(n).fill(0));
        for (let l = 3; l <= n; l++) {
            for (let i = 0; i + l <= n; i++) {
                const j = i + l - 1;
                dp[i][j] = Infinity;
                for (let k = i + 1; k < j; k++)
                    dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + values[i]*values[j]*values[k]);
            }
        }
        return dp[0][n-1];
    }
}

Complexity

  • ⏰ Time complexity: O(n^3) where n is the number of vertices. Each interval and split is checked.
  • 🧺 Space complexity: O(n^2) for the DP table.