1
00:00:00,240 --> 00:00:01,350
Welcome back, everyone.

2
00:00:01,710 --> 00:00:06,840
In the last video, I mentioned that there is a further optimization we can make when we realized that

3
00:00:06,840 --> 00:00:13,110
we're only leveraging the last two stored values inside of our DP as we iterate through AI to end.

4
00:00:13,590 --> 00:00:19,350
What we can do now is just replace this deep scaling array with two variables instead.

5
00:00:19,860 --> 00:00:22,860
So to start, I'm just going to erase all of the code.

6
00:00:22,860 --> 00:00:27,390
That is irrelevant because this is the part of it that we're going to rewrite.

7
00:00:27,720 --> 00:00:30,980
I'm also going to get rid of the space and time complexity here.

8
00:00:30,990 --> 00:00:32,580
Just remember that it was O of end.

9
00:00:33,420 --> 00:00:39,750
Next, what we're going to do is just think about the two variables we need to initialize when we start

10
00:00:39,750 --> 00:00:40,590
this value.

11
00:00:41,100 --> 00:00:45,120
DP one, which is what I'm going to call the first variable.

12
00:00:46,080 --> 00:00:53,070
Is going to actually be equal to cost at zero because this is the starting variable that we need to

13
00:00:53,070 --> 00:00:53,470
store.

14
00:00:54,240 --> 00:01:00,320
Secondly, then you can assume that DP two is going to be equal to cost at one.

15
00:01:01,110 --> 00:01:05,760
Now that we've initialized our two DP variables, we can just run through the iteration like we did

16
00:01:05,760 --> 00:01:07,080
before here.

17
00:01:07,080 --> 00:01:12,900
We're going to use the same for loop, except we're going to initialize at two instead of zero, because

18
00:01:12,900 --> 00:01:20,040
essentially what we did when we set up one in two was we cast the variables as the first two instances

19
00:01:20,040 --> 00:01:22,320
of I we don't need to generate DP's for them.

20
00:01:22,330 --> 00:01:24,970
That's what these two lines have literally done for us.

21
00:01:25,350 --> 00:01:29,070
So with AI starting at two, we're going to go up to end the same way.

22
00:01:29,490 --> 00:01:36,180
And I plus plus and now inside, what we want to do is generate the current value for AI.

23
00:01:36,750 --> 00:01:40,860
So here it's going to utilize the two values at our DP.

24
00:01:41,100 --> 00:01:45,510
So it's going to be cost at AI plus math dot min.

25
00:01:48,800 --> 00:01:49,700
DP won.

26
00:01:50,570 --> 00:01:51,590
And DP to.

27
00:01:53,640 --> 00:02:00,660
Once we've generated and figured out what this current value is for, I, we need to replace our deep

28
00:02:00,690 --> 00:02:01,120
one.

29
00:02:01,800 --> 00:02:08,760
So first, what we're going to do is say that DPE one is equal to DPE two, because as we know from

30
00:02:08,760 --> 00:02:16,350
the tree, when we use DPE one and two to generate the current value, we're going to use these two

31
00:02:16,350 --> 00:02:21,150
now to generate this value, which means that this DPE one value is no longer needed.

32
00:02:21,380 --> 00:02:24,510
Instead, this value is going to be the new DPE one.

33
00:02:24,690 --> 00:02:26,130
This value we just generate.

34
00:02:26,130 --> 00:02:30,230
It is the new DPE two or just keeping track of the two most recent values.

35
00:02:30,600 --> 00:02:38,460
So that's why DPE one is now equal to TPE two and two is equal to our current value.

36
00:02:39,510 --> 00:02:45,810
And that's all we need to do, just like that, we've generated and built up the same deep and of course,

37
00:02:45,810 --> 00:02:51,200
the final answer that we return is just going to be the men of these two values.

38
00:02:51,390 --> 00:02:56,700
So we're just going to return math to men between DPE one and deep two.

39
00:02:58,420 --> 00:03:02,770
Once again, the reason why this happens is because the last two values that we've sought should be

40
00:03:02,770 --> 00:03:07,300
the deep values for these last two variables inside of our staircase.

41
00:03:08,340 --> 00:03:09,940
And this is the optimization.

42
00:03:10,530 --> 00:03:17,130
Now, let's compare the space and time, if we look at our time complexity, first we notice that there's

43
00:03:17,130 --> 00:03:18,860
no real optimization here.

44
00:03:18,870 --> 00:03:23,940
We're still going to loop over the majority of N, but we're only going to do it once still.

