lc 1199. Minimum Time to Build Blocks

简直精妙。

哈夫曼编码?

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

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

时间: 2024-08-30 18:35:08

lc 1199. Minimum Time to Build Blocks的相关文章

LC 963. Minimum Area Rectangle II

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes. If there isn't any rectangle, return 0. Example 1: Input: [[1,2],[2,1],[1,0],[0,1]]

[LC] 76. Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC" Note: If there is no such window in S

[LC] 452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

[LC] 1007. Minimum Domino Rotations For Equal Row

In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.) We may rotate the i-th domino, so that A[i] and B[i] swap values. Return th

LC 871. Minimum Number of Refueling Stops

A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0]miles east of the starting position, an

LC 599. Minimum Index Sum of Two Lists

题目描述 Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with the least list index sum. If there is a choice tie

[LC] 64. Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Example: Input: [   [1,3,1], [1,5

bc.34.B.Building Blocks(贪心)

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 751    Accepted Submission(s): 164 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build block

BC#34 1002 hdu 5192 Building Blocks

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 60    Accepted Submission(s): 6 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks.