题目如下:

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. 获取oracle数据库对象定义

    在oracle中,使用DBMS_METADATA包中的GET_DDL函数来获得对应对象的定义语句.GET_DDL函数的定义如下: DBMS_METADATA.GET_DDL ( object_type ...

  2. 线段树板子1(洛谷P3372)

    传送 一道线段树板子(最简单的) 似乎之前在培训里写过线段树的样子?不记得了 何为线段树? 一般就是长成这样的树,树上的每个节点代表一个区间.线段树一般用于区间修改,区间查询的问题. 我们如何种写一棵 ...

  3. 在阿里云centOS7上部署Redis 5.0.5主从 + 哨兵模式

    一.在两台服务器上分别安装.配置Redis 5.0.5 ,为一主一从 安装Redis关键命令: 将安装包上传至:/home 目录下解 压:.tar.gz 安装依赖:yum install gcc 安装 ...

  4. ConcurrentSkipListMap 源码分析

    ConcurrentSkipListMap ConcurrentSkipListMap 能解决什么问题?什么时候使用 ConcurrentSkipListMap? 1)ConcurrentSkipLi ...

  5. Understanding ECMAScript 6 阅读问题小记

    拖了一年说要看这本书,一直都没坚持下来,开个 bo 记录下觉得疑惑的问题,也算鞭策一下自己. 第一章 块级绑定 1. 第一章“块级绑定”下,说 const 变量如果绑定的是对象 Object,那么修改 ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_06 Properties集合_1_使用Properties集合存储数据,遍历取出集合中的数据

    map下面的实现类叫做Hashtable Properties是唯一和IO流相结合的 讲解 代码

  7. iView 实战系列教程(21课时)_1.iView 实战教程之配置篇_图片优化

    首先需要安装vue cli 3.0版本 点击添加插件, 输入iview 选中后安装 全部导入还是按需导入. 2.是否需要自定义主题变量 3.多语言的设置. 这里我们全部选择为默认 然后点击继续. 启动 ...

  8. windows10下安装docker报错:error during connect

    详细报错信息如下: C:\Users\zig>docker info error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engin ...

  9. mysql 添加外键报错:

    1.报错信息 Cannot add or update a child row: a foreign key constraint fails 2.原因分析 [1]字段的数据类型 父表: 子表: 以上 ...

  10. 【ABAP系列】SAP 关于出口(user-exit)MV50AFZ1的一些问题

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 关于出口(user-ex ...