1
00:00:00,000 --> 00:00:04,000
Dynamic programming stores answers to
subproblems that recur.

2
00:00:04,320 --> 00:00:08,480
Start by defining what one answer means
before you allocate a table.

3
00:00:08,720 --> 00:00:13,600
Our example selects nonadjacent rewards
from four, nine, three,

4
00:00:13,760 --> 00:00:15,000
seven, and six.

5
00:00:15,280 --> 00:00:17,120
You may select no positions.

6
00:00:17,360 --> 00:00:21,760
We will derive a recurrence, expose
repeated recursive work, and compute the

7
00:00:21,840 --> 00:00:24,720
same answer with a table and then two
variables.

8
00:00:25,033 --> 00:00:29,793
Skip the last position and every choice
comes from the preceding prefix.

9
00:00:29,833 --> 00:00:34,553
Take the last position and its neighbor
must be absent, so combine its reward with

10
00:00:34,633 --> 00:00:37,473
the prefix that ends two positions
earlier.

11
00:00:37,513 --> 00:00:39,873
Choose the larger of those totals.

12
00:00:39,913 --> 00:00:43,913
Every valid selection belongs to one of
these cases, which makes the recurrence

13
00:00:43,993 --> 00:00:45,033
complete.

14
00:00:45,267 --> 00:00:49,187
Entry d p of i stores the best total from
the first i

15
00:00:49,787 --> 00:00:50,440
positions.

16
00:00:50,494 --> 00:00:54,920
The zero entry is zero because an empty
prefix has no reward.

17
00:00:54,974 --> 00:00:59,027
The one-position entry is the greater of
zero and the first reward because

18
00:00:59,107 --> 00:01:01,160
selecting nothing is allowed.

19
00:01:01,214 --> 00:01:05,347
For later entries, compare the previous
optimum with the optimum two positions

20
00:01:05,427 --> 00:01:07,747
back plus the current reward.

21
00:01:07,967 --> 00:01:12,047
The recursive function handles the empty
and single-position prefixes

22
00:01:12,127 --> 00:01:13,167
first.

23
00:01:13,247 --> 00:01:17,527
These base cases stop the calls before an
invalid position is read.

24
00:01:17,567 --> 00:01:22,127
The two recursive branches compute the
skip and take choices, and the return

25
00:01:22,207 --> 00:01:23,860
selects their maximum.

26
00:01:23,914 --> 00:01:28,047
This definition is correct, but it does
not remember an answer when another branch

27
00:01:28,287 --> 00:01:30,127
requests the same prefix.

28
00:01:30,367 --> 00:01:35,220
A call for five positions asks for
prefixes of four and three.

29
00:01:35,274 --> 00:01:40,087
The four-position call also asks for
three, so that answer is recomputed.

30
00:01:40,127 --> 00:01:44,447
The call count grows exponentially under
this repeated branching, although the live

31
00:01:44,567 --> 00:01:46,247
call depth is only linear.

32
00:01:46,287 --> 00:01:49,687
There are only n plus one distinct prefix
states.

33
00:01:49,727 --> 00:01:54,287
Store each answer once instead of
expanding every repeated request.

34
00:01:54,533 --> 00:01:59,333
For reward four, skipping gives zero and
taking gives four, so

35
00:01:59,573 --> 00:02:00,693
keep four.

36
00:02:00,773 --> 00:02:05,653
For reward nine, skipping gives four and
taking gives nine, so keep

37
00:02:05,733 --> 00:02:06,413
nine.

38
00:02:06,453 --> 00:02:11,013
For reward three, skipping keeps nine and
taking combines three with the

39
00:02:11,093 --> 00:02:13,173
earlier four, giving seven.

40
00:02:13,253 --> 00:02:14,173
Keep nine.

41
00:02:14,213 --> 00:02:18,773
Each calculation uses only prefix answers
that are already complete.

42
00:02:19,000 --> 00:02:22,200
For reward seven, skipping keeps nine.

43
00:02:22,400 --> 00:02:26,440
Taking it adds seven to the best two-
position total nine, giving

44
00:02:26,520 --> 00:02:27,600
sixteen.

45
00:02:27,640 --> 00:02:30,880
For reward six, skipping keeps sixteen.

46
00:02:30,920 --> 00:02:35,000
Taking it adds six to the best three-
position total nine, giving

47
00:02:35,040 --> 00:02:35,987
fifteen.

48
00:02:36,013 --> 00:02:38,160
The maximum remains sixteen.

49
00:02:38,200 --> 00:02:42,840
The selected rewards nine and seven occupy
nonadjacent positions.

50
00:02:43,067 --> 00:02:47,067
The table starts with a zero entry for
every prefix length.

51
00:02:47,107 --> 00:02:51,040
The loop fills entries from one through
the complete input length.

52
00:02:51,094 --> 00:02:54,960
The previous two entries supply the skip
and take candidates.

53
00:02:55,014 --> 00:02:59,627
By induction, both candidates use correct
smaller answers, so their maximum is

54
00:02:59,707 --> 00:03:01,520
correct for this prefix.

55
00:03:01,654 --> 00:03:05,547
Return the complete table when you want to
inspect all state answers.

56
00:03:05,767 --> 00:03:09,660
The recurrence needs only two preceding
prefix totals.

57
00:03:09,874 --> 00:03:11,567
Initialize both to zero.

58
00:03:11,607 --> 00:03:15,500
Each new reward produces the next optimum
from those totals.

59
00:03:15,554 --> 00:03:19,687
The simultaneous assignment then shifts
the states forward without losing the

60
00:03:19,767 --> 00:03:22,140
older value before the calculation.

61
00:03:22,274 --> 00:03:25,207
Return the latest total after the final
reward.

62
00:03:25,287 --> 00:03:29,367
This version returns the value but does
not retain chosen positions.

63
00:03:29,600 --> 00:03:34,320
Direct recursion has exponential time,
with big O of two to the n as a

64
00:03:34,400 --> 00:03:37,760
valid upper bound, and linear call-stack
space.

65
00:03:37,840 --> 00:03:42,400
The table computes n transitions in linear
time and stores n plus one

66
00:03:42,680 --> 00:03:43,360
answers.

67
00:03:43,440 --> 00:03:48,000
The two-variable version keeps linear time
and constant auxiliary space.

68
00:03:48,120 --> 00:03:53,120
These bounds assume constant-cost integer
arithmetic; very large Python integers add

69
00:03:53,200 --> 00:03:54,720
a bit-length cost.

70
00:03:55,033 --> 00:03:57,593
An empty input returns zero.

71
00:03:57,713 --> 00:04:01,913
An all-negative input also returns zero
because selecting nothing is

72
00:04:01,993 --> 00:04:02,913
permitted.

73
00:04:02,953 --> 00:04:07,073
A request for chosen positions requires
more than the final total.

74
00:04:07,113 --> 00:04:11,513
Retain enough state to reconstruct the
decisions, or explain another

75
00:04:11,593 --> 00:04:12,993
reconstruction method.

76
00:04:13,033 --> 00:04:17,673
Memoization can also store recursive
prefix answers, but it keeps both a

77
00:04:17,753 --> 00:04:20,073
cache and the recursive call depth.
