面试题 08.13. 堆箱子

题目

堆箱子。给你一堆n个箱子,箱子宽 wi、深 di、高 hi。箱子不能翻转,将箱子堆起来时,下面箱子的宽度、高度和深度必须大于上面的箱子。实现一种方法,搭出最高的一堆箱子。箱堆的高度为每个箱子高度的总和。
输入使用数组[wi, di, hi]表示每个箱子。

示例1:

 输入:box = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
 输出:6

示例2:

 输入:box = [[1, 1, 1], [2, 3, 4], [2, 6, 7], [3, 4, 5]]
 输出:10

分析: 动态规划的问题,最关键在于找到状态转移方程,对于此问题,最简单的暴力解法就是求出每一个箱子为底部时的最大叠加高度,以第n个箱子为底部, f ( n ) = m a x ( f ( i ) + h ( n ) ) f(n)=max(f(i)+h(n)) f(n)=max(f(i)+h(n))其中f(i)为除第n个箱子外其余的箱子为底部的最大叠加高度,且找到的第i个箱子需要满足宽、高、深的条件, 以此类推,分别求出从第一个到最后一个箱子为底的最大叠加高度,取最大值即可,不难看出,暴力解法存在重叠的子问题,f(i)可能会被计算多次,因此,通过创建一个数组记录已经算出来的f(i)的最大值,这样就能避免重复计算的问题。

方法1: 递归解法,不对箱子进行排序

class Solution:
    def pileBox(self, box: List[List[int]]) -> int:
        box_h = [0] * len(box)
        def dp(indx):
            if box_h[indx] != 0:
                return box_h[indx]
            b = box[indx]
            total_height = 0
            for i in range(len(box)):
                b2 = box[i]
                if b[0] > b2[0] and b[1] > b2[1] and b[2] > b2[2]:
                    total_height = max(total_height, b[2]+dp(i))
            # print(total_height)
            if total_height == 0:
                total_height = box[indx][2]
            box_h[indx] = total_height
            return total_height

        for i in range(len(box)):
            dp(i)
        return max(box_h)

方法2: 在方法1中,存在一个可以优化的点,当我们以第n个箱子为底步时,需要对剩下的每一个箱子与第n个箱子进行比较,判断是否符合条件,假如我们事先对箱子按照宽高深其中一个维度进行降序/升序排序,那么根据条件只需要与当前位置的后面/前面的箱子进行条件判断即可,节省大量比较时间

class Solution:
    def pileBox(self, box: List[List[int]]) -> int:
        box = sorted(box, key=lambda x:x[-1], reverse=True)
        box_h = [0] * len(box)
        def dp(indx):
            if box_h[indx] != 0:
                return box_h[indx]
            b = box[indx]
            total_height = b[2]
            for i in range(indx+1, len(box)):
                b2 = box[i]
                if b[0] > b2[0] and b[1] > b2[1] and b[2] > b2[2]:
                    total_height = max(total_height, b[2]+dp(i))
            box_h[indx] = total_height
            return total_height

        for i in range(len(box)):
            dp(i)
        return max(box_h)

方法3: 对箱子先排序,然后通过迭代法求解,动态规划问题常见的一种解法,自底向上的过程

class Solution:
    def pileBox(self, box: List[List[int]]) -> int:
        box = sorted(box, key=lambda x:x[-1])
        print(box)
        box_h = [0] * len(box)
        for i in range(len(box)):
            b = box[i]
            max_height = b[2]
            for j in range(i):
                b2 = box[j]
                if b[0] > b2[0] and b[1] > b2[1] and b[2] > b2[2]:
                    max_height = max(max_height, b[2] + box_h[j])
            box_h[i] = max_height
        return max(box_h)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值