Camina de izquierda a derecha, usando una pila para hacer un seguimiento del color en el que estás. En lugar de un mapa discreto, use los 10 números en su conjunto de datos como puntos de quiebre.
Comenzando con una pila vacía y estableciendo starta 0, repite hasta llegar al final:
- Si la pila está vacía:
- Busque el primer color que comience en o después
start, y empújelo junto con todos los colores de menor rango en la pila. En su lista aplanada, marque el comienzo de ese color.
- más (si no está vacío):
- Encuentre el siguiente punto de inicio para cualquier color de mayor rango en o después
start, y encuentre el final del color actual
- Si el siguiente color comienza primero, empújalo y cualquier otra cosa en el camino hacia la pila. Actualice el final del color actual como el comienzo de este y agregue el inicio de este color a la lista aplanada.
- Si no hay ninguno y el color actual termina primero, configúrelo
startal final de este color, sáquelo de la pila y verifique el siguiente color mejor clasificado
- Si
startestá dentro del rango del siguiente color, agregue este color a la lista aplanada, comenzando en start.
- Si la pila se vacía, simplemente continúa el ciclo (vuelve al primer punto).
Este es un repaso mental dados sus datos de ejemplo:
# Initial data.
flattened = []
stack = []
start = 0
# Stack is empty. Look for the next starting point at 0 or later: "b", 0 - Push it and all lower levels onto stack
flattened = [ (b, 0, ?) ]
stack = [ r, b ]
start = 0
# End of "b" is 5.4, next higher-colored start is "g" at 2 - Delimit and continue
flattened = [ (b, 0, 2), (g, 2, ?) ]
stack = [ r, b, g ]
start = 2
# End of "g" is 12, next higher-colored start is "y" at 3.5 - Delimit and continue
flattened = [ (b, 0, 2), (g, 2, 3.5), (y, 3.5, ?) ]
stack = [ r, b, g, y ]
start = 3.5
# End of "y" is 6.7, next higher-colored start is "o" at 6.7 - Delimit and continue
flattened = [ (b, 0, 2), (g, 2, 3.5), (y, 3.5, 6.7), (o, 6.7, ?) ]
stack = [ r, b, g, y, o ]
start = 6.7
# End of "o" is 10, and there is nothing starting at 12 or later in a higher color. Next off stack, "y", has already ended. Next off stack, "g", has not ended. Delimit and continue.
flattened = [ (b, 0, 2), (g, 2, 3.5), (y, 3.5, 6.7), (o, 6.7, 10), (g, 10, ?) ]
stack = [ r, b, g ]
start = 10
# End of "g" is 12, there is nothing starting at 12 or later in a higher color. Next off stack, "b", is out of range (already ended). Next off stack, "r", is out of range (not started). Mark end of current color:
flattened = [ (b, 0, 2), (g, 2, 3.5), (y, 3.5, 6.7), (o, 6.7, 10), (g, 10, 12) ]
stack = []
start = 12
# Stack is empty. Look for the next starting point at 12 or later: "r", 12.5 - Push onto stack
flattened = [ (b, 0, 2), (g, 2, 3.5), (y, 3.5, 6.7), (o, 6.7, 10), (g, 10, 12), (r, 12.5, ?) ]
stack = [ r ]
start = 12
# End of "r" is 13.8, and there is nothing starting at 12 or higher in a higher color. Mark end and pop off stack.
flattened = [ (b, 0, 2), (g, 2, 3.5), (y, 3.5, 6.7), (o, 6.7, 10), (g, 10, 12), (r, 12.5, 13.8) ]
stack = []
start = 13.8
# Stack is empty and nothing is past 13.8 - We're done.