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-2triangles 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的更多相关文章

  1. LeetCode 1039. Minimum Score Triangulation of Polygon

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

  2. 【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 ...

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

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

  4. Leetcode 第135场周赛解题报告

    这周比赛的题目很有特点.几道题都需要找到一定的技巧才能巧妙解决,和以往靠数据结构的题目不太一样. 就是如果懂原理,代码会很简单,如果暴力做,也能做出来,但是十分容易出错. 第四题还挺难想的,想了好久才 ...

  5. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  6. LabVIEW部分视觉函数中文解说

    IMAQ Learn Pattern 2 VI 在匹配阶段创建您要搜索的图案匹配的模板图像的描述,此描述的数据被附加到输入模板图像中.在匹配阶段,从模板图像中提取模板描述符并且用于从检查图像中搜索模板 ...

  7. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  8. Quiz(贪心,快速幂乘)

    C. Quiz time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  9. codeforces 337C Quiz(贪心)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Quiz Manao is taking part in a quiz. The ...

随机推荐

  1. Blade 模板

    在Laravel 5.3中,@foreach指令提供了更加强大的功能,在每一个@foreach循环体中都可以调用一个新的$loop变量.该变量是一个stdClass实例,包含了当前循环的元数据信息,让 ...

  2. springboot错误1 Failed to execute goal org.springframework.boot:spring-boot-maven-plugin

    关于Springboot打包错误的问题 | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin https ...

  3. List集合、泛型、装箱拆箱

    1.List集合 Vector:增删改查都慢 线程同步 线程安全 LlinkedList:以链表结构存储数据,查询慢.增删快 ArrayList:的运行速度比较快 连续数据空间存储数据,查询快(下标) ...

  4. 包管理神器-pipenv

    一:前言 介绍一个包管理神器-pipenv,这个工具可以让我们在写代码.创建Python运行环境.package依赖关系以及项目合作的时候更有效率. 在pycon2018上,Kenneth Reitz ...

  5. Zabbix 4.0.2试用(七):在Linux主机中安装zabbix agent并添加该主机(yum源安装)

    Zabbix 4.0.2试用(七):在Linux主机中安装zabbix agent并添加主机(yum源安装) 2018年12月20日, 上午6:42 之前介绍的是下载源安装包,编译安装的方式来安装ag ...

  6. springMVC中的ModelAndView说明

    ModelAndView 类别就如其名称所示,是代表了Spring Web MVC程式中呈现画面时所使用Model资料物件与View资料物件,由于Java程式中一次只能返回一个物件,所以ModelAn ...

  7. Java-数据类型与编码(ASCII、Unicode 和 UTF-8)

    机械硬盘硬件结构(了解)https://diy.pconline.com.cn/cpu/study_cpu/1009/2215404_all.html 一.数据储存单位 1.bit(位) https: ...

  8. 浅谈 Nginx和LVS的各种优缺点

    VS的负载能力强,因为其工作方式逻辑非常简单,仅进行请求分发,而且工作在网络的第4层,没有流量,所以其效率不需要有过多的忧虑. LVS基本能支持所有应用,因为工作在第4层,所以LVS可以对几乎所有应用 ...

  9. [Python]python-jenkins 启动需要参数的job

    需求: 我要用python通过api,启动这个job,并且启动这个job需要1个参数 安装依赖: pipenv install python-jenkins 熟悉API的使用方法: 了解一个API的最 ...

  10. tensorflow文件读取

    1.知识点 """ 注意:在tensorflow当中,运行操作具有依赖性 1.CPU操作计算与IO计算区别: CPU操作: 1.tensorflow是一个正真的多线程,并 ...