1
00:00:00,000 --> 00:00:02,800
A dictionary associates a key with a
value.

2
00:00:03,200 --> 00:00:07,680
It can remember a previous position, a
count, or a summary of the input already

3
00:00:07,760 --> 00:00:08,480
processed.

4
00:00:09,040 --> 00:00:13,440
Our worked problem counts contiguous
segments whose sum is three, including

5
00:00:13,520 --> 00:00:14,480
negative values.

6
00:00:15,040 --> 00:00:19,360
A dictionary of prefix frequencies avoids
checking every segment separately.

7
00:00:19,840 --> 00:00:24,640
We will first distinguish membership from
a stored count, then follow every update

8
00:00:24,680 --> 00:00:25,600
in the example.

9
00:00:25,933 --> 00:00:29,800
The current value supplies a key or helps
us compute one.

10
00:00:29,826 --> 00:00:34,253
A dictionary lookup retrieves the value
stored under that key without scanning

11
00:00:34,333 --> 00:00:37,093
every entry under ordinary hash behavior.

12
00:00:37,133 --> 00:00:39,826
The stored information depends on the
problem.

13
00:00:39,880 --> 00:00:44,573
A set stores only membership; a dictionary
can store how many times a value

14
00:00:44,653 --> 00:00:45,493
occurred.

15
00:00:45,533 --> 00:00:48,466
Integer keys and string keys are hashable.

16
00:00:48,520 --> 00:00:51,613
A mutable list cannot serve as a
dictionary key.

17
00:00:51,867 --> 00:00:56,867
Read four and use get with a default of
zero, then add one.

18
00:00:56,907 --> 00:00:59,587
Read one and create its first count.

19
00:00:59,627 --> 00:01:03,974
Read four again and replace the count at
the existing key with two.

20
00:01:04,000 --> 00:01:08,320
The dictionary now distinguishes one
occurrence from repeated occurrences.

21
00:01:08,374 --> 00:01:11,360
Its insertion order does not sort the
keys.

22
00:01:11,494 --> 00:01:16,107
Calling get on an absent key returns the
default without inserting that key, which

23
00:01:16,187 --> 00:01:18,827
is useful when a missing count means zero.

24
00:01:19,067 --> 00:01:22,227
Let prefix mean the sum through the
current position.

25
00:01:22,427 --> 00:01:27,307
If a segment ending here has sum three,
the prefix before that segment must

26
00:01:27,467 --> 00:01:29,387
equal prefix minus three.

27
00:01:29,627 --> 00:01:34,587
Start with zero occurring once, because
the empty prefix exists before the first

28
00:01:34,667 --> 00:01:35,227
value.

29
00:01:35,467 --> 00:01:37,707
Keep a frequency for every prefix.

30
00:01:38,107 --> 00:01:42,747
Two earlier positions can have the same
sum, and both positions can start a valid

31
00:01:42,827 --> 00:01:43,387
segment.

32
00:01:43,707 --> 00:01:46,267
A membership set would lose that
multiplicity.

33
00:01:46,567 --> 00:01:51,527
After reading two, the prefix is two and
the needed earlier prefix is minus

34
00:01:51,607 --> 00:01:52,380
one.

35
00:01:52,434 --> 00:01:55,567
It has not occurred, so the total stays
zero.

36
00:01:55,607 --> 00:01:58,274
Store prefix two after the lookup.

37
00:01:58,380 --> 00:02:02,967
After reading minus one, the prefix
becomes one and the needed value is minus

38
00:02:03,047 --> 00:02:03,767
two.

39
00:02:03,847 --> 00:02:05,647
That value is also absent.

40
00:02:05,687 --> 00:02:07,314
Store prefix one.

41
00:02:07,340 --> 00:02:11,447
The dictionary now describes the empty
prefix and the two prefixes already

42
00:02:11,527 --> 00:02:14,007
visited, ready for the next position.

43
00:02:14,233 --> 00:02:17,353
The next two raises the prefix to three.

44
00:02:17,393 --> 00:02:22,353
We need an earlier zero, and the initial
empty prefix supplies one match.

45
00:02:22,393 --> 00:02:25,486
This counts the first three values as a
segment.

46
00:02:25,540 --> 00:02:28,260
The final one raises the prefix to four.

47
00:02:28,286 --> 00:02:32,153
We need an earlier one, which occurs after
the second input.

48
00:02:32,233 --> 00:02:35,300
That counts the final two values as
another segment.

49
00:02:35,326 --> 00:02:37,246
The result is two.

50
00:02:37,380 --> 00:02:41,513
Negative values cause no problem because
the argument uses subtraction, not a

51
00:02:41,593 --> 00:02:43,033
monotone window sum.

52
00:02:43,267 --> 00:02:46,787
Initialize the prefix dictionary with the
empty prefix.

53
00:02:46,827 --> 00:02:50,787
The loop adds one input value to the
running sum and looks up the needed

54
00:02:50,867 --> 00:02:51,867
earlier sum.

55
00:02:51,907 --> 00:02:53,827
Add its frequency to total.

56
00:02:53,947 --> 00:02:56,987
Store the current prefix only after that
lookup.

57
00:02:57,027 --> 00:03:01,467
This order keeps the dictionary restricted
to earlier positions during the count.

58
00:03:01,507 --> 00:03:06,387
If the target is zero and we insert first,
we incorrectly count the empty segment

59
00:03:06,467 --> 00:03:08,627
that starts after the current value.

60
00:03:08,867 --> 00:03:13,667
Expected time is linear in the number of
inputs because each iteration uses a

61
00:03:13,747 --> 00:03:17,627
fixed number of expected constant-time
dictionary operations.

62
00:03:17,827 --> 00:03:22,187
Auxiliary space is linear in the worst
case because every prefix can be

63
00:03:22,227 --> 00:03:22,787
different.

64
00:03:23,187 --> 00:03:27,187
Severe hash collisions can make an
individual lookup linear and the full

65
00:03:27,427 --> 00:03:28,467
algorithm quadratic.

66
00:03:28,787 --> 00:03:32,867
We also treat integer arithmetic and
hashing as constant-time here.

67
00:03:33,027 --> 00:03:37,107
Arbitrarily large Python integers
introduce a separate dependence on their

68
00:03:37,187 --> 00:03:37,907
bit length.

69
00:03:38,233 --> 00:03:43,113
An empty input has no nonempty segment, so
the answer is zero.

70
00:03:43,193 --> 00:03:47,913
Two zeros with a zero target produce three
valid segments: each single

71
00:03:47,993 --> 00:03:49,553
zero and the pair.

72
00:03:49,593 --> 00:03:53,993
A mixture of positive and negative values
tests whether the approach depends on an

73
00:03:54,073 --> 00:03:55,793
invalid window assumption.

74
00:03:55,833 --> 00:03:59,993
In an interview, explain what each
dictionary value means before you write

75
00:04:00,033 --> 00:04:00,846
the loop.

76
00:04:00,900 --> 00:04:05,193
That explanation makes the initialization
and the count-before-insert order much

77
00:04:05,273 --> 00:04:06,793
easier to justify.
