2020年2月21日 星期五

[LeetCode] Medium - 1130. Minimum Cost Tree From Leaf Values

Source From Here
Question
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:
  1. Input: arr = [6,2,4]  
  2. Output: 32  
  3. Explanation:  
  4. There are two possible trees.  The first has non-leaf node sum 36, and the second has non-leaf node sum 32.  
  5.   
  6.     24            24  
  7.    /  \          /  \  
  8.   12   4        6    8  
  9. /  \               / \  
  10. 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).

Solution
Naive Solution
  1. #!/usr/bin/env python  
  2. import sys  
  3.   
  4. class Solution:  
  5.     def mctFromLeafValues(self, arr):  
  6.         sum_of_non_leaf = 0  
  7.   
  8.         print("### arr={}".format(arr))  
  9.         def look_4_min(arr):  
  10.             mi = 0  
  11.             mv = arr[0] * arr[1]  
  12.             for i in range(1, len(arr)-1):  
  13.                 v = arr[i] * arr[i+1]  
  14.                 if v < mv:  
  15.                     mi = i  
  16.                     mv = v  
  17.   
  18.             rst = (mi, max(arr[mi], arr[mi+1]), mv)  
  19.             print('\t{}'.format(rst))  
  20.             return rst  
  21.   
  22.         while len(arr) > 2:  
  23.             print("{}: {}".format(arr, sum_of_non_leaf))  
  24.             mi, mx, mv = look_4_min(arr)  
  25.             sum_of_non_leaf += mv  
  26.             arr[mi:mi+2] = [mx]  
  27.   
  28.         print("{}: {}".format(arr, sum_of_non_leaf))  
  29.         sum_of_non_leaf += arr[0] * arr[1]  
  30.   
  31.         print("[]: {}".format(sum_of_non_leaf))  
  32.         return sum_of_non_leaf  
  33.   
  34. if __name__ == '__main__':  
  35.     s = Solution()  
  36.     for arr, aw in [  
  37.                 ([6,2,4], 32),  
  38.                 ([15,13,5,3,15], 500),  
  39.                 ([1,2,3,4], 20),  
  40.                 ([4,3,2,1], 20),  
  41.                 ([1,3,4,2], 23),  
  42.                 ([4,2,3,1], 21)  
  43.                ]:  
  44.         rel = s.mctFromLeafValues(arr)  
  45.         assert aw == rel  
  46.         print("")  
Execution result:
### arr=[6, 2, 4]
[6, 2, 4]: 0
(1, 4, 8)
[6, 4]: 8
[]: 32

### arr=[15, 13, 5, 3, 15]
[15, 13, 5, 3, 15]: 0
(2, 5, 15)
[15, 13, 5, 15]: 15
(1, 13, 65)
[15, 13, 15]: 80
(0, 15, 195)
[15, 15]: 275
[]: 500
...


Supplement
米开:LeetCode 1130. Minimum Cost Tree From Leaf Values

1 則留言:

  1. Water Hack Burns 2 lb of Fat OVERNIGHT

    Over 160 thousand men and women are trying a easy and SECRET "liquid hack" to lose 2lbs each and every night as they sleep.

    It's proven and works with everybody.

    This is how to do it yourself:

    1) Hold a drinking glass and fill it with water half glass

    2) And then follow this strange hack

    and be 2lbs skinnier as soon as tomorrow!

    回覆刪除

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...