简直精妙。
哈夫曼编码?
我用的是dp,这种区间dp的时间复杂度是真的难算!状态转移方程为n的区间dp时间都算作n^3吧。
先把任务从长到短排序,然后看worker在那一层要细分多少?就是位置i和员工数n的dp转移。
但是可以贪心!!!!!!!!!!!!每次都是把时间最短的放在最后,而且这两个必然同父,合体后与其他点没有任何差异,继续找最短的合体。
dp代码(python过不了,c++可以):
class Solution:
def minBuildTime(self, ns, l) -> int:
dp={}
ns.sort(reverse=True)
mx=l*len(ns)*ns[0]
def get(i,n):
if i==len(ns):
return 0
if n==0:
return mx
lst = len(ns) - i
if n >= lst:
return ns[i]
if (i,n) in dp:
return dp[(i,n)]
ans=max(ns[i],get(i+1,n-1))
ans=min(ans,l+get(i,n*2))
dp[(i,n)]=ans
return ans
# for i in reversed(range(len(ns))):
# lst=len(ns)-i
# for j in range(1,lst+1):
# get(i,j)
x=get(0,1)
return x