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:
3 <= A.length <= 501 <= 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的更多相关文章
- 【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 ...
- 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 ...
- leetcode_1039. Minimum Score Triangulation of Polygon_动态规划
https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题意:给定一个凸的N边形(N<=50),每个顶点有一个 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
随机推荐
- Django框架之DRF APIView Serializer
一.APIView 我们在使用DjangoRestfulFramework的时候会将每个视图类继承APIView,取代原生Django的View类 APIView的流程分析: rest_framewo ...
- KEPServerEX 6 配置连接 Allen-Bradley MicroLogix 1400
=============================================== 2019/7/28_第1次修改 ccb_warlock == ...
- 对于Node中Express框架的中间件概念的感知
中间件是什么呢? 中间件就是客户端http请求发起传送到服务器和服务器返回响应之间的一些处理函数. 为什么要使用中间件? 通过中间件,可以对数据进行操作使得我们能方便地操作请求数据编写服务器响应.如b ...
- C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?
原文:C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思? 在 .NET 中创建进程时,可以传入 ProcessStartInfo ...
- C#——零散学习
C#——零散学习0 //控制台输入字符串,转化为int,double,float等数值类型: //Convert.ToXXX32();函数. Convert.ToInt32(); //把字符串转换为i ...
- C# vb .net实现玻璃桌子效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的玻璃桌子效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- "Sed" 高级实用功能汇总
sed命令有两个空间,一个叫pattern space,一个叫hold space.这两个空间能够证明人类的脑瓜容量是非常小的,需要经过大量的训练和烧脑的理解,才能适应一些非常简单的操作. 不信看下面 ...
- .net 后台以post方式调用微信公众平台接口
public class Fresult { public int errcode { get; set; } public string errmsg { get; set; } public st ...
- 随笔小skill
1.用拉链函数zip()将字典转换成元组对!函数中的两个参数必须是序列!p = {'name':'zhangsanfeng','age':18,'gender':'nan'}print(list(zi ...
- static 关键字在java语言中的特性
一,将自己注入到一个静态变量中实现静态类,如下写法 以上方法的目的是要实现一个静态类,方便用类名获取对象实例,一般情况下调用普通方法需要对象实例.这对象要么new出来,要么spring的注入如下是 ...