示例#1
0
 internal void Merge(StackOfBoxes other)
 {
     foreach (var box in other.boxes)
     {
         AddTop(box);
     }
 }
示例#2
0
        private StackOfBoxes BuildMaxHeightStackRecurse(
            List <Box> boxes, int index, Dictionary <int, StackOfBoxes> maxStackMap)
        {
            if (maxStackMap.ContainsKey(index))
            {
                return(maxStackMap[index]);
            }

            StackOfBoxes stackWithBottomIndex = new StackOfBoxes();

            stackWithBottomIndex.AddBottom(boxes[index]);

            StackOfBoxes tallestStackWithBottomIndex = stackWithBottomIndex.Clone();

            for (int i = index + 1; i < boxes.Count; i++)
            {
                var currentStack = stackWithBottomIndex.Clone();
                if (boxes[i] < boxes[index])
                {
                    var aboveStack = BuildMaxHeightStackRecurse(boxes, i, maxStackMap);
                    currentStack.Merge(aboveStack);


                    if (tallestStackWithBottomIndex < currentStack)
                    {
                        tallestStackWithBottomIndex = currentStack;
                    }
                }
            }

            maxStackMap[index] = tallestStackWithBottomIndex;
            return(tallestStackWithBottomIndex);
        }
示例#3
0
        public StackOfBoxes Clone()
        {
            StackOfBoxes clone = new StackOfBoxes();

            foreach (Box box in boxes)
            {
                clone.AddTop(box);
            }

            return(clone);
        }
示例#4
0
        public StackOfBoxes BuildMaxHeightStack(List <Box> boxes)
        {
            boxes.Sort();
            boxes.Print();

            // Map from bottom index to max height stack of boxes
            Dictionary <int, StackOfBoxes> maxStackMap = new Dictionary <int, StackOfBoxes>();
            StackOfBoxes tallestStack = new StackOfBoxes();

            for (int i = 0; i < boxes.Count; i++)
            {
                var currentTallestStack = BuildMaxHeightStackRecurse(boxes, i, maxStackMap);
                if (tallestStack < currentTallestStack)
                {
                    tallestStack = currentTallestStack;
                }
            }

            return(tallestStack);
        }