题目如下:

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

解题思路:这里推荐一本书《趣学算法》,里面有几个专题,讲解也非常有意思。本题对应书中的4.7章:最优三角剖分,解答如下图。

代码如下:

class Solution(object):
def minScoreTriangulation(self, A):
"""
:type A: List[int]
:rtype: int
"""
dp = []
for i in A:
dp.append([0] * len(A))
# dp[i][j] = dp[i][k] + dp[k+1][j] + A[i]+A[j]+A[k]
for i in range(len(A)-3,-1,-1):
for j in range(i+2,len(A)):
for k in range(i+1,j):
if dp[i][j] == 0 or dp[i][j] > dp[i][k] + dp[k][j] + A[i]*A[j]*A[k]:
dp[i][j] = dp[i][k] + dp[k][j] + A[i]*A[j]*A[k]
#print dp
return dp[0][-1]

【leetcode】1039. 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】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  3. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  4. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  5. 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 ...

  6. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  7. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

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

  8. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  9. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

随机推荐

  1. DRF 组件

    DRF组件中的认证  授权  频率限制   分页  注册器  url控件

  2. KVM/QEMU/qemu-kvm/libvirt 概念全解

    目录 目录 前言 KVM QEMU KVM 与 QEMU qemu-kvm Libvirt Libvirt 在 OpenStack 中的应用 前言 如果是刚开始接触虚拟机技术的话, 对上述的概念肯定会 ...

  3. Jmeter之Switch Controller

    在测试过程中,各种不同的情况需要执行不同的操作,这个时候用if控制器比较麻烦,此时就可以使用Switch Controller代替. 一.界面显示 二.配置说明 1.名称:标识 2.注释:备注 3.S ...

  4. 接口自动化之提取响应结果(raw、json)

    做接口测试时,我们一般会取得响应结果的部分值,来与预期结果对比,做断言 目录 1.响应结果类型 2.json响应结果的提取 3.raw响应结果的提取 1.响应结果类型 通过fiddler的respon ...

  5. Jenkins持续集成_04_解决HTML测试报告样式丢失问题

    前言 最近进行Jenkins自动化测试持续集成,配置HTML测试报告后,但是点击进去发现测试报告样式丢失,未加载CSS&JS样式,如下图: 由于Jenkins中配置了CSP(Content S ...

  6. [Git] 002 初识 Git 与 GitHub 之加入文件 第一弹

    在 GitHub 的 UI 界面使用 Git 往仓库里加文件 第一弹 1. 点击右上方的 Create new file 2. 在左上方填入文件名,若有后缀,记得加上 3. 页面跳转,此时已有两个文件 ...

  7. [Python3 练习] 006 汉诺塔2 非递归解法

    题目:汉诺塔 II 接上一篇 [Python3 练习] 005 汉诺塔1 递归解法 这次不使用递归 不限定层数 (1) 解决方式 利用"二进制" (2) 具体说明 统一起见 我把左 ...

  8. java高级开发面试总结

    Java高级工程师面试题总结及参考答案 (转载)博客原文链接:https://www.cnblogs.com/java1024/p/8594784.html 一.面试题基础总结 1. JVM结构原理. ...

  9. dp(最大分段和)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Othe ...

  10. 浅谈格雷码(Grey Code)在信息学竞赛中的应用

    1.格雷码的概念 1.性质 格雷码(Grey Code),又叫循环二进制码或反射二进制码,是一种编码方式,它的基本特点是任意两个相邻的格雷码只有一位二进制数不同. 常用的二进制数与格雷码间的转换关系如 ...