เดินจากซ้ายไปขวาใช้สแต็คเพื่อติดตามสีที่คุณใช้ แทนที่จะเป็นแผนที่แบบไม่ต่อเนื่องใช้ตัวเลข 10 ตัวในชุดข้อมูลของคุณเป็นจุดพัก
เริ่มต้นด้วยสแต็กเปล่าและตั้งค่าstart
เป็น 0 วนซ้ำจนกว่าเราจะถึงจุดสิ้นสุด:
- หากสแต็กว่างเปล่า:
- มองหาสีแรกเริ่มที่หรือหลังจาก
start
นั้นกดและสีที่มีอันดับต่ำกว่าทั้งหมดลงบนสแต็ก ในรายการที่แบนให้ทำเครื่องหมายจุดเริ่มต้นของสีนั้น
- อื่น (ถ้าไม่ว่าง):
- ค้นหาจุดเริ่มต้นถัดไปสำหรับสีอันดับที่สูงขึ้นในหรือหลัง
start
และค้นหาจุดสิ้นสุดของสีปัจจุบัน
- หากสีถัดไปเริ่มขึ้นก่อนให้ดันสีและสิ่งอื่น ๆ ตามขวางลงบนสแต็ก อัพเดตจุดสิ้นสุดของสีปัจจุบันเป็นจุดเริ่มต้นของสีนี้และเพิ่มจุดเริ่มต้นของสีนี้ลงในรายการที่แบน
- หากไม่มีและสีปัจจุบันสิ้นสุดลงก่อนให้ตั้งค่า
start
เป็นสีสุดท้ายแล้วนำออกมาจากสแต็กและตรวจสอบสีที่มีอันดับสูงสุดถัดไป
- หากอยู่ในช่วงสีต่อไปเพิ่มสีนี้ลงในรายการบี้เริ่มต้นที่
start
start
- หากสแต็กเปล่าให้ทำต่อไปเรื่อย ๆ (ย้อนกลับไปที่สัญลักษณ์แรก)
นี่คือการใช้งานที่ได้รับจากข้อมูลตัวอย่างของคุณ:
# 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.