[Swift]LeetCode1130. 叶值的最小代价生成树 | Minimum Cost Tree From Leaf Values

[Swift]LeetCode1130. 叶值的最小代价生成树 | Minimum Cost Tree From Leaf Values,第1张

概述★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ?微信公众号:山青咏芝(shanqingyongzhi) ?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/) ?GitHub地址:https://github.com/strengthen/LeetCode ?原文地址:https://www.cnblogs.com/st

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公众号:山青咏芝(shanqingyongzhi)
?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11223721.html 
?如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
?原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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: 32Explanation: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).

给你一个正整数数组 arr,考虑所有满足以下条件的二叉树:

每个节点都有 0 个或是 2 个子节点。 数组 arr 中的值与树的中序遍历中每个叶节点的值一一对应。(知识回顾:如果一个节点有 0 个子节点,那么该节点为叶节点。) 每个非叶节点的值等于其左子树和右子树中叶节点的最大值的乘积。

在所有这样的二叉树中,返回每个非叶节点的值的最小可能总和。这个和的值是一个 32 位整数。

示例:

输入:arr = [6,4]输出:32解释:有两种可能的树,第一种的非叶节点的总和为 36,第二种非叶节点的总和为 32。    24            24   /  \          /    12   4        6    8 /  \               / 6    2             2   4

提示:

2 <= arr.length <= 40 1 <= arr[i] <= 15 答案保证是一个 32 位带符号整数,即小于 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) 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)..<n10             {11                  maxn[i][j] = max(maxn[i][j - 1],arr[j])12             }13         }14         for d in 2...n15         {16             var i:Int = 017             while(i + d - 1 < n)18             {19                 var j:Int = i + d - 120                 dp[i][j] = (1<<60)21                 for k in i..<j22                 {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 += 126             }27         }28         return (dp[0][n - 1])29     }30 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode1130. 叶值的最小代价生成树 | Minimum Cost Tree From Leaf Values全部内容,希望文章能够帮你解决[Swift]LeetCode1130. 叶值的最小代价生成树 | Minimum Cost Tree From Leaf Values所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/999924.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存