All language subtitles for 006 Thinking About Our Optimal Solution_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,460 --> 00:00:05,560 So now we reach that crucial step where we ask, can we optimize our solution? 2 00:00:06,530 --> 00:00:13,700 Going back to our code here, we know that we have a time complexity of of x squared and a space complexity 3 00:00:13,700 --> 00:00:14,480 of of one. 4 00:00:15,480 --> 00:00:18,090 The end squared is formed by our double for loop. 5 00:00:18,090 --> 00:00:24,030 And when we did this in our first question, we were thinking of ways to increase our space in order 6 00:00:24,030 --> 00:00:25,320 to decrease our time. 7 00:00:25,470 --> 00:00:27,660 But we need to rationally think about this. 8 00:00:27,660 --> 00:00:30,510 Is that same approach going to help us here? 9 00:00:30,510 --> 00:00:35,820 And this is a very important thing because we need to actually think about what this question is asking 10 00:00:35,820 --> 00:00:38,940 us and how our solution attempts to solve it. 11 00:00:39,480 --> 00:00:40,860 What are we doing? 12 00:00:41,220 --> 00:00:48,420 We are storing the max area that we've come across so far of every area that we calculate using P one 13 00:00:48,420 --> 00:00:49,140 and P two. 14 00:00:49,680 --> 00:00:57,960 So this double for loop does not separate because we need p one and p two together in order to figure 15 00:00:57,960 --> 00:00:58,740 out the area. 16 00:00:58,740 --> 00:01:04,410 And that's why we have no step in between these two for loops, which means that there's actually no 17 00:01:04,410 --> 00:01:06,990 additional caching that can happen for p one. 18 00:01:07,290 --> 00:01:10,170 This first for loop, what are the second for loop? 19 00:01:10,170 --> 00:01:15,240 Is there anything we can do with memory in order to improve this process or even eliminate this for 20 00:01:15,240 --> 00:01:16,140 loop as a whole? 21 00:01:16,790 --> 00:01:23,180 Well, what we do is we calculate the height and width using these two P's, and then we get the area. 22 00:01:23,360 --> 00:01:29,150 Then we essentially just check against the max error we've got so far and we see if we need to update 23 00:01:29,150 --> 00:01:29,420 it. 24 00:01:30,140 --> 00:01:36,560 But there really is nothing here that we're doing that would consume more memory because the moment 25 00:01:36,560 --> 00:01:40,610 that we're done with this area, we don't need it anymore. 26 00:01:40,790 --> 00:01:45,180 If we needed it and it was greater than what we had, then we would have stored it already. 27 00:01:45,200 --> 00:01:52,640 But there's nothing here that we can do in order to scale our memory, in order to improve this process. 28 00:01:52,640 --> 00:01:55,640 So what we did last time is not going to work here. 29 00:01:55,820 --> 00:02:00,320 And that's a very important thing, because we need to really rationally think about this problem. 30 00:02:00,800 --> 00:02:07,430 So what we do need to do that can improve this is actually learn a new technique which is known as shifting 31 00:02:07,430 --> 00:02:08,050 pointers. 32 00:02:08,060 --> 00:02:09,650 And let me show you what that is. 33 00:02:10,490 --> 00:02:17,210 So here I have generated a new test case for us and I switched the pointers from P one and p two to 34 00:02:17,210 --> 00:02:20,180 A and B to better match what we have in our formula. 35 00:02:20,570 --> 00:02:23,180 And I want us to really think deeply. 36 00:02:23,420 --> 00:02:27,620 Now, this new test case will help us understand this new concept. 37 00:02:27,620 --> 00:02:34,250 But I also want to build upon the knowledge that we've gained so far from both our previous array question, 38 00:02:34,250 --> 00:02:38,930 as well as what we've just done with our two pointers in our non optimal solution. 39 00:02:39,630 --> 00:02:42,660 Let's first think back to our original array question. 40 00:02:42,660 --> 00:02:43,810 The very first one we did. 41 00:02:43,830 --> 00:02:47,490 If you remember, we were given some target value. 42 00:02:47,700 --> 00:02:51,000 Let's say using the same array, that value was 13. 43 00:02:52,030 --> 00:02:58,090 We know that when our first pointer is pointing at this value and our second pointer is pointing at 44 00:02:58,090 --> 00:03:01,090 this value, four and nine, add together to give us 13. 45 00:03:01,090 --> 00:03:02,890 And there we had our solution. 46 00:03:03,430 --> 00:03:09,850 The key difference here is that the distance between A and B didn't matter with that question. 47 00:03:11,060 --> 00:03:18,810 And B being on the opposite sides of the array has no impact on how we come to the solution for finding 48 00:03:18,810 --> 00:03:19,530 our target. 49 00:03:19,800 --> 00:03:24,540 In fact, this is easily demonstrated that if our target changed to be six. 50 00:03:25,370 --> 00:03:30,260 This means that in order for a solution to work, B needs to be in this place here. 51 00:03:31,170 --> 00:03:33,450 But this has no impact. 52 00:03:33,810 --> 00:03:38,670 And B, being anywhere in our array doesn't change how we find our target. 53 00:03:38,670 --> 00:03:42,090 But this is not the same case as our current question. 54 00:03:42,600 --> 00:03:48,990 We know that with our current question, the width has a direct impact on the actual area. 55 00:03:49,620 --> 00:03:56,610 We know that by looking at our equation, we can see immediately that the distance between A and B has 56 00:03:56,610 --> 00:04:03,300 a direct impact on the value of the area that comes out of it, because that's the nature of multiplication. 57 00:04:04,320 --> 00:04:09,540 The other thing we need to note, though, is that we don't really know whether or not it's increasing 58 00:04:09,540 --> 00:04:10,620 or decreasing. 59 00:04:10,620 --> 00:04:15,720 That will change this area outcome because it varies from a case to case basis. 60 00:04:15,750 --> 00:04:22,770 It's not a guarantee that as long as A and B are greater distance apart, then somehow our area goes 61 00:04:22,770 --> 00:04:23,100 up. 62 00:04:23,100 --> 00:04:23,970 That's not the case. 63 00:04:23,970 --> 00:04:30,270 We can actually see that directly in this current example, let's do the calculation for the area now 64 00:04:30,270 --> 00:04:32,310 using the placement of A and B right here. 65 00:04:32,610 --> 00:04:36,240 So the minimum between A and B is the minimum between four and nine. 66 00:04:36,240 --> 00:04:38,010 So four is the minimum value. 67 00:04:38,220 --> 00:04:43,220 As for B index minus eight index, that's five -0, which gives us five. 68 00:04:43,230 --> 00:04:45,690 So four times five gives us 20. 69 00:04:46,420 --> 00:04:49,270 But what happens if we move a forward buy one. 70 00:04:49,600 --> 00:04:52,840 So let's move forward and do a new calculation. 71 00:04:52,840 --> 00:04:54,970 We'll keep the 20 as a reference. 72 00:04:55,820 --> 00:04:57,460 Amos forward to eight. 73 00:04:57,470 --> 00:05:00,950 And now we have to recalculate the minimum between A and B. 74 00:05:00,950 --> 00:05:02,750 So eight and nine is eight. 75 00:05:03,510 --> 00:05:11,010 And B of I, which is five minus A, VI, which is one is five minus one, which gives us four, eight 76 00:05:11,010 --> 00:05:13,660 times four gives us 32. 77 00:05:13,680 --> 00:05:20,820 So automatically we can see that by shrinking the size of our width, we actually ended up with a larger 78 00:05:20,820 --> 00:05:21,530 area. 79 00:05:21,540 --> 00:05:28,020 So this leads us to our second point, which is that if we really think about what just happened, the 80 00:05:28,020 --> 00:05:35,190 outcome of the area is impacted by the lesser of the two values of A and B. 81 00:05:35,430 --> 00:05:42,840 We saw that by shrinking the distance between A and B, we somehow end up with a larger area, which 82 00:05:42,840 --> 00:05:52,320 tells us that the only value in this part of the equation that has some impact on the outcome of the 83 00:05:52,320 --> 00:05:54,600 area is the smaller value. 84 00:05:54,780 --> 00:05:57,030 So let's just go back to what we had before. 85 00:05:58,010 --> 00:06:04,910 We saw earlier what happens if we were to move our smaller value by increasing it? 86 00:06:04,910 --> 00:06:08,140 We saw a direct transformation where the area got bigger. 87 00:06:08,150 --> 00:06:09,680 But what about the larger value? 88 00:06:09,710 --> 00:06:10,850 What about B? 89 00:06:11,210 --> 00:06:13,240 Let's say we move B inwards. 90 00:06:13,250 --> 00:06:15,770 What are the only possibilities that could happen for B? 91 00:06:15,980 --> 00:06:18,800 Well, be either gets larger or it gets smaller. 92 00:06:19,220 --> 00:06:20,630 So let's explore these two cases. 93 00:06:20,630 --> 00:06:22,910 Let's say B got larger first. 94 00:06:22,910 --> 00:06:25,520 So instead of three, just imagine it was 12. 95 00:06:26,360 --> 00:06:30,770 When we were to plug this into our error calculation, what happens to the myth? 96 00:06:30,770 --> 00:06:36,680 It's still going to be four against 12, which means that the number that comes out of this is still 97 00:06:36,680 --> 00:06:37,370 four. 98 00:06:37,790 --> 00:06:43,160 This means that even though B got bigger, there was no direct impact on our area calculation. 99 00:06:43,430 --> 00:06:47,660 So now we know that if the larger number gets larger, it doesn't matter. 100 00:06:47,660 --> 00:06:48,890 We don't even care about it. 101 00:06:49,040 --> 00:06:52,120 But what happens if the larger number gets smaller? 102 00:06:52,130 --> 00:06:58,430 So instead of let's say the 12, let's say we, it becomes the three in the place that it is right now. 103 00:06:58,550 --> 00:07:04,970 So now the calculation becomes the mean of four and three, and now our min changes are min becomes 104 00:07:04,970 --> 00:07:05,750 three. 105 00:07:06,500 --> 00:07:13,880 So here we actually see that our area would have been smaller in this case because not only did we decrease 106 00:07:13,880 --> 00:07:18,560 the value on this side, we've also decreased the value on this side. 107 00:07:18,830 --> 00:07:26,630 So we know that for the larger value, the only case is that by changing its placement, we have somehow 108 00:07:26,630 --> 00:07:28,460 decreased our total area. 109 00:07:28,460 --> 00:07:29,450 And we don't want that. 110 00:07:29,450 --> 00:07:34,940 We only want to figure out if we can maximize the area, if there's a way for us to increase the area 111 00:07:34,940 --> 00:07:35,690 possible. 112 00:07:35,780 --> 00:07:38,810 So now let's put these two ideas together. 113 00:07:38,960 --> 00:07:47,120 We know that the only thing that really matters in comes to moving either A or B is trying to move the 114 00:07:47,120 --> 00:07:52,340 smaller value forward and try and see if that somehow yields us a larger calculation. 115 00:07:52,340 --> 00:07:58,370 And that's going to be the real crux of this optimal solution and this new technique, which is known 116 00:07:58,370 --> 00:08:00,710 as the two shifting pointers. 117 00:08:01,360 --> 00:08:03,850 So what we want to do is exactly what we have here. 118 00:08:03,850 --> 00:08:09,430 We want to initialize two pointers, but we want to initialize them at the opposite sides of the array 119 00:08:09,430 --> 00:08:14,010 because what we're doing is we're maximizing this value right here. 120 00:08:14,020 --> 00:08:18,880 We want to get the biggest calculation possible for width because it does have an impact. 121 00:08:19,780 --> 00:08:26,680 And we also want to make sure that as we shift our values, there is no possible way where the values 122 00:08:26,680 --> 00:08:36,070 on the outskirts, meaning the opposing sides of our variables, could possibly give us a larger container. 123 00:08:36,070 --> 00:08:37,360 So we just got to make sure of that. 124 00:08:37,360 --> 00:08:40,270 And how we make sure of that is just by starting on the ends. 125 00:08:40,690 --> 00:08:46,570 So now our calculation becomes what is the smaller of these two values? 126 00:08:47,650 --> 00:08:50,920 We're going to calculate the area first of A and B. 127 00:08:50,920 --> 00:08:54,520 So we did this calculation already, but let's just do it again. 128 00:08:54,520 --> 00:09:01,210 But we're also going to keep track of the max area that we find so far very similar to what we did before. 129 00:09:01,600 --> 00:09:04,540 And let's just say it's Max and it starts at zero. 130 00:09:04,960 --> 00:09:11,650 So the minimum between four and nine is four and five -0, which is the indices is five. 131 00:09:11,650 --> 00:09:16,030 So four times five is 20 and that's bigger than our max area. 132 00:09:16,030 --> 00:09:17,200 So let's update that. 133 00:09:18,700 --> 00:09:20,770 Next, let's move a forward. 134 00:09:21,510 --> 00:09:26,430 Because A is the smaller value and as we mentioned, we only care about the smaller value. 135 00:09:27,670 --> 00:09:30,370 Amos forward and now we get the men between eight and nine. 136 00:09:30,370 --> 00:09:32,440 So this value is now eight. 137 00:09:33,240 --> 00:09:36,870 And the distance is five minus one, which is four. 138 00:09:37,260 --> 00:09:39,540 Eight times four is 32. 139 00:09:39,960 --> 00:09:42,300 And we have a new max area of 32. 140 00:09:43,290 --> 00:09:47,070 Now we have to shift the smaller over once more. 141 00:09:47,070 --> 00:09:50,160 So A comes over and we do our calculation. 142 00:09:50,160 --> 00:09:55,920 Once again, those min between one and nine is one, five minus two is three. 143 00:09:56,040 --> 00:09:57,810 The area here is three. 144 00:09:58,200 --> 00:09:59,670 Three is not bigger than 32. 145 00:09:59,670 --> 00:10:04,890 So nothing happens in terms of our max area, but we shift our values over again. 146 00:10:05,760 --> 00:10:10,320 And this time we do our calculation the exact same as we did last time. 147 00:10:10,770 --> 00:10:14,070 The men between two and nine is two. 148 00:10:14,100 --> 00:10:17,680 The distance is five minus three, which is two. 149 00:10:17,700 --> 00:10:22,440 So this gives us a area of four for still not larger than 32. 150 00:10:22,920 --> 00:10:28,530 So we do our a shift again, which is because it's the smaller number. 151 00:10:29,910 --> 00:10:37,470 And now the men between three and nine is three, B and A is five minus four, which is one. 152 00:10:37,470 --> 00:10:41,300 So once again, we get another container area of three. 153 00:10:41,310 --> 00:10:46,050 Three is still smaller than 32, so our max area now is done. 154 00:10:46,050 --> 00:10:51,450 We have no more room for any of our pointers to move because they've just come up against each other. 155 00:10:51,630 --> 00:10:53,280 So now we have our answer. 156 00:10:53,280 --> 00:10:55,770 32 is our maximum area. 157 00:10:55,770 --> 00:11:01,260 The other thing that will notice is that we've also only touched each element once. 158 00:11:01,830 --> 00:11:08,940 This means that we've managed to bring our time complexity down to zero of RN, which is a drastic improvement 159 00:11:08,940 --> 00:11:10,050 from O of UN squared. 160 00:11:10,380 --> 00:11:17,100 Every element will only be operated on once and then after that we are definitely moving one of our 161 00:11:17,100 --> 00:11:17,790 two pointers. 162 00:11:17,790 --> 00:11:23,940 We don't know which one, but one of them is moving, which means that at most we performed PN operations 163 00:11:23,940 --> 00:11:24,810 on this array. 164 00:11:24,990 --> 00:11:27,990 So this is the thing about two pointers. 165 00:11:28,230 --> 00:11:35,850 The main key with the two pointers is that we have to think about how we move these two pointers when 166 00:11:35,850 --> 00:11:37,800 it's considered done moving. 167 00:11:37,800 --> 00:11:43,650 And also what is the rationale between determining which of them to move in the first place? 168 00:11:43,650 --> 00:11:50,520 We were able to figure this out because we were able to see from our area calculation that the minimum 169 00:11:50,520 --> 00:11:59,070 value between these two was the only one that actually directly had an impact on an area getting bigger. 170 00:11:59,190 --> 00:12:04,710 The other thing was that we were able to see that the width had some factors to do with it because in 171 00:12:04,710 --> 00:12:09,240 some ways when the width changes, these two values must change. 172 00:12:09,240 --> 00:12:10,710 One of them has to change. 173 00:12:10,710 --> 00:12:13,590 So we just had to figure out which one of them to change. 174 00:12:13,590 --> 00:12:22,470 And by looking at this minimum value and how it affects the area, we could see that by moving the minimum 175 00:12:22,470 --> 00:12:22,920 value. 176 00:12:22,920 --> 00:12:26,520 That was how we could possibly end up with a larger area. 177 00:12:26,730 --> 00:12:34,350 So then the optimization just became, okay, let's shrink our width on every operation, but then conditionally, 178 00:12:34,350 --> 00:12:38,190 based on which value was smaller, would be the one that we shrunk. 179 00:12:38,190 --> 00:12:43,410 And along the way we just calculate the area and store the max area the way that we've been doing before. 180 00:12:43,680 --> 00:12:50,910 So I know that a lot of that is really new knowledge and this process of breaking down this equation 181 00:12:50,910 --> 00:12:57,840 into the rational steps to do this is going to be one of the key things in critical thinking that we 182 00:12:57,840 --> 00:13:05,670 need to work on and really thinking deeply about these things that seem at first very basic. 183 00:13:06,550 --> 00:13:13,180 Because within them lie the chance for optimization when combined with these technical tricks such as 184 00:13:13,180 --> 00:13:16,170 this two pointer style of problem solving. 185 00:13:16,180 --> 00:13:19,360 And this course is really going to focus on these two things. 186 00:13:19,360 --> 00:13:24,520 We're going to learn some of these technical tools that we just saw with the two pointers, but also 187 00:13:24,520 --> 00:13:32,110 how do we build up a mental framework so that we can rationally break down what the rules are and how 188 00:13:32,110 --> 00:13:36,310 we can best utilize these tools in order to deliver us an optimal solution. 189 00:13:36,670 --> 00:13:41,530 So what I want you to do now is take what we've just seen and try and code it yourself. 190 00:13:41,530 --> 00:13:45,940 It's going to be a little different from what we did with the four loops, but it's still good to give 191 00:13:45,940 --> 00:13:48,910 it a try if you can't figure out how to do it. 192 00:13:48,910 --> 00:13:49,720 No problem. 193 00:13:49,720 --> 00:13:52,000 That's what we're going to do in the next video. 18890

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.