1
00:00:00,000 --> 00:00:03,240
A queue processes the oldest waiting item
first.

2
00:00:03,520 --> 00:00:08,240
Its first-in, first-out rule is useful
when work must proceed in arrival order

3
00:00:08,560 --> 00:00:10,640
or by distance from a starting point.

4
00:00:11,280 --> 00:00:16,240
Our example gives each queued job one unit
of work and returns unfinished jobs to

5
00:00:16,280 --> 00:00:16,800
the back.

6
00:00:17,440 --> 00:00:22,160
Python's deque provides efficient
insertion and removal at both ends, so the

7
00:00:22,240 --> 00:00:25,840
queue does not shift all waiting jobs each
time it serves one.

8
00:00:26,167 --> 00:00:29,127
Front removal takes the oldest waiting
job.

9
00:00:29,247 --> 00:00:32,967
One work unit reduces its remaining
requirement by one.

10
00:00:33,047 --> 00:00:37,714
Back insertion returns an unfinished job
after all other waiting jobs.

11
00:00:37,740 --> 00:00:41,287
A completed job leaves the queue and
enters the result.

12
00:00:41,367 --> 00:00:43,660
This rule is a round-robin schedule.

13
00:00:43,714 --> 00:00:48,087
The queue contains only unfinished jobs,
and its order records who receives the

14
00:00:48,207 --> 00:00:49,154
next turn.

15
00:00:49,180 --> 00:00:53,687
The example uses positive integer work
requirements and no new arrivals during

16
00:00:53,727 --> 00:00:54,647
the schedule.

17
00:00:54,867 --> 00:00:59,507
Append adds on the right and popleft
removes on the left, which gives

18
00:00:59,587 --> 00:01:00,627
FIFO behavior.

19
00:01:01,027 --> 00:01:05,787
Appendleft and pop provide the opposite
ends when another algorithm needs them.

20
00:01:05,987 --> 00:01:09,027
Both ends support constant-time
operations.

21
00:01:09,067 --> 00:01:13,747
The list operation pop at position zero
shifts the remaining references and

22
00:01:13,827 --> 00:01:15,347
costs linear time.

23
00:01:15,427 --> 00:01:20,387
A deque is not a replacement for every
list use: indexed access in its middle can

24
00:01:20,467 --> 00:01:21,747
take linear time.

25
00:01:21,867 --> 00:01:25,747
Choose the structure from the operations
the algorithm actually requires.

26
00:01:26,067 --> 00:01:29,107
Serve A, which starts with two units.

27
00:01:29,187 --> 00:01:33,347
One unit remains, so A returns behind B
and C.

28
00:01:33,427 --> 00:01:36,147
Serve B, which needs only one unit.

29
00:01:36,227 --> 00:01:38,387
B completes and leaves the queue.

30
00:01:38,467 --> 00:01:41,347
Serve C, which starts with three units.

31
00:01:41,427 --> 00:01:44,467
Two remain, so C returns behind A.

32
00:01:44,547 --> 00:01:49,267
The first completed job is B even though A
arrived first, because each turn

33
00:01:49,347 --> 00:01:53,747
supplies only one unit and jobs can
require different numbers of turns.

34
00:01:54,067 --> 00:01:57,187
Serve A again and finish its second unit.

35
00:01:57,267 --> 00:01:59,747
A becomes the second completed job.

36
00:01:59,827 --> 00:02:04,707
Serve C again and reduce its remaining
requirement from two to one, then place

37
00:02:04,787 --> 00:02:06,987
it back in the otherwise empty queue.

38
00:02:07,027 --> 00:02:09,534
Serve C once more and complete it.

39
00:02:09,560 --> 00:02:13,707
The queue is empty, and the completion
order is B, A, C.

40
00:02:13,747 --> 00:02:18,307
Six turns supply the six total work units
required by the three jobs.

41
00:02:18,533 --> 00:02:22,853
Reject a nonpositive work requirement
before creating the queue.

42
00:02:23,013 --> 00:02:26,933
Copy the input jobs into a deque and
create the output list.

43
00:02:27,173 --> 00:02:31,533
Remove one job on each loop iteration and
reduce its remaining work.

44
00:02:31,573 --> 00:02:34,400
Reinsert it only if work remains.

45
00:02:34,666 --> 00:02:37,133
Otherwise append its name to completed.

46
00:02:37,173 --> 00:02:41,813
Each iteration reduces the total remaining
work by one, so positive finite

47
00:02:42,173 --> 00:02:45,893
requirements guarantee that this loop
eventually empties the queue.

48
00:02:46,133 --> 00:02:51,013
Let n be the number of jobs and T be their
total work requirement.

49
00:02:51,453 --> 00:02:55,306
Initialization and input validation take
linear time in n.

50
00:02:55,360 --> 00:03:00,000
The queue loop runs T times, with
constant-time operations at its ends.

51
00:03:00,106 --> 00:03:02,906
Total time is big O of n plus T.

52
00:03:02,960 --> 00:03:07,693
The queue holds at most n jobs, so
auxiliary space is big O of n.

53
00:03:07,733 --> 00:03:12,293
Reporting only big O of n time would
ignore jobs that need many turns.

54
00:03:12,533 --> 00:03:16,453
An empty job list produces an empty
completion list.

55
00:03:16,573 --> 00:03:20,053
One job receives repeated turns until it
finishes.

56
00:03:20,133 --> 00:03:24,613
Equal requirements preserve arrival order
among completions, while unequal

57
00:03:24,693 --> 00:03:26,906
requirements can change that order.

58
00:03:26,960 --> 00:03:30,586
A zero work requirement is rejected by
this contract.

59
00:03:30,640 --> 00:03:35,413
When explaining the algorithm, state what
remains in the queue after each iteration

60
00:03:35,533 --> 00:03:38,346
and why the sum of remaining work
decreases.

61
00:03:38,400 --> 00:03:40,533
That argument proves termination.
