码迷,mamicode.com
首页 > 其他好文 > 详细

lc 1199. Minimum Time to Build Blocks

时间:2019-09-22 10:36:43      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:time   return   nim   turn   而且   solution   ==   class   pytho   

简直精妙。

哈夫曼编码?

我用的是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

 

lc 1199. Minimum Time to Build Blocks

标签:time   return   nim   turn   而且   solution   ==   class   pytho   

原文地址:https://www.cnblogs.com/waldenlake/p/11565771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!