zoukankan      html  css  js  c++  java
  • LeetCode 1039. Minimum Score Triangulation of Polygon

    原题链接在这里:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/

    题目:

    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:

    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

    题解:

    Edge between A[i] and A[j] would construct only one triangle in polygon. With k between i and j, these 3 nodes construct trangle and the rest i~k, and k~j are polygons. Maintain the minimum.

    Let dp[i][j] denotes the minimum score got using nodes from A[i] to A[j].

    For all k bigger than i and smaller than j, maintain the mimimum score by min(dp[i][k] + dp[k][j] + A[i]*A[j]*A[k]).

    Time Complexity: O(n^3). n = A.length.

    Space: O(n^2).

    AC Java:

     1 class Solution {
     2     public int minScoreTriangulation(int[] A) {
     3         int n = A.length;
     4         int [][] dp = new int[n][n];
     5         for(int d = 2; d<n; d++){
     6             for(int i = 0; i+d<n; i++){
     7                 int j = i+d;
     8                 dp[i][j] = Integer.MAX_VALUE;
     9                 for(int k = i+1; k<j; k++){
    10                     dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
    11                 }
    12             }
    13         }
    14         
    15         return dp[0][n-1];
    16     }
    17 }

    Another implementation.

     1 class Solution {
     2     public int minScoreTriangulation(int[] A) {
     3         int n = A.length;
     4         int [][] dp = new int[n][n];
     5         for(int j = 2; j<n; j++){
     6             for(int i = j-2; i>=0; i--){
     7                 dp[i][j] = Integer.MAX_VALUE;
     8                 for(int k = i+1; k<j; k++){
     9                     dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
    10                 }
    11             }
    12         }
    13         
    14         return dp[0][n-1];
    15     }
    16 }
  • 相关阅读:
    简单说说sharepoint虚机 金大昊(jindahao)
    SharePoint 自定义UI 金大昊(jindahao)
    学习SharePoint 2010 UI 总结 金大昊(jindahao)
    隐藏sharepoint2010列表的下拉菜单 金大昊(jindahao)
    安装Office Web Apps 2010 金大昊(jindahao)
    corev4.css
    SharePoint 2010的Form认证用户维护页面 金大昊(jindahao)
    今天买了两本中文SharePoint2010书 金大昊(jindahao)
    痛苦的日立移动硬盘 金大昊(jindahao)
    liferay开发环境创建
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11576043.html
Copyright © 2011-2022 走看看