Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Minimum Score Triangulation of Polygon

Posted on August 5, 2019July 26, 2020 by braindenny

Minimum Score Triangulation of Polygon



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #dynamicprogramming, #intervaldp

Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], …, A[N-1] in clockwise order.

Suppose you 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 is the sum of these values over all N-2 triangles in the triangulation.

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

Example 1:

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

Example 2:
Minimum Score Triangulation of Polygon

Input: [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:

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

Note:

  1. 3 <= A.length <= 50
  2. 1 <= A[i] <= 100

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/minimum-score-triangulation-of-polygon
// Basic Ideas: math
//
// Observation: for each triangle, one node can be removed
//   So dp(i) fall back to dp(i-1)
// Observation: There must be one triangle with A[0], A[N-1] + A[i]
//    The sum would be A[0...i], A[i...N-1] + A[0]*A[N-1]*A[i]
//    Loop all the possiblity of i, we can get the result.
//
//   dp(i, j): value from A[i...j]
//
// Notice: there may not exist a central point to divide the polygon
//
// Complexity: Time O(n), Space O(1)
func minScoreTriangulation(A []int) int {
    dp := make([][]int, len(A))
    for i, _ := range dp {
        dp[i] = make([]int, len(A))
    }
    // dp from diagonal line
    for i:=len(A)-1; i>=0; i-- {
        for j:=i; j<len(A); j++ {
            // A[i...j]
            for k:=i+1; k<j; k++ {
                if dp[i][j] == 0 {
                    // set initial value
                    dp[i][j] = 1<<31-1
                }
                v := dp[i][k] + A[i]*A[k]*A[j] + dp[k][j]
                if dp[i][j] > v {
                    dp[i][j] = v
                }
            }
        }
    }
    return dp[0][len(A)-1]
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #dynamicprogramming, intervaldp

Post navigation

LeetCode: Numbers With Same Consecutive Differences
LeetCode: Shopping Offers

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.