1
00:00:00,000 --> 00:00:04,640
Each reading asks how far away its first
later, strictly higher reading

2
00:00:04,720 --> 00:00:05,360
occurs.

3
00:00:06,000 --> 00:00:10,480
Our invented readings are eight, six, six,
nine,

4
00:00:10,640 --> 00:00:11,680
seven, and ten.

5
00:00:12,320 --> 00:00:16,560
An equal value does not qualify, and a
reading with no later higher value

6
00:00:16,640 --> 00:00:17,600
receives zero.

7
00:00:18,240 --> 00:00:21,520
The stack retains the indices whose
answers remain unknown.

8
00:00:22,080 --> 00:00:26,960
Those indices tell us both which earlier
values to compare and how far away the new

9
00:00:27,040 --> 00:00:27,920
value lies.

10
00:00:28,560 --> 00:00:33,040
We will trace the whole scan and explain
why a loop inside another loop still takes

11
00:00:33,120 --> 00:00:34,640
linear total time here.

12
00:00:34,967 --> 00:00:39,167
Read the next value and inspect the index
at the top of the stack.

13
00:00:39,207 --> 00:00:43,567
Resolve the top while its earlier value is
smaller than the new reading.

14
00:00:43,607 --> 00:00:48,527
Each removal receives the current index
minus its own index as the distance.

15
00:00:48,567 --> 00:00:52,607
Continue because one reading can answer
several earlier positions.

16
00:00:52,647 --> 00:00:55,527
Push the current index after those
removals.

17
00:00:55,607 --> 00:00:59,327
If the top value is equal or larger, it
stays pending.

18
00:00:59,367 --> 00:01:04,007
Because the stack values are nonincreasing
from bottom to top, no value below that

19
00:01:04,127 --> 00:01:06,407
top can qualify for removal either.

20
00:01:06,633 --> 00:01:11,353
At index zero, eight has no earlier
reading to resolve, so index

21
00:01:11,433 --> 00:01:13,233
zero enters the stack.

22
00:01:13,273 --> 00:01:17,753
At index one, six is smaller than eight,
so it also enters without a

23
00:01:17,873 --> 00:01:18,673
removal.

24
00:01:18,713 --> 00:01:22,660
At index two, another six meets an equal
top value.

25
00:01:22,846 --> 00:01:26,913
Equality is not strictly greater, so both
sixes remain pending.

26
00:01:26,953 --> 00:01:31,873
The stack now holds indices zero, one, and
two, whose values are eight,

27
00:01:31,913 --> 00:01:33,326
six, and six.

28
00:01:33,380 --> 00:01:37,113
The zeros in the answer array are still
provisional because later values can

29
00:01:37,313 --> 00:01:38,153
replace them.

30
00:01:38,400 --> 00:01:42,960
Nine exceeds the six at index two, so that
index leaves with distance

31
00:01:43,120 --> 00:01:43,840
one.

32
00:01:43,880 --> 00:01:46,560
The next top is the six at index one.

33
00:01:46,640 --> 00:01:49,040
It also leaves, with distance two.

34
00:01:49,280 --> 00:01:53,760
Finally the eight at index zero is smaller
than nine and leaves with distance

35
00:01:53,840 --> 00:01:54,640
three.

36
00:01:54,760 --> 00:01:58,560
All three removals use the same current
index, three.

37
00:01:58,640 --> 00:02:03,040
Nine is the first greater reading for each
removed position because any earlier

38
00:02:03,120 --> 00:02:06,507
qualifying reading would already have
removed that position.

39
00:02:06,613 --> 00:02:10,720
After the removals, index three becomes
the only pending index.

40
00:02:11,033 --> 00:02:15,753
Seven remains pending after the nine
because it cannot resolve a larger value.

41
00:02:15,873 --> 00:02:20,633
Ten resolves the seven at index four with
distance one, then the nine at index

42
00:02:20,713 --> 00:02:22,633
three with distance two.

43
00:02:22,793 --> 00:02:25,673
Index five is pushed after those removals.

