예제 #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);
        }