A loop in a loop, still linear.
transcript
A while loop inside a for loop. That sounds like n times n. A hundred thousand prices, ten billion steps? It is not. Here is why.
Five prices arrive. Each one is pushed once. Four arrives. It pops the three. Two is pushed on top. Six arrives. It pops two, four and five. One arrival popped three values. The next arrival may pop none.
Count the whole pass. Five values were pushed. Four were popped. One is left on the stack.
A value is pushed once, when it arrives. A value is popped at most once. So pops never outnumber pushes. This holds for any list of values.
A hundred thousand prices push a hundred thousand times. They pop at most as many. About two hundred thousand steps in all.
One value can pop many. The total stays small. Each pop was paid for by an earlier push. So the average is one pop at most. Full breakdown in the lecture.