44
00:02:25,753 --> 00:02:30,233
The final pending index has no later
reading at all, so its zero becomes the

45
00:02:30,313 --> 00:02:31,993
correct final answer.

46
00:02:32,033 --> 00:02:36,793
The complete distances are three, two,
one, two,

47
00:02:36,953 --> 00:02:38,473
one, and zero.

48
00:02:38,513 --> 00:02:42,953
The function returns a separate result
list and leaves the readings unchanged.

49
00:02:43,267 --> 00:02:47,827
Before each iteration, every pending
position has seen no strictly greater

50
00:02:47,907 --> 00:02:50,547
value in the processed part to its right.

51
00:02:50,587 --> 00:02:54,947
A removal records the first such value
because the current reading is the first

52
00:02:55,027 --> 00:02:59,067
qualifying value encountered while that
index remains pending.

53
00:02:59,107 --> 00:03:02,667
A retained top is at least as large as the
current value.

54
00:03:02,707 --> 00:03:06,707
Every value below it is at least as large
again, so the scan can stop

55
00:03:06,787 --> 00:03:07,787
removing.

56
00:03:07,827 --> 00:03:12,147
Pushing the current index preserves both
the index order and the nonincreasing

57
00:03:12,227 --> 00:03:13,227
value order.

58
00:03:13,267 --> 00:03:17,107
These statements explain why the stored
distances are correct.

59
00:03:17,367 --> 00:03:21,687
The pending list stores indices rather
than only the readings.

60
00:03:21,767 --> 00:03:26,327
The while condition first checks that the
list is nonempty, then compares the value

61
00:03:26,367 --> 00:03:31,127
at its last index with the current value
using a strict less-than test.

62
00:03:31,207 --> 00:03:35,714
The distance update subtracts the removed
index from the current index.

63
00:03:35,900 --> 00:03:40,487
Replacing less than with less than or
equal would answer a next-greater-or-equal

64
00:03:40,567 --> 00:03:44,380
problem and make equal readings incorrect
for this contract.

65
00:03:44,434 --> 00:03:48,007
The full function and its docstring are
available in the written lesson and

66
00:03:48,207 --> 00:03:49,607
downloadable script.

67
00:03:49,833 --> 00:03:54,713
Each index enters the stack once, giving
six pushes in this example.

68
00:03:54,833 --> 00:03:58,660
Each index leaves at most once, and five
leave here.

69
00:03:58,686 --> 00:04:03,393
The whole scan therefore has at most n
pushes and n successful pops,

70
00:04:03,433 --> 00:04:07,860
regardless of how unevenly the pops are
distributed between iterations.

71
00:04:07,886 --> 00:04:12,286
There is also at most one failed top
comparison per input iteration.

72
00:04:12,340 --> 00:04:16,873
With constant-time integer comparisons and
amortized list operations, total

73
00:04:16,953 --> 00:04:18,660
time is O of n.

74
00:04:18,686 --> 00:04:23,113
The pending stack needs O of n auxiliary
space in a decreasing or constant

75
00:04:23,193 --> 00:04:27,353
input, and the returned answer needs O of
n output space.

76
00:04:27,600 --> 00:04:32,320
An equal pair must return two zeros, which
exposes a mistaken equality

77
00:04:32,600 --> 00:04:33,280
condition.

78
00:04:33,320 --> 00:04:37,440
A decreasing input keeps every index
pending and tests the largest

79
00:04:37,520 --> 00:04:38,320
stack.

80
00:04:38,400 --> 00:04:43,040
An increasing input resolves the previous
index immediately, while our nine example

81
00:04:43,120 --> 00:04:45,520
tests several removals at once.

82
00:04:45,600 --> 00:04:49,840
A circular input changes the search range
and needs a separate argument about a

83
00:04:49,920 --> 00:04:53,680
second scan and when original indices
enter the stack.

84
00:04:53,760 --> 00:04:58,240
Use the written repeat exercise to
reconstruct this method, then attempt the

85
00:04:58,280 --> 00:05:01,840
circular variant without copying the first
loop unchanged.
