
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum Score Triangulation of Polygon in C++
Suppose we have a value N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] are in clockwise order. Now suppose we want to triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation will be the sum of these values over all N-2 triangles in the triangulation. We have to find the smallest possible total score that we can achieve with some triangulation of the polygon. So if the input is [1,2,3], then the output will be 6, as the polygon is already triangulated, and the score of the only triangle is 6.
To solve this, we will follow these steps −
Create a matrix dp of size 50 x 50, fill this with 0
n := size of the given array
for l in range n to n
for i := 0, j := l – 1, when j < n, increase i and j by 1
for k in range i + 1 to j – 1
if of dp[i, j] = 0, then
dp[i, j] := minimum of inf and dp[i, k] + dp[k, j] + A[i] * A[j] * A[k]
otherwise dp[i, j] := minimum of dp[i,j] and dp[i, k] + dp[k, j] + A[i] * A[j] * A[k]
return dp[0, n – 1]
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; typedef long long int lli; class Solution { public: int minScoreTriangulation(vector<int>& A) { lli dp[50][50]; for(int i = 0; i < 50; i++){ for(int j = 0; j < 50; j++){ dp[i][j] = 0; } } int n = A.size(); for(int l = 3; l <= n; l++){ for(int i = 0, j = l - 1; j < n;i++, j++){ for(int k = i + 1; k < j; k++){ dp[i][j] = min(dp[i][j] == 0?INT_MAX : dp[i][j], dp[i][k] + dp[k][j] + A[i] * A[k] * A[j]); } } } return dp[0][n - 1]; } }; main(){ vector<int> v1 = {1,2,3}; Solution ob; cout << (ob.minScoreTriangulation(v1)); }
Input
[1,2,3]
Output
6
- Related Articles
- Minimum Cost Polygon Triangulation
- C++ Program to implement Slicker Algorithm that avoids Triangulation to find Area of a Polygon
- Program to find minimum difference of stone games score in Python
- Find minimum score from the entire four columns of a table in MySQL
- What's the minimum exterior angle possible of a regular polygon?
- Score of Parentheses in C++
- Finding score of brackets in JavaScript
- Application of Z-score in Psychology
- (a) What is the minimum interior angle possible for a regular polygon? Why?(b) What is the maximum exterior angle possible for a regular polygon?
- Number of Paths with Max Score in C++
- Convex Polygon in C++
- What is Quality Score? Which 3 Factors Determine Quality Score?
- Center of each side of a polygon in JavaScript
- The interior angle of a regular polygon is $156$ . Find the number of sides of the polygon.
- The length of each side of a regular polygon is 1.3 cm. The perimeter of polygon is 10.4 cm How many sides does the polygon have?
