[Swift]LeetCode1130. 葉值的最小代價生成樹 | Minimum Cost Tree From Leaf Values

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

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

  • 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.git

Example 1:github

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

給你一個正整數數組 arr,考慮全部知足如下條件的二叉樹:微信

  • 每一個節點都有 0 個或是 2 個子節點。
  • 數組 arr 中的值與樹的中序遍歷中每一個葉節點的值一一對應。(知識回顧:若是一個節點有 0 個子節點,那麼該節點爲葉節點。)
  • 每一個非葉節點的值等於其左子樹和右子樹中葉節點的最大值的乘積。

在全部這樣的二叉樹中,返回每一個非葉節點的值的最小可能總和。這個和的值是一個 32 位整數。app

示例:less

輸入:arr = [6,2,4]
輸出:32
解釋:
有兩種可能的樹,第一種的非葉節點的總和爲 36,第二種非葉節點的總和爲 32。

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

提示:ide

  • 2 <= arr.length <= 40
  • 1 <= arr[i] <= 15
  • 答案保證是一個 32 位帶符號整數,即小於 2^31

12ms
 1 class Solution {
 2     func mctFromLeafValues(_ arr1: [Int]) -> Int {
 3         guard arr1.count > 0 else { return 0 }
 4         guard arr1.count > 1 else { return arr1[0] }
 5         
 6         var arr = [Int.max]
 7         arr.append(contentsOf: arr1)
 8         arr.append(Int.max)
 9         var sum = 0
10         while arr.count > 4 {
11             var mn = arr[1]
12             var idx = 1
13             for i in stride(from: 2, to: arr.count-1, by: 1) {
14                 if mn > arr[i] {
15                     mn = arr[i]
16                     idx = i
17                 }
18             }
19             var toMultiply = arr[idx-1] > arr[idx+1] ? arr[idx+1] : arr[idx-1]
20             sum += mn * toMultiply
21             arr.remove(at: idx)
22         }
23         
24         return sum + arr[1] * arr[2]
25     }
26 }

20msthis

 1 class Solution {
 2     func mctFromLeafValues(_ arr: [Int]) -> Int {
 3         var solution = 0
 4         var currentArray = arr
 5         
 6         while currentArray.count > 1 {
 7             let nextIndex = currentArray.firstIndex(of: currentArray.min()!)!
 8             
 9             if nextIndex > 0 && nextIndex < currentArray.count - 1 {
10                 solution += currentArray[nextIndex] * 
11                             min(currentArray[nextIndex-1], currentArray[nextIndex+1])
12             } else {
13                 solution += currentArray[nextIndex] * 
14                             (nextIndex == 0 ? currentArray[nextIndex+1] : currentArray[nextIndex-1])
15             }
16             
17             currentArray.remove(at: nextIndex)
18         }
19         
20         
21         return solution
22     }
23 }

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 }
相關文章
相關標籤/搜索