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. 分析:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/discuss/286753/C%2B%2B-with-picture
If we pick a side of our polygon, it can form n - 2
triangles. Each such triangle forms 2 sub-polygons. We can analyze n - 2
triangles, and get the minimum score for sub-polygons using the recursion.
This is how this procedure looks for a sub-polygon (filled with diagonal pattern above).
Top-Down Solution
• Fix one side of the polygon i, j and move k within (i, j).
• Calculate score of the i, k, j "orange" triangle.
• Add the score of the "green" polygon i, k using recursion.
• Add the score of the "blue" polygon k, j using recursion.
• Use memoisation to remember minimum scores for each sub-polygons.
class Solution {
public int minScoreTriangulation(int[] arr) {
int len = arr.length;
int[][] lookup = new int[len][len];
return minScoreFromTo(arr, , len - , lookup);
} private int minScoreFromTo(int[] arr, int from, int to, int[][] lookup) {
if (from >= to || from + == to) {
return ;
}
if (lookup[from][to] > ) {
return lookup[from][to];
}
lookup[from][to] = Integer.MAX_VALUE;
for (int mid = from + ; mid < to; mid++) {
lookup[from][to] = Math.min(lookup[from][to], arr[mid] * arr[from] * arr[to] + minScoreFromTo(arr, from, mid, lookup)
+ minScoreFromTo(arr, mid, to, lookup));
}
return lookup[from][to];
}
}
Minimum Score Triangulation of Polygon的更多相关文章
- LeetCode 1039. Minimum Score Triangulation of Polygon
原题链接在这里:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题目: Given N, consider ...
- 【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 ...
- leetcode_1039. Minimum Score Triangulation of Polygon_动态规划
https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题意:给定一个凸的N边形(N<=50),每个顶点有一个 ...
- Leetcode 第135场周赛解题报告
这周比赛的题目很有特点.几道题都需要找到一定的技巧才能巧妙解决,和以往靠数据结构的题目不太一样. 就是如果懂原理,代码会很简单,如果暴力做,也能做出来,但是十分容易出错. 第四题还挺难想的,想了好久才 ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- LabVIEW部分视觉函数中文解说
IMAQ Learn Pattern 2 VI 在匹配阶段创建您要搜索的图案匹配的模板图像的描述,此描述的数据被附加到输入模板图像中.在匹配阶段,从模板图像中提取模板描述符并且用于从检查图像中搜索模板 ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- Quiz(贪心,快速幂乘)
C. Quiz time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- codeforces 337C Quiz(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Quiz Manao is taking part in a quiz. The ...
随机推荐
- P4981 父子 Cayley公式
CayleyCayley公式的定义是这样的,对于n个不同的节点,能够组成的无根树(原来是无向连通图或者是有标志节点的树)的种数是n^(n-2)种.(这里让大家好理解一点,就写成了无根树,其实应该是一样 ...
- 转:玩转HTML5移动页面(动效篇)
作为一名前端,在拿到设计稿时你有两种选择: 1.快速输出静态页面 2.加上高级大气上档次狂拽炫酷屌炸天的动画让页面动起来 作为一个有志向的前端,当然是选2啦!可是需求时间又很短很短,怎么办呢? 这次就 ...
- fanout(Publish/Subscribe)发布/订阅
引言 它是一种通过广播方式发送消息的路由器,所有和exchange建立的绑定关系的队列都会接收到消息 不处理路由键,只需要简单的将队列绑定到交换机上 fanout交换机转发消息是最快的,它不需要做路由 ...
- [CSP-S模拟测试]:密码(数位DP+库默尔定理)
题目描述 为了揭穿$SERN$的阴谋,$Itaru$黑进了$SERN$的网络系统.然而,想要完全控制$SERN$,还需要知道管理员密码.$Itaru$从截获的信息中发现,$SERN$的管理员密码是两个 ...
- 全链路跟踪skywalking简介
该文章主要包括以下内容: skywalking的简介 skywalking的使用,支持多种调用中间件(httpclent,springmvc,dubbo,mysql等等) skywalking的tra ...
- 石川es6课程---6、解构赋值
石川es6课程---6.解构赋值 一.总结 一句话总结: 结构相同一一对应的方式赋值:let [json, arr, num, str] = [{ a: 1, b: 2 }, [1, 2, 3], 8 ...
- 极光推送报错time_to_live value should be a non-negative integertime_to_live value should be a non-negative integer
文件中修改
- laravel中跟据某个特定顺序去排序查出来的数据:FIND_IN_SET
//返回有顺序的客户id $customer_ids = $customer->bespeakTime($uid); $res = Customer::with('customer_indust ...
- koa 基础(二十六)数据库 与 art-template 模板 联动 --- 编辑数据、删除数据
1.通过 ObjectID 获取 _id 根目录/module/db.js /** * DB库 */ var MongoDB = require('mongodb'); var MongoClient ...
- python sqlalchemy 进行 mysql 数据库操作
1. 进行mysql数据库的创建,如果已经存在,就相当于进行数据库的连接操作 from sqlalchemy import create_engine from sqlalchemy.ext.decl ...