1
00:00:00,000 --> 00:00:02,800
A set stores distinct hashable values.

2
00:00:03,200 --> 00:00:07,760
It answers whether a value is present,
while a dictionary can also retain a count

3
00:00:07,840 --> 00:00:08,720
or another value.

4
00:00:09,200 --> 00:00:13,720
Our example finds the longest consecutive
run in an unsorted input.

5
00:00:14,000 --> 00:00:18,560
The input contains a repeated three, but
repeating a value does not make its run

6
00:00:18,640 --> 00:00:19,280
longer.

7
00:00:19,520 --> 00:00:24,240
We will use membership tests to find each
run's start and avoid scanning the same

8
00:00:24,360 --> 00:00:25,360
run repeatedly.

9
00:00:25,667 --> 00:00:30,200
The input values can arrive in any order
and can include duplicates.

10
00:00:30,254 --> 00:00:33,640
The set members retain each distinct value
once.

11
00:00:33,694 --> 00:00:37,347
We do not rely on their iteration order
because a Python set has no

12
00:00:37,547 --> 00:00:39,294
insertion-order guarantee.

13
00:00:39,320 --> 00:00:42,987
The run starts are the members whose
predecessor is absent.

14
00:00:43,027 --> 00:00:47,347
Starting only at those values ensures that
each consecutive run is extended

15
00:00:47,427 --> 00:00:48,227
once.

16
00:00:48,427 --> 00:00:52,227
Sorting the displayed set makes the
picture readable, but the algorithm does

17
00:00:52,307 --> 00:00:53,747
not need that sort.

18
00:00:53,967 --> 00:00:58,367
The first four is absent, so the duplicate
flag is false before we add

19
00:00:58,447 --> 00:00:59,007
it.

20
00:00:59,047 --> 00:01:02,580
The first one is also absent and becomes
another member.

21
00:01:02,634 --> 00:01:07,407
The repeated four is already present, so
the flag is true and adding it leaves the

22
00:01:07,487 --> 00:01:09,114
membership unchanged.

23
00:01:09,140 --> 00:01:14,087
A set cannot tell us that four occurred
twice unless we keep separate counts.

24
00:01:14,127 --> 00:01:18,527
Use a dictionary for frequencies and a
list when repeated values or positions

25
00:01:18,607 --> 00:01:19,647
must remain.

26
00:01:19,900 --> 00:01:22,860
Start at three because two is absent.

27
00:01:23,020 --> 00:01:27,740
Three, four, and five are present, and six
is absent, so this run has length

28
00:01:27,900 --> 00:01:28,620
three.

29
00:01:28,860 --> 00:01:32,060
Inspect four and find its predecessor,
three.

30
00:01:32,220 --> 00:01:36,140
Skip it because the run beginning at three
already covers four.

31
00:01:36,300 --> 00:01:39,500
Inspect five and find four, so skip again.

32
00:01:39,580 --> 00:01:43,740
This predecessor check prevents a long run
from being rescanned once from every

33
00:01:44,100 --> 00:01:44,540
member.

34
00:01:44,767 --> 00:01:47,727
Start at eight because seven is absent.

35
00:01:47,807 --> 00:01:51,327
Nine is also absent, so this run has
length one.

36
00:01:51,407 --> 00:01:56,007
Start at ten because nine is absent, and
stop when eleven is absent.

37
00:01:56,047 --> 00:02:00,767
The longest run remains three, from the
values three, four, and five.

38
00:02:00,847 --> 00:02:04,874
Every distinct input value belongs to
exactly one of these runs.

39
00:02:04,900 --> 00:02:09,487
The repeated three was removed by the set
and contributes no extra length.

40
00:02:09,733 --> 00:02:13,253
Build the set and initialize longest to
zero.

41
00:02:13,333 --> 00:02:16,186
The outer loop visits each distinct
member.

42
00:02:16,240 --> 00:02:19,853
The predecessor test skips members that
cannot start a run.

43
00:02:19,893 --> 00:02:23,226
Advance end while that integer remains
present.

44
00:02:23,280 --> 00:02:27,533
At the first missing value, end minus
start equals the run length.

45
00:02:27,573 --> 00:02:30,346
Update the longest length and continue.

46
00:02:30,400 --> 00:02:34,933
An empty input leaves longest at zero,
which also gives the correct empty-input

47
00:02:35,373 --> 00:02:35,893
result.

48
00:02:36,133 --> 00:02:39,493
The outer loop visits each distinct value
once.

49
00:02:39,693 --> 00:02:44,586
Across all inner loops, each distinct
value is included in one run only.

50
00:02:44,640 --> 00:02:48,613
With expected constant-time set
operations, the full algorithm takes

51
00:02:48,693 --> 00:02:51,373
linear expected time in the input length.

52
00:02:51,413 --> 00:02:54,400
The set requires space for the distinct
values.

53
00:02:54,426 --> 00:02:57,693
A severe collision pattern can make the
time quadratic.

54
00:02:57,733 --> 00:03:02,613
Do not count the sorted display as part of
the unsorted algorithm, or omit it if

55
00:03:02,693 --> 00:03:05,013
your submitted code actually sorts.

56
00:03:05,233 --> 00:03:09,233
An empty set is written as set with
parentheses.

57
00:03:09,393 --> 00:03:11,633
Empty braces create a dictionary.

58
00:03:11,793 --> 00:03:16,753
Discard tolerates a missing value, while
remove raises an error when that value

59
00:03:16,833 --> 00:03:17,793
is absent.

60
00:03:17,873 --> 00:03:22,753
For this algorithm, test an empty input,
repeated copies of one integer,

61
00:03:22,833 --> 00:03:25,873
negative integers, and isolated values.

62
00:03:26,073 --> 00:03:29,873
State why the predecessor check makes
every run appear once.

63
00:03:29,953 --> 00:03:34,833
The argument must hold regardless of the
order in which the set visits its members.
