【leetcode】1039. Minimum Score Triangulation of Polygon
题目如下:
Given
N, consider a convexN-sided polygon with vertices labelledA[0], A[i], ..., A[N-1]in clockwise order.Suppose you triangulate the polygon into
N-2triangles. 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 allN-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.Note:
3 <= A.length <= 501 <= 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的更多相关文章
- LeetCode 1039. Minimum Score Triangulation of Polygon
原题链接在这里:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题目: Given N, consider ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 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】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【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 ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
随机推荐
- Oracle12c ASMM和hugepage
在Oracle 12c,测试发现默认并不启用AMM 特性,而是使用了ASMM.因为在X86 架构下的linux 平台中,配置hugepage时,也是需要关闭AMM,使用ASMM特性. AMM分配内存的 ...
- MySQL的性能指标计算和优化方法
MySQL的性能指标计算和优化方法1 QPS计算(每秒查询数) 针对MyISAM引擎为主的DB mysql> show global status like 'questions';+----- ...
- 二十九、pycharm中报错“too many blank lines (3) ”等类似错误
报错如下图: 解决方法一: 鼠标移至报错处,按住Alt+enter键,选择ignore errors like this 方法二:找到设置File - Settings…… - Editor - In ...
- Redis安装启动,Redis Desktop Manager安装
Window 下安装下载地址:https://github.com/MSOpenTech/redis/releases.Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择,这 ...
- floding regions
- Binder的Native实现libbinder
libbinder – Binder的Native实现 出于性能和代码统一性的角度考虑,Binder IPC并不Java和Native环境里各实现一次,而只是分别在不同的执行环境里提供使用的接口.使用 ...
- vue复合组件----注册表单
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- vue通信之子父组件通信
子父组件通信: 创建一个父组件 Home , 创建一个子组件 Head Home 组件: import Head from "./Head.vue" // 引入 Head 组件 c ...
- 浅谈WebService开发二(同步与异步调用)转
上文 <http://www.dotnetgeek.cn/xuexiwebservice1.html>已经跟大家说了,如果创建一个webservice和简单的调用,本文将注重webserv ...
- Python基础-1 python由来 Python安装入门 注释 pyc文件 python变量 获取用户输入 流程控制if while
1.Python由来 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚 ...
