1
00:00:00,000 --> 00:00:03,920
Two pointers preserve the part of an input
that still matters.

2
00:00:04,000 --> 00:00:08,680
Their movement is correct only when you
can explain which candidates it removes.

3
00:00:08,960 --> 00:00:12,560
Our first example finds a target pair in a
sorted list.

4
00:00:12,800 --> 00:00:17,080
The second finds the longest substring
without repeated characters.

5
00:00:17,120 --> 00:00:21,280
Both avoid revisiting every earlier
position, but they rely on different

6
00:00:21,360 --> 00:00:22,240
invariants.

7
00:00:22,567 --> 00:00:27,367
A small sum cannot be repaired by keeping
the left value and selecting an even

8
00:00:27,527 --> 00:00:29,247
smaller right value.

9
00:00:29,287 --> 00:00:31,087
Move left forward.

10
00:00:31,127 --> 00:00:35,767
A large sum cannot be repaired by keeping
the right value and selecting an even

11
00:00:35,847 --> 00:00:37,487
larger left value.

12
00:00:37,527 --> 00:00:39,340
Move right backward.

13
00:00:39,394 --> 00:00:41,900
An equal sum supplies the answer.

14
00:00:41,954 --> 00:00:46,807
This argument requires sorted values, and
the left index must remain below the right

15
00:00:46,887 --> 00:00:50,167
index so one position is never used twice.

16
00:00:50,400 --> 00:00:54,400
One plus nine gives ten, below the target
thirteen.

17
00:00:54,560 --> 00:00:57,840
Discard the first value and move left
forward.

18
00:00:57,920 --> 00:01:02,840
Three plus nine gives twelve, which is
still too small, so move left again.

19
00:01:02,880 --> 00:01:05,093
Four plus nine gives thirteen.

20
00:01:05,227 --> 00:01:07,320
Return indices two and four.

21
00:01:07,360 --> 00:01:12,280
Every discarded left value was too small
even with the largest remaining partner,

22
00:01:12,320 --> 00:01:15,040
which explains why no solution was lost.

23
00:01:15,267 --> 00:01:18,947
Initialize the pointers at the first and
last positions.

24
00:01:19,147 --> 00:01:22,174
Compute the sum while the positions remain
distinct.

25
00:01:22,200 --> 00:01:25,880
The comparison selects a return or one
pointer update.

26
00:01:25,934 --> 00:01:30,387
A target that never appears eventually
leaves no candidate pair, and the function

27
00:01:30,667 --> 00:01:31,787
returns None.

28
00:01:31,827 --> 00:01:36,547
Each unsuccessful iteration reduces the
separation, so the loop cannot repeat a

29
00:01:36,627 --> 00:01:38,547
boundary state indefinitely.

30
00:01:38,767 --> 00:01:43,407
The pointer separation decreases on every
unsuccessful iteration, giving

31
00:01:43,607 --> 00:01:46,687
linear time and constant auxiliary space.

32
00:01:46,847 --> 00:01:51,807
Sorting first adds a different cost if the
caller supplies unsorted values.

33
00:01:51,967 --> 00:01:55,300
Original indices can also be lost when
values move.

34
00:01:55,354 --> 00:01:59,967
For an unsorted pair problem that requests
original positions, a dictionary of

35
00:02:00,047 --> 00:02:02,727
earlier values is often a better choice.

36
00:02:02,767 --> 00:02:05,647
Select the pattern after you read the
contract.

37
00:02:05,867 --> 00:02:10,187
The current window contains distinct
characters between left and the current

38
00:02:10,267 --> 00:02:11,680
right endpoint.

39
00:02:11,734 --> 00:02:16,534
The dictionary stores the most recent
position of every character already read.

40
00:02:16,560 --> 00:02:20,907
A repeated character inside the window
moves left just past its earlier

41
00:02:20,987 --> 00:02:22,054
occurrence.

42
00:02:22,080 --> 00:02:26,667
An occurrence before left does not belong
to the current window and must not move

43
00:02:26,747 --> 00:02:28,054
left backward.

