DP22 堆盒子问题 Box Stacking Problem @geeksforgeeks

【微软算法面试高频题】堆箱子 微软和谷歌的几个大佬组织了一个面试刷题群,可以加管理员VX:sxxzs3998(备注优快云),进群参与讨论和直播 1. 题目 给你一堆n个箱子,箱子宽 wiw_iwi​、深 did_idi​、高 hih_ihi​。箱子不能翻转,将箱子堆起来时,下面箱子的宽度、高度和深度必须大于上面的箱子。实现一种方法,搭出最高的一堆箱子。箱堆的高度为每个箱子高度的总和。 输入使用数组[wi,di,hi][w_i, d_i, h_i][wi​,di​,hi​]表示每个箱子。 示例1: 输入:box = [[1, 1, 1 阅读详情

You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numbers). You want to create a stack of boxes which is as tall as possible, but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box so that any side functions as its base. It is also allowable to use multiple instances of the same type of box.

Source: http://people.csail.mit.edu/bdean/6.046/dp/. The link also has video for explanation of solution.

The Box Stacking problem is a variation of LIS problem. We need to build a maximum height stack.

Following are the key points to note in the problem statement:
1) A box can be placed on top of another box only if both width and depth of the upper placed box are smaller than width and depth of the lower box respectively.
2) We can rotate boxes. For example, if there is a box with dimensions {1x2x3} where 1 is height, 2×3 is base, then there can be three possibilities, {1x2x3}, {2x1x3} and {3x1x2}.
3) We can use multiple instances of boxes. What it means is, we can have two different rotations of a box as part of our maximum height stack.

Following is the solution based on DP solution of LIS problem.

1) Generate all 3 rotations of all boxes. The size of rotation array becomes 3 times the size of original array. For simplicity, we consider depth as always smaller than or equal to width.

2) Sort the above generated 3n boxes in decreasing order of base area.

3) After sorting the boxes, the problem is same as LIS with following optimal substructure property.
MSH(i) = Maximum possible Stack Height with box i at top of stack
MSH(i) = { Max ( MSH(j) ) + height(i) } where j < i and width(j) > width(i) and depth(j) > depth(i).
If there is no such j then MSH(i) = height(i)

4) To get overall maximum height, we return max(MSH(i)) where 0 < i < n


3维的就确定下两维(底面积),根据底面积排序,然后找高度的LDS

package DP;

import java.util.Arrays;
import java.util.Comparator;

public class BoxStacking {

	public static void main(String[] args) {
		Box[] A = new Box[4];
		A[0] = new Box(4,6,7);
		A[1] = new Box(1,2,3);
		A[2] = new Box(4,5,6);
		A[3] = new Box(10,12,32);
		int n = A.length;
		System.out.println(maxStackHeight(A, n));
	}	
	
	// Time Complexity: O(n^2) Auxiliary Space: O(n)
	public static int maxStackHeight(Box[] A, int n){
		/* Create an array of all rotations of given boxes
	      For example, for a box {1, 2, 3}, we consider three
	      instances{{1, 2, 3}, {2, 1, 3}, {3, 1, 2}} */
		Box[] rot = new Box[3*n];
		int index = 0;
		for(int i=0; i<n; i++){
			rot[index] = A[i];		// Copy the original box
			index++;
			
			rot[index] = new Box();		// First rotation of box
			rot[index].h = A[i].w;
			rot[index].d = Math.max(A[i].h, A[i].d);
			rot[index].w = Math.min(A[i].h, A[i].d);
			index++;
			
			rot[index] = new Box();		// Second rotation of box
			rot[index].h = A[i].d;
			rot[index].d = Math.max(A[i].h, A[i].w);
			rot[index].w = Math.min(A[i].h, A[i].w);
			index++;
		}
		
		n = 3*n;		// Now the number of boxes is 3n
		
		/* Sort the array ‘rot[]‘ in decreasing order, using library
	      function for quick sort */
		Arrays.sort(rot, new Comparator<Box>() {
			@Override
			public int compare(Box o1, Box o2) {		// 按底面积递减排序
				return (o2.d*o2.w) - (o1.d*o1.w);
			}
		});
		
		/* Initialize msh values for all indexes 
	      msh[i] –> Maximum possible Stack Height with box i on top */
		int[] msh = new int[n];
		for(int i=0; i<n; i++){
			msh[i] = rot[i].h;
		}
		
		/* Compute optimized msh values in bottom up manner */
		for(int i=1; i<n; i++){
			for(int j=0; j<i; j++){
				if(rot[i].w<rot[j].w && rot[i].d<rot[j].d){
					msh[i] = Math.max(msh[i], msh[j]+rot[i].h);
				}
			}
		}
		
		/* Pick maximum of all msh values */
		int max = 0;
		for(int i=0; i<n; i++){
			max = Math.max(max, msh[i]);
		}
		return max;
	}

