-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-cost-tree-from-leaf-values.py
36 lines (30 loc) · 1.09 KB
/
minimum-cost-tree-from-leaf-values.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution(object):
def mctFromLeafValues(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
def getMaxIdx(arr):
maxi = 0
maxIdx = -1
for i in range(len(arr)):
if(arr[i] > maxi):
maxi = arr[i]
maxIdx = i
return (maxi, maxIdx)
def calculateAll(arr):
print(arr)
if(len(arr) == 1):
return (0, arr[0])
maxi = getMaxIdx(arr)
maxL = getMaxIdx(arr[: maxi[1]])
maxR = getMaxIdx(arr[maxi[1] + 1 :])
if(maxL[0] < maxR[0]):
left = calculateAll(arr[: maxi[1] + 1])
right = calculateAll(arr[maxi[1] + 1 :])
return (left[0] + right[0] + left[1] * right[1], max(left[1], right[1]))
else:
left = calculateAll(arr[: maxi[1]])
right = calculateAll(arr[maxi[1] :])
return (left[0] + right[0] + left[1] * right[1], max(left[1], right[1]))
return calculateAll(arr)[0]