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 <= 50
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的更多相关文章
- 【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 ...
随机推荐
- 长乐国庆集训Day2
T1 连珠风暴 题目 [题目描述] 给定M种颜色的珠子,每种颜色珠子的个数均不限,将这些珠子做成长度为N的项链. 问能做成多少种不重复的项链.两条项链相同,当且仅当两条项链通过旋转或是翻转后能重合在一 ...
- C++错题记录
D. 通俗讲 , 前置++ : 先自增,再赋值 后置++: 先赋值,再自增 从反汇编中,可以看出: 前置++效率比后置++高 前置++: 自增后返回引用 后置++: 拷贝一份临时变量,再自增 ...
- 分布式缓存重建并发冲突和zookeeper分布式锁解决方案
如果缓存服务在本地的ehcache中都读取不到数据. 这个时候就意味着,需要重新到源头的服务中去拉去数据,拉取到数据之后,赶紧先给nginx的请求返回,同时将数据写入ehcache和redis中 分布 ...
- java之mybatis之helloworld
1. MyBatis 是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架. MyBatis几乎消除了所有的 JDBC 代码,也基本不需要手工去设置参数和获取检索结果. MyBatis几乎能够 ...
- C语言 - 可变参数再stm32中的应用
参考 C 可变参数 | 菜鸟教程 void func(const char* str,...) { ... } func的最后一个参数写成 ... ,表示可变参数, C语言的printf就是类似这种声 ...
- zookerper安装使用教程
转载自 http://blog.java1234.com/blog/articles/379.html 再安装zookeeper之前,我们看下zookeeper简介 https://baike.bai ...
- 如何使用Git 优雅的版本回退呢?
在版本迭代开发过程中,相信很多人都会有过错误提交的时候(至少良许有过几次这样的体验).这种情况下,菜鸟程序员可能就会虎驱一震,紧张得不知所措.而资深程序员就会微微一笑,摸一摸锃亮的脑门,然后默默的进行 ...
- old english diamaund钻石
Diamond Di"a*mond (?; 277), n. [OE. diamaund, the hardest iron, steel, diamond, Gr. . Perh. the ...
- MVC、MVP及MVVM之间的关系
介绍 写这篇随笔完全是为了加深自己的印象,毕竟写比看能获得得更多,另外本人对这三种模式的认识还是浅薄的,有待在以后的工作学习中有更深入的理解,因此不免会有误解,这里推荐大家阅读廖雪峰关于MVVM的介绍 ...
- MySQL DataType--定点数(Fixed-Point Types)学习
DECIMAL和NUMERIC MySQL支持两种定点数类型:DECIMAL和NUMERIC,而NUMERIC实现为DECIMAL,因此MySQL中DECIMAL和NUMERIC等价相同. 如使用下面 ...