C++ 堆叠框9.10在破解编码面试中

C++ 堆叠框9.10在破解编码面试中,c++,dynamic-programming,C++,Dynamic Programming,我在这个问题上花了两个小时 您有一个n个框的堆栈,宽度为wi,高度为hi,深度为di。如果堆叠中的每个盒子在宽度、高度和深度上大于或等于其上方的盒子,则这些盒子不能旋转,只能堆叠在另一个盒子的顶部。实现一种方法来构建可能最高的堆栈,其中堆栈的高度是每个框的高度之和 解决方案的想法是,我们尝试将每个可能的框作为底部,因此解决方案的最大值为(框1作为底部,框2作为底部,依此类推)。作者有一个主要方法的代码,在这个方法中,我们得到给定底部的最大堆栈 vector<Box> createSt

我在这个问题上花了两个小时

您有一个n个框的堆栈,宽度为wi,高度为hi,深度为di。如果堆叠中的每个盒子在宽度、高度和深度上大于或等于其上方的盒子,则这些盒子不能旋转,只能堆叠在另一个盒子的顶部。实现一种方法来构建可能最高的堆栈,其中堆栈的高度是每个框的高度之和

解决方案的想法是,我们尝试将每个可能的框作为底部,因此解决方案的最大值为(框1作为底部,框2作为底部,依此类推)。作者有一个主要方法的代码,在这个方法中,我们得到给定底部的最大堆栈

vector<Box> createStack(vector<Box> boxes, Box bottom, unordered_map<Box, vector<Box>, BoxHasher> map) 
vector createStack(向量框、框底、无序映射)
我们不需要添加另一个方法,在该方法中,我们为所有可用的框调用上述方法吗?作者在解决方案中没有提到这一点。 我想添加的方法是

vector<Box> createStackMain(vector<Box> boxes)
vector createStackMain(向量框)
在这里,我们尝试每个可能的框的主要方法

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std; // because this is an example

class Box {
public:
    int d;
    int w;
    int h;
    Box(int d, int w, int h) {
        this->d = d;
        this->w = w;
        this->h = h;
    }
    bool canBeAbove(Box *other) {
        printf("comparing %d %d %d to %d %d %d\n", d, w, h, other->d, other->w, other->h);
        return (d < other->d && w < other->w && h < other->h);
    }
    bool operator==(const Box &other) const {
        return (h == other.h && d == other.d && w == other.w);
    }
};

struct BoxHasher {
    std::size_t operator()(const Box& k) const {
        std::hash<std::string> hashFuction;
        return ((hashFuction(to_string(k.h)) ^ (hashFuction(to_string(k.d)) ^ (hashFuction(to_string(k.w)) << 1)) >> 1));
    }
};

int stackHeight(vector<Box> stack) {
    int height = 0;
    for (int i = 0; i < stack.size(); i++) {
        height += stack[i].h;
    }
    return height;
}

vector<Box> createStack(vector<Box> boxes, Box bottom, unordered_map<Box, vector<Box>, BoxHasher> map) {
    if (map.count(bottom) > 0) {
        return map[bottom];
    }
    int max_height = 0;
    vector<Box> max_stack;

    for (int i = 0; i < boxes.size(); i++) {
        if (boxes[i].canBeAbove(&bottom)) {
            printf("yes\n");
            std::vector<Box> new_stack = createStack(boxes, boxes[i], map);
            int new_height = stackHeight(new_stack);
            if (new_height > max_height) {
                max_height = new_height;
                max_stack = new_stack;
            }
        }
    }
    if (max_stack.size() == 0) {
        max_stack = std::vector<Box>();
    }
    max_stack.push_back(bottom);
    printf("pushing to the max stack %d\n", bottom.h);
    std::reverse(max_stack.begin(), max_stack.end());
    map[bottom] = max_stack;
    return max_stack;
}

// missing method MAX = MAX(BOX1 as a bottom, BOX2 as a bottom and so on)
vector<Box> createStackMain(vector<Box> boxes) {
    vector<Box> max_stack;
    int max_height = 0;
    unordered_map<Box, vector<Box>, BoxHasher> map;
    for (int i = 0; i < boxes.size(); i++) {
        vector<Box> new_stack = createStack(boxes, boxes[i], map);
        int new_height = stackHeight(new_stack);
        if (new_height > max_height) {
            max_height = new_height;
            max_stack = new_stack;
        }
    }
    return max_stack;
}

int main() {
    vector<Box> boxes;
    boxes.push_back(Box(1, 1, 3));
    boxes.push_back(Box(2, 1, 4));
    boxes.push_back(Box(4, 2, 1));
    boxes.push_back(Box(6, 7, 7));
    vector<Box> max = createStackMain(boxes);
    printf("%d\n", stackHeight(max));
    return 0;
}
#包括
#包括
#包括
使用命名空间std;//因为这是一个例子
类框{
公众:
int d;
int w;
int-h;
方框(整数d、整数w、整数h){
这个->d=d;
这->w=w;
这->h=h;
}
bool Canbeave(方框*其他){
printf(“将%d%d%d与%d%d%d\n进行比较”,d,w,h,other->d,other->w,other->h);
返回(dd&ww&hh);
}
布尔运算符==(常量框和其他)常量{
返回(h==other.h&&d==other.d&&w==other.w);
}
};
结构BoxHasher{
std::size\u t运算符()(常数框和k)常数{
散列函数;
返回((hashFuction(to_string(k.h))^(hashFuction(to_string(k.d))^(hashFuction(to_string(k.w))>1);
}
};
整数堆栈高度(向量堆栈){
整数高度=0;
对于(int i=0;i0){
返回图[底部];
}
int max_height=0;
向量max_叠加;
对于(int i=0;i最大高度){
最大高度=新高度;
最大_堆栈=新_堆栈;
}
}
}
if(max_stack.size()=0){
max_stack=std::vector();
}
最大堆叠。向后推(底部);
printf(“推到最大堆栈%d\n”,底部.h);
std::reverse(max_stack.begin(),max_stack.end());
map[底部]=最大堆栈;
返回max_堆栈;
}
//缺少方法MAX=MAX(框1作为底部,框2作为底部,依此类推)
向量createStackMain(向量框){
向量max_叠加;
int max_height=0;
无序地图;
对于(int i=0;i最大高度){
最大高度=新高度;
最大_堆栈=新_堆栈;
}
}
返回max_堆栈;
}
int main(){
向量盒;
盒子。推回(盒子(1,1,3));
盒子。推回(盒子(2,1,4));
盒子。推回(盒子(4,2,1));
盒子。推回(盒子(6,7,7));
向量最大值=createStackMain(框);
printf(“%d\n”,堆栈高度(最大));
返回0;
}

看起来贪婪的方法是可行的。@mhcuervo如上所述,没有一个盒子可以放在零高度的盒子上。@TavianBarnes你是说目前最高的盒子可以工作吗?那不行——有一堆几乎相同大小的盒子,还有一个盒子又窄又高。窄/高的盒子被贪婪地挑选出来,但另一个盒子盒子会是更好的选择。@Yakk不,选择最大化
min(宽度、高度、深度)
的盒子,然后问这个问题,你不能根据体积对盒子进行排序,这就是你的堆栈吗?