原题链接在这里: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:

 class Solution {
public int minScoreTriangulation(int[] A) {
int n = A.length;
int [][] dp = new int[n][n];
for(int d = 2; d<n; d++){
for(int i = 0; i+d<n; i++){
int j = i+d;
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]+A[i]*A[j]*A[k]);
}
}
} return dp[0][n-1];
}
}

Another implementation.

 class Solution {
public int minScoreTriangulation(int[] A) {
int n = A.length;
int [][] dp = new int[n][n];
for(int j = 2; j<n; j++){
for(int i = j-2; i>=0; i--){
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]+A[i]*A[j]*A[k]);
}
}
} return dp[0][n-1];
}
}

LeetCode 1039. Minimum Score Triangulation of Polygon的更多相关文章

  1. 【leetcode】1039. Minimum Score Triangulation of Polygon

    题目如下: Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in c ...

  2. Minimum Score Triangulation of Polygon

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

  3. leetcode_1039. Minimum Score Triangulation of Polygon_动态规划

    https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题意:给定一个凸的N边形(N<=50),每个顶点有一个 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  6. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  7. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. spring的exception

    Springmvc的对于异常类进行统一处理的方法 一.局部异常统一处理 当异常出现时,将抛给异常处理方法,异常处理发放接收到异常数据,进行处理,统一到异常页面 @ExceptionHandler:通过 ...

  2. 用NDK生成cURL和OpenSSL库

    最近在用Qt开发Android应用时需要获取https页面内容,但Qt内置的QNetworkAccessManager类只支持下面这些协议(调用其supportedSchemes成员函数获取): (& ...

  3. 洛谷 P1411 树 (树形dp)

    大意: 给定树, 求删除一些边, 使得连通块大小的乘积最大 设$dp_{i,j}$表示只考虑点$i$的子树, $i$所在连通块大小为$j$的最大值. 转移的时候不计算$i$所在连通块的贡献, 留到最后 ...

  4. .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?

    原文:.NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用? 都知道可以在任务管理器中查看进程的 CPU 和内存占用,那么如何通过 .NET 编写代码的方式来获取到 ...

  5. Mars Android 接入指南

    Mars Android 接入指南 https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8 ...

  6. NEST dynamic 和 alias

    /// <summary> /// Dynamic = false无法搜索 /// </summary> public void Dynamicmapping() { var ...

  7. 浮动IP地址(Float IP)与 ARP欺骗技术

    浮动IP地址: 一个网卡是可以添加多个IP的. 就是多个主机工作在 同一个集群中,即两台主机以上.每台机器除了自己的实IP外,会设置一个浮动IP,浮动IP与主机的服务(HTTP服务/邮箱服务)绑在一起 ...

  8. 2019 三七互娱java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.三七互娱等公司offer,岗位是Java后端开发,因为发展原因最终选择去了三七互娱,入职一年时间了,也成为了面 ...

  9. java jar启动

    linux中启动 java -jar 后台运行程序 直接用java -jar xxx.jar,当退出或关闭shell时,程序就会停止掉.以下方法可让jar运行后一直在后台运行. 1. java -ja ...

  10. Qt Creator 的下载与安装

    一.Qt和Qt Creator的区别 Qt是C++的一个库,或者说是开发框架,里面集成了一些库函数,提高开发效率. Qt Creator是一个IDE,就是一个平台,一个开发环境,类似的比如说VS,也可 ...