LeetCode 1039. Minimum Score Triangulation of Polygon

原题链接在这里: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:

复制代码
 1 class Solution {
 2     public int minScoreTriangulation(int[] A) {
 3         int n = A.length;
 4         int [][] dp = new int[n][n];
 5         for(int d = 2; d<n; d++){
 6             for(int i = 0; i+d<n; i++){
 7                 int j = i+d;
 8                 dp[i][j] = Integer.MAX_VALUE;
 9                 for(int k = i+1; k<j; k++){
10                     dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
11                 }
12             }
13         }
14         
15         return dp[0][n-1];
16     }
17 }
复制代码

Another implementation.

复制代码
 1 class Solution {
 2     public int minScoreTriangulation(int[] A) {
 3         int n = A.length;
 4         int [][] dp = new int[n][n];
 5         for(int j = 2; j<n; j++){
 6             for(int i = j-2; i>=0; i--){
 7                 dp[i][j] = Integer.MAX_VALUE;
 8                 for(int k = i+1; k<j; k++){
 9                     dp[i][j] = Math.min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
10                 }
11             }
12         }
13         
14         return dp[0][n-1];
15     }
16 }
复制代码

 

posted @ 2019-09-24 00:43  Dylan_Java_NYC  阅读(392)  评论(0)    收藏  举报
编辑推荐:
· 复杂业务系统线上问题排查过程
· 通过抓包,深入揭秘MCP协议底层通信
· 记一次.NET MAUI项目中绑定Android库实现硬件控制的开发经历
· 糊涂啊!这个需求居然没想到用时间轮来解决
· 浅谈为什么我讨厌分布式事务
阅读排行:
· 那些年我们一起追过的Java技术,现在真的别再追了!
· 还在手写JSON调教大模型?.NET 9有新玩法
· 为大模型 MCP Code Interpreter 而生:C# Runner 开源发布
· 面试时该如何做好自我介绍呢?附带介绍样板示例!!!
· JavaScript 编年史:探索前端界巨变的幕后推手
历史上的今天:
2017-09-24 LeetCode 646. Maximum Length of Pair Chain
2015-09-24 LeetCode 283. Move Zeroes
2015-09-24 LeetCode 45. Jump Game II
2015-09-24 LeetCode 55. Jump Game
2015-09-24 LeetCode 134. Gas Station
点击右上角即可分享
微信分享提示