[Swift]LeetCode1039. 多邊形三角剖分的最低得分 | Minimum Score Triangulation of Polygon

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xcltfkpq-mc.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in clockwise order.git

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

Return the smallest possible total score that you can achieve with some triangulation of the polygon.微信

Example 1:ide

Input: [1,2,3]
Output: 6 Explanation: The polygon is already triangulated, and the score of the only triangle is 6. 

Example 2:this

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:spa

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:code

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

給定 N,想象一個凸 N 邊多邊形,其頂點按順時針順序依次標記爲 A[0], A[i], ..., A[N-1]htm

假設您將多邊形剖分爲 N-2 個三角形。對於每一個三角形,該三角形的值是頂點標記的乘積,三角剖分的分數是進行三角剖分後全部 N-2 個三角形的值之和。blog

返回多邊形進行三角剖分後能夠獲得的最低分。

示例 1:

輸入:[1,2,3]
輸出:6
解釋:多邊形已經三角化,惟一三角形的分數爲 6。

示例 2:

輸入:[3,7,4,5]
輸出:144
解釋:有兩種三角剖分,可能得分分別爲:3*7*5 + 4*5*7 = 245,或 3*4*5 + 3*4*7 = 144。最低分數爲 144。

示例 3:

輸入:[1,3,1,4,1,5]
輸出:13
解釋:最低分數三角剖分的得分狀況爲 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13。

提示:

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

20ms
 1 class Solution {
 2     func minScoreTriangulation(_ A: [Int]) -> Int {
 3         var dp = Array(repeating: Array(repeating: 0, count: A.count), count: A.count + 1)
 4         for size in 3...A.count {
 5             for i in 0...A.count - size {
 6                 if size == 3 {
 7                     dp[size][i] = A[i] * A[i + 1] * A[i + 2]
 8                 } else {
 9                     var result = Int.max
10                     
11                     for other in (i + 1)..<(i + size - 1) {
12                         var this = A[i] * A[i + size - 1] * A[other]
13                         
14                         if other > i + 1 {
15                             this += dp[other - i + 1][i]
16                         }
17                         
18                         if other < i + size - 2 {
19                             this += dp[i + size - other][other]
20                         }
21                         
22                         result = min(result, this)
23                     }
24                     
25                     dp[size][i] = result
26                 }
27             }
28         }
29         
30         return dp[A.count][0]
31     }
32 }

Runtime: 36 ms

Memory Usage: 18.8 MB
 1 class Solution {
 2     func minScoreTriangulation(_ A: [Int]) -> Int {
 3         var n:Int = A.count
 4         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:n),count:n)
 5         for len in 2..<n
 6         {
 7             for i in 0..<(n - len)
 8             {
 9                 var j:Int = i + len
10                 dp[i][j] = Int.max
11                 for k in (i + 1)..<j
12                 {
13                     dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + A[i]*A[j]*A[k])
14                 }
15             }
16         }
17         return dp[0][n-1]        
18     }
19 }

44ms

 

 1 class Solution {
 2     func minScoreTriangulation(_ A: [Int]) -> Int {
 3         var memo = [[Int]](repeating: [Int](repeating: 0, count: A.count), count:A.count)
 4         return dfsHelper(A, 0, A.count - 1, &memo)
 5     }
 6     
 7     fileprivate func dfsHelper(_ A:[Int], _ i:Int, _ j: Int, _ memo: inout [[Int]]) -> Int {
 8         
 9         if j < i + 2 {
10             return 0 
11         }
12         if memo[i][j] != 0 {
13             return memo[i][j]
14         }
15         var result = Int.max
16         for k in stride(from: i+1, to: j, by: 1) {
17             result = min(result, dfsHelper(A, i, k, &memo) + dfsHelper(A, k, j, &memo) + A[i] * A[k] * A[j])
18         }
19         memo[i][j] = result
20         return memo[i][j]
21     }
22 }
相關文章
相關標籤/搜索