堆叠盒9.10破解编码面试

时间:2015-03-03 22:41:10

标签: c++ dynamic-programming

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

你有一堆n个盒子,宽度为wi,高度为hi,深度为di。如果堆叠中的每个盒子的宽度,高度和深度都大于或等于其上方的盒子,则盒子不能旋转并且只能堆叠在一起。实现一种方法来构建最高的堆栈,其中堆栈的高度是每个盒子高度的总和。

解决方案的想法是我们尝试每个可能的框作为底部,因此解决方案是最大的(box1作为底部,box2作为底部,依此类推)。作者只有主要方法的代码,在给定给定底部

的情况下,我们获得最大堆栈
vector<Box> createStack(vector<Box> boxes, Box bottom, unordered_map<Box, vector<Box>, BoxHasher> map) 

我们是否需要添加另一种方法来为我们可用的所有方框调用上述方法?作者在解决方案中没有提到它。 我要添加的方法是

vector<Box> createStackMain(vector<Box> boxes)

我们为每个可能的框尝试主要方法

#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;
}

0 个答案:

没有答案