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/

金融监管压力测试场景生成 压力测试是一种评估金融资产或机构在极端市场条件下的表现和风险承受能力的方法。它通过模拟各种不利的市场情景,测试金融机构在这些情景下的财务状况和运营能力。压力测试的作用在于帮助金融机构识别潜在风险,评估应对策略,并制定应急预案。总结压力测试场景生成系统的开发过程和实现效果,强调其在金融监管中的重要性和应用价值。随着金融市场的发展,实时压力测试将越来越重要。未来,压力测试需要更加注重实时性和动态性,及时反映市场的变化。本文详细介绍了金融监管压力测试的重要性、核心概念、算法原理、系统架构以及实战案例。 阅读详情

相关推荐

如何在STM32上5分钟搞定nanopb协议传输?附完整代码示例

本文提供了在STM32微控制器上快速集成nanopb协议传输的实战指南。通过详细的步骤,从.proto文件定义、代码生成到STM32CubeIDE项目移植,帮助开发者利用nanopb这一轻量级Protocol Buffers C语言实现,在5分钟内构建高效、跨平台的嵌入式设备通信框架,并附有完整的代码示例。

jwt8token的博客 839

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

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

未见花闻的博客 1090

DeepSpeed Zero3 Offload实战:从GCC版本检查到环境配置的完整避坑指南

本文是一份针对DeepSpeed Zero3 Offload部署的完整避坑指南。针对训练中常见的`AttributeError: ‘DeepSpeedCPUAdam‘ object has no attribute ‘ds_opt_adam`报错,文章深入剖析了其根源在于GCC编译器版本过低。指南提供了从系统GCC版本深度探查、安全启用高版本GCC环境,到DeepSpeed一致性安装验证的全链路实战方案,旨在帮助用户构建稳定的大模型训练环境。

weixin_29070721的博客 474

动态规划问题之背包模型(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

西门子博图指令(定时器操作三)

西门子博图定时器中复位定时器功能指令介绍

yue008的博客 8873

微信小程序后台定位与实时位置更新的实战指南

本文提供了微信小程序实现后台定位与实时位置更新的完整实战指南。针对小程序退到后台后位置更新中断的痛点,详细解析了基于`startLocationUpdateBackground`API的后台定位解决方案。内容涵盖从隐私协议配置、项目权限声明,到授权、启动、监听、停止的核心代码流程,并分享了真机调试、性能优化及权限引导等关键避坑经验,帮助开发者构建稳定可靠的后台位置服务。

weixin_29008055的博客 468

[DP] 堆盒子问题

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

weixin_30396699的博客 250

高速PCB差分对布线仿真实战案例

差分对布线是高速PCB设计的关键环节,直接影响信号完整性与EMI性能。本文结合实际项目,详解等长控制、阻抗匹配、间距优化等核心技巧,并通过仿真工具验证眼图、串扰和时序裕量,助力工程师规避常见高速pcb设计陷阱。

weixin_29092787的博客 349

叠罗汉III之推箱子

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

weixin_34293246的博客 361

优快云专栏限时活动!福利公告必读!

本人优快云付费专栏周排名27名专业博主,每周文章平均阅读量约7w以上!

FJN110的博客 3153

AutoSAR从0开始实战 --- 目录

​​​​​​本专栏是全网最真实的AutoSAR实战专栏,博主将从一个0基础小白的身份来讲解AutoSAR工具的配置,以及相应代码的讲解。

wj博客 1728

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

Transformer实战——Transformer自编码模型

自编码模型依赖于原始 Transformer 编码器,并且在解决分类问题时非常高效。除了 BERT 外,还有许多流行的自编码模型,在本节中,我们将一些除 BERT 外的自编码模型,这些模型的目标是通过优化预训练过程、增加层数或头数、改善数据质量、设计更好的目标函数等,来在下游任务上取得更好的表现。这些改进大致可以分为两个方面,更好的架构设计选择和预训练控制。

盼小辉丶的博客 9319

堆箱子问题

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

jinghe的知识屋 514

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

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

huanghuchuan 679

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

原文地址: 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、付费专栏及课程。

余额充值