	public static class Box{
		int h, w, d;		// h –> height, w –> width, d –> depth
		// for simplicity of solution, always keep w <= d
		public Box(){
		}
		public Box(int h_, int w_, int d_){
			h = h_;
			w = w_;
			d = d_;
		}
	}
}

http://www.geeksforgeeks.org/dynamic-programming-set-21-box-stacking-problem/

http://people.csail.mit.edu/bdean/6.046/dp/

AI视频生成本地搭建图文详细过程 在自己的电脑上搭建一个AI,主要用于通过自己输入的关键字来自动生成视频文案,把视频文案自动生成视频,再自动给视频配音配字幕。 阅读详情

相关推荐

ROS2 Control实战:多机器人仿真与控制器管理

本文详细介绍了ROS2 Control在多机器人仿真中的应用,涵盖命名空间配置、控制器管理及性能优化。通过Gazebo仿真环境和ROS2 Control框架,开发者可以高效管理多个机器人的硬件抽象和控制器,实现资源隔离与平滑切换。文章还提供了常见问题排查和调试工具推荐,助力机器人开发实践。

u9v0w的博客 203

【贪心数学困难题】1739. 放置盒子

想要占地板数最少,那么需要靠墙角进行方块的放置,因为墙角天然提供了两个侧面的消耗,这是这道题贪心的一个点吧,并且以类阶梯放置时,所占地面的方块数最少,下面我们来进行找规律。

未见花闻的博客 1090

Star CCM+中的物性参数(定值、多项式、查表)

仿真过程中我们一定会遇到设置物性参数的问题,对于固定的物性参数还好说,直接输入一个固定的值即可。但是对于冷却液、气体、两相流等,物性参数是随着温度或温度和压力变化的。此时就涉及到多项式和查表。首先创建物理连续体,选择计算模型。

2201_75375391的博客 2957

动态规划问题之背包模型(18题)

背包问题是动态规划问题的一大类型,下面我们对这个进行总结。 以 Acwing y中总结的 几个类型,我写了几个题解 应用知识点 基础的 01 背包 : 完全背包 : 一、采药 Acwing 题目链接 #include <bits/stdc++.h> using namespace std; const int N = 1010; int f[N][N]; int n, m; int main() { cin >> m >> n; memset

weixin_45573921的博客 658

Android面试指南及参考答案

【摘要】LesPark招聘Android开发工程师,要求3年以上经验,负责客户端功能开发与优化。岗位需掌握Java、多线程、网络编程(TCP/UDP/HTTP)及性能调试工具。面试重点考察:1)核心技术如网络协议选择(TCP可靠消息/UDP实时音视频)、内存泄漏分析;2)业务能力如图片懒加载三级缓存设计、启动速度优化案例;3)高阶系统原理如插件化框架设计;4)行为问题应对策略。建议候选人准备并发编程、JVM差异等深度知识,并量化项目优化成果,尤其关注社交应用相关的图片处理、实时通信等场景题。

郑伟强dev的专栏 804

软件测试面试题之易错题

