原题链接在这里:https://leetcode.com/problems/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.

Note:

  1. 3 <= A.length <= 50
  2. 1 <= A[i] <= 100

题解:

Edge between A[i] and A[j] would construct only one triangle in polygon. With k between i and j, these 3 nodes construct trangle and the rest i~k, and k~j are polygons. Maintain the minimum.

Let dp[i][j] denotes the minimum score got using nodes from A[i] to A[j].

For all k bigger than i and smaller than j, maintain the mimimum score by min(dp[i][k] + dp[k][j] + A[i]*A[j]*A[k]).

Time Complexity: O(n^3). n = A.length.

Space: O(n^2).

AC Java:

 class Solution {
public int minScoreTriangulation(int[] A) {
int n = A.length;
int [][] dp = new int[n][n];
for(int d = 2; d<n; d++){
for(int i = 0; i+d<n; i++){
int j = i+d;
dp[i][j] = Integer.MAX_VALUE;
for(int k = i+1; k<j; k++){
dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
}
}
} return dp[0][n-1];
}
}

Another implementation.

 class Solution {
public int minScoreTriangulation(int[] A) {
int n = A.length;
int [][] dp = new int[n][n];
for(int j = 2; j<n; j++){
for(int i = j-2; i>=0; i--){
dp[i][j] = Integer.MAX_VALUE;
for(int k = i+1; k<j; k++){
dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
}
}
} return dp[0][n-1];
}
}

LeetCode 1039. Minimum Score Triangulation of Polygon的更多相关文章

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

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

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

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

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  6. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  7. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof  ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

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

随机推荐

  1. Symfony框架笔记

    控制器获取前端传入的参数 $parameters = $request->request; $data = $parameters->all(); 引用静态资源(js.css.img) 使 ...

  2. xorm-删除和软删除实例

    删除数据Delete方法,参数为struct的指针并且成为查询条件.注意:当删除时,如果user中包含有bool,float64或者float32类型,有可能会使删除失败 package main i ...

  3. 小菜鸡deepin系统手动更新火狐浏览器

    前言 Deepin 是个好系统,让我看到国产系统的希望,也让我看到Linux桌面化和大众化的可能(如果你想抬杠:Deepin只是Linux魔改没什么好显摆的.那--你开心就好 ^ _ ^ ).虽然有一 ...

  4. @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本

    今天遇到了很奇葩的问题,我本机的是以test环境启动的,调用另一个服务接口的时候返回参数却不同,调用接口是没错,怎么会这样,排查了很久,发现在eureka上注册的另一个服务是testone环境,而这个 ...

  5. CLRS最大子数组问题

    今天我们一起来看一下关于最大子数组的一些问题.最大子数组的应用场景可以是这样的:有一天,你搞了一场投资开始炒股,这时你就会想,我怎样才能获得最大的利润呢,最简单的想法就是我在股票的最低价时买入,然后在 ...

  6. 2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐

    原文:2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐 image Raspberry Pi** 是一款基于 ARM 的单板计算机,默认运行一款称为 Raspbian ...

  7. Matlab建造者模式

    构建者模式又叫建造者模式(Builder),是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示.创建者模式隐藏了复杂对象的创建过程,它把复杂对象的创建过程加以抽象,通过子类继 ...

  8. python错误日志记录工具,解决项目排错问题

    我们写项目的时候难免会遇到代码报错的问题,遇到这样的问题了如何快速的定位问题并解决问题呢? 我今天来整理了利用python只带的工具来解决这个问题,我能需要使用的库有: logging os 这些都是 ...

  9. 基于代理类实现Spring AOP

    目录 ProxyFactoryBean类介绍 基于JDK动态代理的Spring  AOP实现 基于CGLIB代理的Spring  AOP实现 Spring的通知类型 ProxyFactoryBean类 ...

  10. 继 首次使用DoNetCore EFCore DbFirst 更新数据实体

    //EFCore DB First 步骤 //第一步:Install-Package Microsoft.EntityFrameworkCore.SqlServer -version 2.1.1 // ...