45
00:03:24,270 --> 00:03:31,560
So here it's still O of N, but when we look at our space complexity, we have no scaling data structures

46
00:03:31,560 --> 00:03:32,010
anymore.

47
00:03:32,340 --> 00:03:39,120
Instead, we just initialize to static variables, which means that we ended up with an O of one space

48
00:03:39,120 --> 00:03:45,480
complexity, which is an amazing improvement over are even very first recursive solution.

49
00:03:45,630 --> 00:03:49,590
If you think back, they were both two to the end here.

50
00:03:49,590 --> 00:03:56,850
We've brought this down to of non-linear and of one that is incredible performance, constant space

51
00:03:56,850 --> 00:03:58,050
and linear time.

52
00:03:58,170 --> 00:04:00,210
That is one of the best possible solutions.

53
00:04:00,420 --> 00:04:06,600
And this is why dynamic programming is so powerful as an algorithmic paradigm.

54
00:04:06,900 --> 00:04:12,240
If you think about it, the very nature of the fact that we have to generate all the possible optional

55
00:04:12,240 --> 00:04:17,250
solutions that could exist for a given question and then pick the best one from amongst them, that

56
00:04:17,250 --> 00:04:21,210
by default sounds like a very computationally expensive endeavor.

57
00:04:21,510 --> 00:04:28,380
But through dynamic programming, we're able to bring that down to such a drastically performance solution.

58
00:04:29,260 --> 00:04:34,120
So here is the key that you have to remember, there are so many steps of dynamic programming, the

59
00:04:34,120 --> 00:04:39,220
first one begins with identifying the recurrence relation formula that you can then use to build the

60
00:04:39,220 --> 00:04:45,130
recursive function from the recursive function you want to build at top down and determine if there

61
00:04:45,130 --> 00:04:48,280
is an optimization using memorisation that can help you.

62
00:04:48,580 --> 00:04:55,090
Once you see that full top down solution, it's easy for you to extrapolate out the bottom up solution

63
00:04:55,270 --> 00:04:59,620
because you know that if you're going top down, there's definitely a way to go bottom up.

64
00:04:59,620 --> 00:05:04,660
As long as you analyze the correct base cases that you might have, most of the time you'll have these

65
00:05:04,660 --> 00:05:08,200
base cases and they'll help you think about that bottom up approach.

66
00:05:08,620 --> 00:05:13,990
Once you've understood what that is, you figure out the bottom up tree is and then you derive it ideally.

67
00:05:14,320 --> 00:05:22,030
Then you look at your DPI and see if there is any wasted usage of space inside of your scaling DPI.

68
00:05:22,300 --> 00:05:28,210
If there is, you reduce it to the parts that you only need and you're able to bring down to at least

69
00:05:28,210 --> 00:05:32,750
the next level of space complexity with our own notation.

70
00:05:33,580 --> 00:05:36,510
This is dynamic programming and this is the power of it.

71
00:05:37,270 --> 00:05:44,110
If this whole process seems a little bit still kind of vague to you, don't worry, we're going to practice

72
00:05:44,110 --> 00:05:44,850
a lot more.

73
00:05:45,130 --> 00:05:49,720
So just keep this whole step by step, stage by stage process in your mind.

74
00:05:50,260 --> 00:05:57,640
And always remember that with your interviews, you may not require going through the entire dynamic

75
00:05:57,640 --> 00:06:01,470
programming step to get to this final optimized solution.

76
00:06:01,750 --> 00:06:07,270
A lot of the times you can just finish the full top down solution with the memorization.

77
00:06:07,810 --> 00:06:12,840
The bottom up is kind of hard to get to within the tight time frame of an interview question.

78
00:06:13,120 --> 00:06:18,940
However, if you're able to articulate what that next step of going from top down to bottom up brings

79
00:06:18,940 --> 00:06:25,090
in terms of its benefits and even just understanding the process of going from that top down tree to

80
00:06:25,090 --> 00:06:30,310
this bottom up tree, if you're able to just articulate this stage of it, where you logically thinking

81
00:06:30,310 --> 00:06:36,640
about it but not coding it out, that is oftentimes enough for your interviewer to know that you could

82
00:06:36,640 --> 00:06:39,250
have solved the problem had you had more time.

83
00:06:39,400 --> 00:06:43,900
And that is often enough for them to pass you through the interview so deeply.

84
00:06:43,900 --> 00:06:49,300
Understand the process that we took to break down this dynamic programming problem and we'll get more

85
00:06:49,300 --> 00:06:51,250
practice with the next problem.