(1)白盒测试:又称功能功能 测试、数据驱动测试和行为测试,是一种从用户观点出发的测试,被测程序为黑盒子, 通过测试来检测每个功能是否能够正常使用。(2)黑盒测试:又称结构测试和逻辑驱动 测试,是知道产品内部工作过程,通过测试来检测产品内部动作是否按照规格说明书 的规定去正常运行。(1)、测试需求分析;(2)动态白盒测试:需要对各个模块功能、模块间的接口、局部数据结构、主要执行路径、 错误处理等方面的测试。(2) 集成测试所测试的对象是模块间的接口,其目的是在找出在模块接口上面,包括整体体系结构上的问题;

m0_37449634的博客 679

[DP] 堆盒子问题

给一堆盒子,知道每个盒子的三围(长宽高),盒子正面朝你,不能旋转摆放,按照大的放在小的下面的原则堆起来,必须是 strictly larger,同样大小的盒子不行,问怎么样堆到最大的高度? 思路:动态规划 最优解一定是 max( {box_1 be the bottom}, {box_2 be the bottom}, ... , {box_n be the bottom} ),所以我们遍历...

weixin_30396699的博客 250

YOLOv9改进策略【Neck】| GSConv+Slim Neck:混合深度可分离卷积和标准卷积的轻量化网络设计

实时检测器架构的轻量级设计GsConv。

Limiiiing的博客 1364

叠罗汉III之推箱子

有一堆箱子,每个箱子宽为wi,长为di,高为hi,现在需要将箱子都堆起来,而且为了使堆起来的箱子不到,上面的箱子的宽度和长度必须小于下面的箱子。请实现一个方法,求出能堆出的最高的高度,这里的高度即堆起来的所有箱子的高度之和。 给定三个int数组w,l,h,分别表示每个箱子宽、长和高,同时给定箱子的数目n。请返回能堆成的最高的高度。保证n小于等于500。 测试样例: [1,1,1],[1,1,1],...

weixin_34293246的博客 361

音乐生成 - 使用LSTM或Transformer模型进行音乐生成,如MIDI音乐生成。

在本篇博客中,我们将介绍如何使用LSTM和Transformer模型生成MIDI音乐。我们将首先概述音乐生成的一般流程,然后深入了解LSTM和Transformer模型的特点和实现。最后,我们将通过Python代码演示如何使用这两种模型生成MIDI音乐。

斌擎科技 1720

沁恒CH32V30X学习笔记02--GPIO的使用教程及2次封装驱动

刚复位后,GPIO 口运行在初始状态,这时大多数 IO 口都是运行在状态所有的 GPIO 口都可以被配置外部中断输入通道,但一个外部中断输入通道最多只能映射到一个GPIO 引脚上,且外部中断通道的序号必须和 GPIO 端口的位号一致,,两方都是一对一的关系。复用功能。

Car12 706

LeetCode题解(面试08.13):堆箱子问题(Python)

题目:原题链接(困难) 标签:动态规划、回溯算法 解法 时间复杂度 空间复杂度 执行用时 Ans 1 (Python) O(N2)O(N^2)O(N2) O(N)O(N)O(N) 840ms (78.50%) Ans 2 (Python) Ans 3 (Python) 解法一(动态规划): class Solution: def pileBox(self, box: List[List[int]]) -> int: # 依据长宽高总和排序

长行 1381

YOLOv8+BiFPN双向特征金字塔:多尺度目标检测精度提升28%的实战教程

双向特征金字塔网络(BiFPN)是一种强化多尺度特征融合支持“自顶向下+自底向上”的双向特征流动,同时保留不同尺度的细节信息;引入加权融合机制,自动学习不同特征的重要性权重;裁剪冗余节点,在轻量化的同时提升融合效率。

m0_64661770的博客 492

堆箱子问题

