The minimum cost spanning tree leaves LeetCode1130 value [Swift] |. Minimum Cost Tree From Leaf Values

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: Shan Wing Chi ( shanqingyongzhi)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: // the WWW. cnblogs.com/strengthen/p/11223721.html 
➤ If the link is not the blog Chi Wing Shan Park address, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given an array arr of positive integers, consider all binary trees such that:

  • Each node has either 0 or 2 children;
  • The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.)
  • The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively.

Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.

Example 1:

Input: arr = [6,2,4]
Output: 32
Explanation:
There are two possible trees.  The first has non-leaf node sum 36, and the second has non-leaf node sum 32.

    24            24
   /  \          /  \
  12   4        6    8
 /  \               / \
6    2             2   4

Constraints:

  • 2 <= arr.length <= 40
  • 1 <= arr[i] <= 15
  • It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).

To give you a positive integer array  arr, consider all the following conditions binary tree:

  • Each node has child nodes 0 or 2.
  • Array  arr inorder traversal of the tree in the median value of each leaf node correspondence. (Knowledge Review: 0 if a node has child nodes, then the node is a leaf node.)
  • Value of each non-leaf node is equal to the product of the maximum value of its left subtree right subtree and a leaf node.

In all such binary tree, returns the minimum possible value of the sum of each non-leaf node. And this value is a 32-bit integer.

Example:

Input: arr = [6,2,4] 
Output: 32 
Explanation: 
There are two possible tree, the sum of the first non-leaf node is 36, the sum of the second non-leaf nodes 32. 

    24 24 
   / \ / \ 
  12468 
 / \ / \ 
6224

prompt:

  • 2 <= arr.length <= 40
  • 1 <= arr[i] <= 15
  • The answer is to ensure a 32-bit signed integer, i.e., less than  2^31.

Runtime: 36 ms
Memory Usage: 20.9 MB
 1 class Solution {
 2     func mctFromLeafValues(_ arr: [Int]) -> Int {
 3         var dp:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:45),count:45)
 4         var maxn:[[Int]] = [[Int]](repeating:[Int](repeating:0,count:45),count:45)
 5         var n:Int = arr.count
 6         for i in 0..<n
 7         {
 8             maxn[i][i] = arr[i]
 9             for j in (i + 1)..<n
10             {
11                  maxn[i][j] = max(maxn[i][j - 1], arr[j])
12             }
13         }
14         for d in 2...n
15         {
16             var i:Int = 0
17             while(i + d - 1 < n)
18             {
19                 var j:Int = i + d - 1
20                 dp[i][j] = (1<<60)
21                 for k in i..<j
22                 {
23                     dp[i][j] = min(dp[i][j],maxn[i][k] * maxn[k+1][j] + dp[i][k] + dp[k + 1][j])
24                 }
25                 i += 1
26             }
27         }
28         return (dp[0][n - 1])
29     }
30 }

 

Guess you like

Origin www.cnblogs.com/strengthen/p/11223721.html