#华为826笔试第二题解法,采用分治法
def f(array1, array2):
if not array2:
return 0
if len(array2) == 1:
return array1[0] * array2[0]
minH = min(array2)
res = sum(array1) * minH
minHIdx = array2.index(minH)
return max(res, f(array1[: minHIdx], array2[: minHIdx]), f(array1[minHIdx + 1:], array2[minHIdx + 1:]))
def searchMaxS(tinput):
try:
nums = tinput.split('],[')
strB = nums[0].strip('[|]')
strH = nums[1].strip('[|]')
b = [int(n) for n in strB.split(',') if int(n) > 0]
h = [int(n) for n in strH.split(',') if int(n) > 0]
if len(b) == 1:
return b[0] * h[0]
minH = min(h)
res = sum(b) * minH
minHIdx = h.index(minH)
return max(res, f(b[: minHIdx], h[: minHIdx]), f(b[minHIdx + 1:], h[minHIdx + 1:]))
except:
return -1
tinput = '[[1,1,1,1,2,1,1],[5,2,5,4,5,1,6]]'
print(searchMaxS(tinput))