思路:动态规划 先将数组按照任何一条边降序重排,目的是降低里层循环次数。 使用dp一维数组,记录以序号i箱子为项时的最大高度。 计算每个箱子i时,在约束条件下,找到所有箱子k,并计算以k为顶最大高度与i的高度之和,取最大值。 所有箱子都操作完成后,取dp数组元素最大值 class Solution { public: // 降序 static bool width_bigger( const vector<int> &b1, const vector<int>

jinghe的知识屋 514

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Geeks 面试题: Box Stacking Problem

Dynamic Programming | Set 22 (Box Stacking Problem) You are given a set of n types of rectangular 3-D boxes, where the i^th box has height h(i), width w(i) and depth d(i) (all real numb...

编程语言小筑 340

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【解题报告】uva103_Stacking Boxes(堆砌盒子, dp)

题目大意: 输入n,代表n个事件。然后输入n个正整数ci,代表第i个事件发生的年份为第ci年。接下来输入多组学生的回答,每组n个正整数ri,同样代表第i个事件发生的年味为第ri年。得分规则为:学生的回答与答案中事件按时间先后排序的序列的相对位置相同时(不需连续),每个事件加1分。求每个学生能得多少分。

huanghuchuan 679

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

今日头条屏幕适配方案终极版正式发布!

原文地址: juejin.im/post/5bce68… 以下是 骚年你的屏幕适配方式该升级了! 系列文章,欢迎转发以及分享: 骚年你的屏幕适配方式该升级了!(一)-今日头条适配方案 骚年你的屏幕适配方式该升级了!(二)-smallestWidth 限定符适配方案 今日头条屏幕适配方案终极版正式发布! 扫描或点击以下二维码,加入技术交流 QQ 群 455850365 前言 我在前面两篇文章中...

weixin_33968104的博客 371

程序员面试金典 - 面试题 08.13. 堆箱子(DP)

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

Michael是个半路程序员 2029

Codeforces 50A(往矩形里填多米诺骨牌)

A. Domino piling time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rectangular board of M × N

coco_astrids的博客 1306

动态规划 (Dynamic Programming) 之 最长递增子序列(Longest Increase Subsequence)

既然已经说到了最长公共子序列,就把这个递增子序列也说了。同样的,这里subsequence表明了这样的子序列不要求是连续的。比如说有子序列{1, 9, 3, 8, 11, 4, 5, 6, 4, 19, 7, 1, 7 }这样一个字符串的的最长递增子序列就是{1,3,4,5,6,7}或者{1,3,4,5,6,19}。 其实这个问题和前面的最长公共子序列问题还是有一定的关联的。假设我们的初始的

meiyouwanzimian的专栏 527

[动态规划]UVA103 - Stacking Boxes

题意: 在数学或电脑科学里,有些概念在一维或二维时还蛮简单的,但到N维就会显得非常复杂。试想一个 n维的「盒子」:在二维空间里,盒子( 2 , 3 )可代表一个长为2个单位,宽为3个单位的盒子;在三维空间里,盒子( 4 , 8 , 9 )则是一个4*8*9(长、宽、高)的盒子。至于在六维空间里,也许我们不清楚( 4 , 5 , 6 , 7 , 8 , 9 )长得怎样,不过我们还是可以分析这些盒子的特性。 在此问题里,我们要算出一组n维盒子里,它们的「最长套入串列」: b 1 , b 2 , ......,b

九问的烦恼 1812

DP21 LIS的变种问题-建桥问题 Variations of LIS-Building Bridge @geeksforgeeks

We have discussed Dynamic Programming solution for Longest Increasing Subsequence problem inthispost and a O(nLogn) solution inthispost.Following are commonly asked variations of the standardLIS ...

系统 运维 245

The Blocks Problem

The Blocks Problem Description Background  Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and

baidu_22079207的博客 340

HDU-小盒子放在格子里-可以旋转

问题及代码: /* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:objection.cpp *作 者:单昕昕 *完成日期:2015年2月3日 *版 本 号:v1.0 * *问题描述:There is a large room in the Pyramid called Room-of-No-Re

Some. 913
上一篇: DP21 LIS的变种问题-建桥问题 Variations of LIS-Building Bridge @geeksforgeeks
下一篇: DP23 Bellman–Ford Algorithm @geeksforgeeks
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值