44
00:02:28,160 --> 00:02:30,667
Taking the maximum preserves that rule.

45
00:02:30,900 --> 00:02:33,700
Read a first and keep left at zero.

46
00:02:33,780 --> 00:02:36,140
The window contains one character.

47
00:02:36,180 --> 00:02:40,527
Read b next and extend the window because
b has not appeared.

48
00:02:40,633 --> 00:02:43,087
Read c next and extend it again.

49
00:02:43,113 --> 00:02:48,020
The dictionary now remembers positions
zero, one, and two for a, b, and

50
00:02:48,100 --> 00:02:48,793
c.

51
00:02:48,847 --> 00:02:52,980
The best span reaches from zero through
the exclusive end three.

52
00:02:53,200 --> 00:02:57,680
The next a appears at position three,
while its previous occurrence is at

53
00:02:57,760 --> 00:02:58,720
zero.

54
00:02:58,800 --> 00:03:02,453
Move left to one so the earlier a leaves
the window.

55
00:03:02,507 --> 00:03:07,360
The following e has no earlier occurrence,
so left stays at one and the window

56
00:03:07,400 --> 00:03:08,373
grows.

57
00:03:08,427 --> 00:03:12,133
The current span contains b, c, a, e.

58
00:03:12,187 --> 00:03:15,920
No scan of those earlier characters is
needed because the dictionary already

59
00:03:16,080 --> 00:03:17,840
identifies the conflict.

60
00:03:18,067 --> 00:03:22,947
Read f and extend the current span to b,
c, a, e, f.

61
00:03:23,347 --> 00:03:24,947
Its length is five.

62
00:03:25,187 --> 00:03:29,427
The final b repeats position one, so left
advances to two.

63
00:03:29,747 --> 00:03:34,627
The new span c, a, e, f, b also has length
five.

64
00:03:34,667 --> 00:03:39,107
The update uses a strict improvement, so
the earlier span remains selected.

65
00:03:39,507 --> 00:03:42,627
Return start one and exclusive end six.

66
00:03:42,933 --> 00:03:47,573
The function initializes an empty
dictionary and an empty best span.

67
00:03:47,653 --> 00:03:52,640
For each character, it retrieves the
earlier position or minus one when absent.

68
00:03:52,666 --> 00:03:56,693
The boundary update takes the maximum of
the existing left boundary and the

69
00:03:56,933 --> 00:03:58,813
position after that occurrence.

70
00:03:58,853 --> 00:04:03,813
Record the new position, then replace the
best span only if the new valid window is

71
00:04:03,933 --> 00:04:04,573
longer.

72
00:04:04,613 --> 00:04:07,893
The returned pair avoids copying every
candidate.

73
00:04:08,133 --> 00:04:12,373
The search visits n characters with
expected constant-time dictionary

74
00:04:12,733 --> 00:04:15,453
operations, giving expected linear time.

75
00:04:15,653 --> 00:04:19,253
The dictionary stores at most one entry
per distinct character.

76
00:04:19,653 --> 00:04:24,173
The final slice costs time and output
space proportional to its length.

77
00:04:24,373 --> 00:04:28,933
The explanation trace prints a slice at
every step, which can produce quadratic

78
00:04:29,013 --> 00:04:29,813
display work.

79
00:04:30,133 --> 00:04:33,413
That diagnostic behavior is absent from
the search function.

80
00:04:33,733 --> 00:04:38,053
The string abba exposes a boundary that
moves backward to an obsolete

81
00:04:38,133 --> 00:04:39,306
occurrence.

82
00:04:39,360 --> 00:04:43,813
A singleton list exposes a pair
implementation that uses the same position

83
00:04:43,933 --> 00:04:44,826
twice.

84
00:04:44,880 --> 00:04:49,600
An empty input checks that your boundaries
return the stated empty result.

85
00:04:49,626 --> 00:04:53,733
In an interview, explain why a discarded
position cannot be required

86
00:04:53,813 --> 00:04:54,720
later.

87
00:04:54,826 --> 00:04:59,093
Without that argument, two moving indices
are only an unproved guess.
