1 00:00:08,516 --> 00:00:09,890 SRINI DEVADAS: Let's get started. 2 00:00:09,890 --> 00:00:13,240 So today, we're going to look at one of my favorite puzzles. 3 00:00:13,240 --> 00:00:17,260 I'll say right at the beginning, that the coding associated 4 00:00:17,260 --> 00:00:20,440 with the puzzle is fairly straightforward. 5 00:00:20,440 --> 00:00:24,940 But the analysis associated with this puzzle is quite intricate. 6 00:00:24,940 --> 00:00:27,100 And in particular, we're going to have 7 00:00:27,100 --> 00:00:30,970 to do worst-case analysis, which is really a good thing for you 8 00:00:30,970 --> 00:00:36,160 to learn about, because asymptotic analysis 9 00:00:36,160 --> 00:00:39,140 in algorithms is all about worst case. 10 00:00:39,140 --> 00:00:39,640 All right. 11 00:00:39,640 --> 00:00:40,990 So keep that in mind. 12 00:00:40,990 --> 00:00:45,130 When you try to come up with the answers to my questions, 13 00:00:45,130 --> 00:00:47,440 always keep in mind that I'm going 14 00:00:47,440 --> 00:00:52,120 to be asking for what happens in the worst case. 15 00:00:52,120 --> 00:00:55,070 In the context of our puzzle-- 16 00:00:55,070 --> 00:00:58,000 which is called the crystal ball puzzle, 17 00:00:58,000 --> 00:01:00,640 and it's fairly popular in literature, 18 00:01:00,640 --> 00:01:03,550 though I've generalized it a little bit-- 19 00:01:03,550 --> 00:01:06,550 you have a situation where you're 20 00:01:06,550 --> 00:01:10,690 supposed to figure out the hardness coefficient 21 00:01:10,690 --> 00:01:12,920 of a crystal ball. 22 00:01:12,920 --> 00:01:14,860 And in the generalized version, you 23 00:01:14,860 --> 00:01:17,770 have a set of identical crystal balls. 24 00:01:17,770 --> 00:01:21,380 So how do you find this hardness coefficient? 25 00:01:21,380 --> 00:01:28,410 Well, you go up to the Shanghai Tower that has 128 floors-- 26 00:01:28,410 --> 00:01:37,419 1 through 128-- and you drop this ball from say, floor 1. 27 00:01:37,419 --> 00:01:39,210 So just you're standing here and let's say, 28 00:01:39,210 --> 00:01:40,600 you just drop it from there. 29 00:01:40,600 --> 00:01:43,750 Or you go up to floor 2, and you drop the ball. 30 00:01:43,750 --> 00:01:47,560 And the hardness coefficient is defined 31 00:01:47,560 --> 00:01:53,080 as the highest floor from which you can drop the ball 32 00:01:53,080 --> 00:01:56,200 and the ball does not break. 33 00:01:56,200 --> 00:02:04,240 So what this means is if at floor f, 34 00:02:04,240 --> 00:02:15,490 the ball doesn't break, and it breaks at f plus 1, 35 00:02:15,490 --> 00:02:19,480 then the hardness coefficient is f. 36 00:02:19,480 --> 00:02:24,190 And you can assume a monotonicity property 37 00:02:24,190 --> 00:02:30,130 that says that if it did not break at floor f, then 38 00:02:30,130 --> 00:02:32,200 it won't break-- 39 00:02:32,200 --> 00:02:39,250 this implies that it won't break at any floor less than 40 00:02:39,250 --> 00:02:40,780 or equal to f. 41 00:02:40,780 --> 00:02:42,430 Which makes sense, right? 42 00:02:42,430 --> 00:02:44,660 And otherwise, it would be kind of complicated. 43 00:02:44,660 --> 00:02:45,680 It would be random. 44 00:02:45,680 --> 00:02:49,060 And we won't be able to do any interesting analysis. 45 00:02:49,060 --> 00:02:53,770 So the easy version of the puzzle, 46 00:02:53,770 --> 00:02:56,470 of course, which is not particularly interesting, 47 00:02:56,470 --> 00:03:02,090 is if you needed to get this exactly-- 48 00:03:02,090 --> 00:03:05,270 so your job depends on getting that number exactly right-- 49 00:03:05,270 --> 00:03:06,400 so there's no shortcuts. 50 00:03:06,400 --> 00:03:07,524 You've got to get it right. 51 00:03:07,524 --> 00:03:09,850 You can't be off by one either way. 52 00:03:09,850 --> 00:03:15,670 And you have only one ball. 53 00:03:15,670 --> 00:03:17,270 So you only have one ball. 54 00:03:17,270 --> 00:03:19,319 So what can you do if you only have one ball 55 00:03:19,319 --> 00:03:20,860 and you have to get it exactly right? 56 00:03:20,860 --> 00:03:22,969 AUDIENCE: Just try every floor. 57 00:03:22,969 --> 00:03:24,260 SRINI DEVADAS: Try every floor. 58 00:03:24,260 --> 00:03:24,760 Right. 59 00:03:24,760 --> 00:03:29,510 And but more important, try it from lower floors 60 00:03:29,510 --> 00:03:30,920 to higher floors, right? 61 00:03:30,920 --> 00:03:34,670 So you drop it at floor 1. 62 00:03:34,670 --> 00:03:42,900 So one ball, you drop from floor 1. 63 00:03:42,900 --> 00:03:46,460 And if it breaks, then your hardness coefficient 64 00:03:46,460 --> 00:03:50,760 is, you return 0. 65 00:03:50,760 --> 00:03:56,130 But then if you keep going and let's say, you drop off dot, 66 00:03:56,130 --> 00:03:57,420 dot, dot-- 67 00:03:57,420 --> 00:04:05,310 of from floor 65 and it breaks, and you've gone in order, 68 00:04:05,310 --> 00:04:09,630 then the hardness coefficient is 64. 69 00:04:09,630 --> 00:04:10,620 So that's easy enough. 70 00:04:10,620 --> 00:04:13,320 There's no optimization here. 71 00:04:13,320 --> 00:04:15,160 It's the only thing you can do. 72 00:04:15,160 --> 00:04:18,810 So that's why it's not particularly interesting. 73 00:04:18,810 --> 00:04:21,149 Now if you have identical crystal balls that 74 00:04:21,149 --> 00:04:23,940 are guaranteed, they were manufactured 75 00:04:23,940 --> 00:04:27,885 in exactly the same way, et cetera, et cetera, then 76 00:04:27,885 --> 00:04:29,760 obviously, things get a lot more interesting, 77 00:04:29,760 --> 00:04:32,460 because you can try to minimize something. 78 00:04:32,460 --> 00:04:34,230 And I'm going to write it over here, 79 00:04:34,230 --> 00:04:38,940 because I want you to keep this in mind throughout the lecture. 80 00:04:38,940 --> 00:04:48,870 Our goal here is to minimize the worst-case number 81 00:04:48,870 --> 00:04:52,980 of drops for our problem. 82 00:04:52,980 --> 00:04:58,440 And we want to solve the problem in the general case 83 00:04:58,440 --> 00:05:00,420 with d balls. 84 00:05:00,420 --> 00:05:02,190 And I should say, d identical balls. 85 00:05:05,650 --> 00:05:07,930 But more important, the reason I'm writing this up 86 00:05:07,930 --> 00:05:14,550 is, as I said before, we need to focus on the worst case. 87 00:05:14,550 --> 00:05:19,010 So it may be the case that for a given set of balls, 88 00:05:19,010 --> 00:05:23,520 your algorithm does very well, in the sense that the hardness 89 00:05:23,520 --> 00:05:25,820 coefficient happened to be 42. 90 00:05:25,820 --> 00:05:34,480 And you picked the floor as of 41 to begin with. 91 00:05:34,480 --> 00:05:36,727 And so you dropped from 41, and it doesn't break. 92 00:05:36,727 --> 00:05:38,560 And then you go to 42, and it doesn't break. 93 00:05:38,560 --> 00:05:41,170 And you go to 43, and it breaks. 94 00:05:41,170 --> 00:05:42,820 And you go, oh, I only needed two drops 95 00:05:42,820 --> 00:05:46,570 for this particular problem. 96 00:05:46,570 --> 00:05:50,920 But if the hardness coefficient happened to be 65 97 00:05:50,920 --> 00:05:53,410 and you started from floor 41, I mean 98 00:05:53,410 --> 00:05:56,500 you'd need 20-odd number of drops. 99 00:05:56,500 --> 00:05:59,590 So you have to be careful in that you 100 00:05:59,590 --> 00:06:02,200 have to analyze whatever strategy you come up 101 00:06:02,200 --> 00:06:07,030 with in the case where the hardness coefficient could 102 00:06:07,030 --> 00:06:08,590 be pretty much anything. 103 00:06:08,590 --> 00:06:12,640 I mean it could be 0 or it could be 128 in our Shanghai Tower 104 00:06:12,640 --> 00:06:14,560 example. 105 00:06:14,560 --> 00:06:17,770 So the first interesting question 106 00:06:17,770 --> 00:06:23,930 is, what happens when I have two identical balls? 107 00:06:23,930 --> 00:06:30,010 So, oh, before we do that, according 108 00:06:30,010 --> 00:06:37,330 to my metric here, what is the worst-case number of drops 109 00:06:37,330 --> 00:06:39,530 for one ball? 110 00:06:39,530 --> 00:06:42,180 What is the worst-case number of drops for one ball? 111 00:06:44,750 --> 00:06:46,690 Give me a number. 112 00:06:46,690 --> 00:06:48,762 Someone else. 113 00:06:48,762 --> 00:06:49,470 Yeah, back there. 114 00:06:49,470 --> 00:06:51,820 AUDIENCE: 128. 115 00:06:51,820 --> 00:06:54,670 SRINI DEVADAS: 128, exactly right. 116 00:06:54,670 --> 00:06:56,230 So it's 128, because I could have 117 00:06:56,230 --> 00:06:59,590 had a ball that doesn't break from the 128 floor. 118 00:06:59,590 --> 00:07:01,150 I don't know that to begin with. 119 00:07:01,150 --> 00:07:02,770 I have to start with floor 1. 120 00:07:02,770 --> 00:07:06,100 And I got to go 1, 2, 3, 4, et cetera, right? 121 00:07:06,100 --> 00:07:08,080 So the worst case for one ball is 128. 122 00:07:08,080 --> 00:07:10,870 And you can't do better than that. 123 00:07:10,870 --> 00:07:15,070 But of course, it gets more interesting when d is large. 124 00:07:15,070 --> 00:07:19,030 So our goal now is to discover an algorithm that 125 00:07:19,030 --> 00:07:22,640 is going to minimize the worst-case number of drops 126 00:07:22,640 --> 00:07:25,060 for d equals 2. 127 00:07:25,060 --> 00:07:28,210 That's our current goal, right? 128 00:07:28,210 --> 00:07:32,470 And then we'll look at more, even more general things. 129 00:07:32,470 --> 00:07:36,120 So how would you do things if you had d equals 2? 130 00:07:39,400 --> 00:07:43,510 Just, it doesn't matter if you don't get our algorithm right. 131 00:07:43,510 --> 00:07:44,330 We'll analyze it. 132 00:07:44,330 --> 00:07:45,560 You propose something. 133 00:07:45,560 --> 00:07:46,509 We'll analyze it. 134 00:07:46,509 --> 00:07:48,550 And then we'll decide if we can do better or not. 135 00:07:48,550 --> 00:07:48,700 Yeah. 136 00:07:48,700 --> 00:07:49,525 Go ahead. 137 00:07:49,525 --> 00:07:52,500 AUDIENCE: Drop the ball from the halfway point, say 64. 138 00:07:52,500 --> 00:07:54,855 If it breaks there, then you can use the bottom half 139 00:07:54,855 --> 00:07:55,730 of what we are using. 140 00:07:55,730 --> 00:07:56,640 SRINI DEVADAS: Use the bottom half. 141 00:07:56,640 --> 00:07:56,970 Excellent. 142 00:07:56,970 --> 00:07:57,678 What's your name? 143 00:07:57,678 --> 00:07:58,810 AUDIENCE: Dhaman. 144 00:07:58,810 --> 00:07:59,120 SRINI DEVADAS: Sorry. 145 00:07:59,120 --> 00:07:59,850 AUDIENCE: Dhaman. 146 00:07:59,850 --> 00:08:00,910 SRINI DEVADAS: Dhaman. 147 00:08:00,910 --> 00:08:06,570 So Dhaman says that we should do binary search, which 148 00:08:06,570 --> 00:08:11,890 is very intuitive and kind of makes sense 149 00:08:11,890 --> 00:08:15,040 in a lot of context, including this one. 150 00:08:15,040 --> 00:08:21,970 And so let's drop from 64. 151 00:08:21,970 --> 00:08:25,450 And the reason she picked that is because 128 divided by 2 152 00:08:25,450 --> 00:08:26,710 is 64. 153 00:08:26,710 --> 00:08:39,340 And if it breaks, then you need to look at the interval, 1, 63 154 00:08:39,340 --> 00:08:41,890 because if it-- 155 00:08:41,890 --> 00:08:42,439 I'm sorry. 156 00:08:42,439 --> 00:08:43,230 Yeah, that's right. 157 00:08:43,230 --> 00:08:46,690 If it breaks, you need to look at the interval 1, 63 158 00:08:46,690 --> 00:08:47,790 with a second ball. 159 00:08:47,790 --> 00:08:50,350 I mean that first ball is gone. 160 00:08:50,350 --> 00:08:53,510 It's shards of crystal or whatever you want to call it. 161 00:08:53,510 --> 00:08:55,120 So you just have the second ball. 162 00:08:55,120 --> 00:08:58,860 And so now at this point, you have no choice. 163 00:08:58,860 --> 00:09:00,940 You only have one ball left. 164 00:09:00,940 --> 00:09:02,860 As I mentioned, your job depends on getting 165 00:09:02,860 --> 00:09:05,350 that hardness coefficient right, so you can only 166 00:09:05,350 --> 00:09:09,910 use that first algorithm in terms of starting from 1 167 00:09:09,910 --> 00:09:11,770 and going all the way to 63. 168 00:09:11,770 --> 00:09:13,840 And so what is the worst-case number 169 00:09:13,840 --> 00:09:17,560 of drops given that Dhaman picked the midpoint 170 00:09:17,560 --> 00:09:19,210 for this algorithm? 171 00:09:19,210 --> 00:09:21,880 Someone else. 172 00:09:21,880 --> 00:09:22,380 Yeah. 173 00:09:22,380 --> 00:09:22,880 Go ahead. 174 00:09:23,380 --> 00:09:25,860 AUDIENCE: It's 63. 175 00:09:25,860 --> 00:09:29,190 SRINI DEVADAS: It's 63. 176 00:09:29,190 --> 00:09:31,770 No actually, you're off by 1. 177 00:09:31,770 --> 00:09:36,780 Because I did drop the ball here, so close. 178 00:09:36,780 --> 00:09:42,400 So the first ball was dropped once, one drop. 179 00:09:42,400 --> 00:09:48,090 And this one, if in fact, the hardness coefficient was 64, 180 00:09:48,090 --> 00:09:52,200 you drop the second ball 63 times. 181 00:09:52,200 --> 00:09:56,960 And that would be the worst case in this instance. 182 00:09:56,960 --> 00:10:00,300 Now, I've skipped over one little thing. 183 00:10:00,300 --> 00:10:02,010 The fact that this is the midpoint 184 00:10:02,010 --> 00:10:05,160 implies that this is, in fact, the correct answer. 185 00:10:05,160 --> 00:10:06,960 But we do have to analyze. 186 00:10:06,960 --> 00:10:09,000 I mean remember, you know, when you do analysis, 187 00:10:09,000 --> 00:10:11,920 you do have to cover the entire spectrum here. 188 00:10:11,920 --> 00:10:18,030 So you do have to look at the case where the drop from 64 189 00:10:18,030 --> 00:10:18,960 does not break. 190 00:10:22,680 --> 00:10:30,150 And in that case, you're looking at 65 through 128. 191 00:10:30,150 --> 00:10:33,330 But you do have two balls for this. 192 00:10:33,330 --> 00:10:34,410 It didn't break. 193 00:10:34,410 --> 00:10:37,110 So you still have two balls for that. 194 00:10:37,110 --> 00:10:40,440 And so you can imagine that, now that you have two balls 195 00:10:40,440 --> 00:10:43,860 and you have a small interval, that you're 196 00:10:43,860 --> 00:10:46,230 going to be able to do something like 32 for that. 197 00:10:46,230 --> 00:10:48,162 The bottom line is, you don't even need to. 198 00:10:48,162 --> 00:10:49,620 You can get a little bit lazy here. 199 00:10:49,620 --> 00:10:51,786 You don't really need to analyze what the number is, 200 00:10:51,786 --> 00:10:53,870 because you know that this number, 63 plus 1, 201 00:10:53,870 --> 00:10:55,230 is larger than that-- 202 00:10:55,230 --> 00:10:56,984 because you wanted worst case, right? 203 00:10:56,984 --> 00:10:58,650 So this is the kind of thing that you'll 204 00:10:58,650 --> 00:11:01,440 have to do when you do algorithmic analysis. 205 00:11:01,440 --> 00:11:05,820 Maybe not exactly in a puzzle situation like this, 206 00:11:05,820 --> 00:11:08,730 but you have to look at all the cases. 207 00:11:08,730 --> 00:11:11,879 And you have to find kind of the max of all the cases. 208 00:11:11,879 --> 00:11:13,170 So is this the best you can do? 209 00:11:15,510 --> 00:11:16,010 Yeah. 210 00:11:16,010 --> 00:11:16,809 Go ahead. 211 00:11:16,809 --> 00:11:21,549 AUDIENCE: You could break the 128 into smaller sections using 212 00:11:21,549 --> 00:11:22,298 the first ball. 213 00:11:22,298 --> 00:11:24,294 So instead of breaking it in half, 214 00:11:24,294 --> 00:11:26,496 you could do it every 16 floors. 215 00:11:26,496 --> 00:11:27,787 SRINI DEVADAS: Every 16 floors. 216 00:11:27,787 --> 00:11:28,287 Yeah. 217 00:11:28,287 --> 00:11:29,783 AUDIENCE: Starting from the bottom, 218 00:11:29,783 --> 00:11:32,200 and then once the first ball breaks, 219 00:11:32,200 --> 00:11:35,490 then you have a smaller interval to test with the second ball. 220 00:11:35,490 --> 00:11:40,240 So let's say it breaks at the 32nd. 221 00:11:40,240 --> 00:11:44,730 Then you have to only test for 16 to 32, instead of 1 to 32. 222 00:11:44,730 --> 00:11:45,980 SRINI DEVADAS: That's correct. 223 00:11:45,980 --> 00:11:46,500 Yeah. 224 00:11:46,500 --> 00:11:47,550 So it shows how you can do it-- 225 00:11:47,550 --> 00:11:49,200 I mean, this is, obviously there's not 226 00:11:49,200 --> 00:11:51,450 going to be a whole lot of optimization 227 00:11:51,450 --> 00:11:53,640 here with d equals 2. 228 00:11:53,640 --> 00:11:56,670 But you'd be surprised as to how much better you 229 00:11:56,670 --> 00:12:00,030 can do than 64, which is obviously a big step 230 00:12:00,030 --> 00:12:03,930 forward from 128. 231 00:12:03,930 --> 00:12:07,690 But you can go a few more steps as well. 232 00:12:07,690 --> 00:12:09,594 And so what was your name? 233 00:12:09,594 --> 00:12:10,380 AUDIENCE: Lucy. 234 00:12:10,380 --> 00:12:11,213 SRINI DEVADAS: Lucy. 235 00:12:11,213 --> 00:12:13,460 So Lucy has it right. 236 00:12:13,460 --> 00:12:18,420 It's a similar idea, except you don't necessarily 237 00:12:18,420 --> 00:12:22,280 need to do the intuitive binary search. 238 00:12:22,280 --> 00:12:26,220 It's actually kind of different from that because of the way 239 00:12:26,220 --> 00:12:28,020 this problem is structured. 240 00:12:28,020 --> 00:12:34,860 And what her idea is is that we'll look at something 241 00:12:34,860 --> 00:12:36,730 smaller, a smaller interval. 242 00:12:41,450 --> 00:12:47,520 And she picked 16, so let's go 16, 32 is our algorithm. 243 00:12:47,520 --> 00:12:52,940 It's something that goes 16, 32, 48, 64, dot, dot, dot. 244 00:12:52,940 --> 00:12:57,500 And maybe gets up all the way to I guess, yeah 128 and 16. 245 00:13:00,582 --> 00:13:03,270 So 128 is a multiple of 16, right? 246 00:13:03,270 --> 00:13:05,990 Some math person make sure I'm right. 247 00:13:09,290 --> 00:13:10,290 So what happens here? 248 00:13:10,290 --> 00:13:12,140 So let's analyze this to make sure 249 00:13:12,140 --> 00:13:15,320 that we're good in terms of an improvement. 250 00:13:15,320 --> 00:13:16,980 We have to be careful. 251 00:13:16,980 --> 00:13:20,990 So let's just arbitrarily say that it 252 00:13:20,990 --> 00:13:25,310 broke at 64, because that's kind of what happened over 253 00:13:25,310 --> 00:13:27,110 in that previous algorithm too. 254 00:13:27,110 --> 00:13:31,280 So that means I've done four drops without a break. 255 00:13:34,750 --> 00:13:36,190 I'm sorry, four drops. 256 00:13:36,190 --> 00:13:41,650 Three drops without a break and four drops total. 257 00:13:41,650 --> 00:13:42,970 So I've done four drops. 258 00:13:42,970 --> 00:13:47,060 But now the cool thing is I don't have to start from 16. 259 00:13:47,060 --> 00:13:49,990 I know it didn't break at 48. 260 00:13:49,990 --> 00:13:58,200 So I can now look at four drops till break of first ball. 261 00:13:58,200 --> 00:14:00,560 That is what I wanted to say. 262 00:14:00,560 --> 00:14:05,470 And now, I can look at the interval 49. 263 00:14:05,470 --> 00:14:08,110 Because 48, it did not break-- 264 00:14:08,110 --> 00:14:12,100 49 to 63, inclusive. 265 00:14:12,100 --> 00:14:14,860 And so if you do that, that's 15. 266 00:14:14,860 --> 00:14:16,280 It's 15. 267 00:14:16,280 --> 00:14:19,430 The difference is 14, but it's inclusive, so it's 15. 268 00:14:19,430 --> 00:14:24,540 So 15 plus 4, that is 19 drops. 269 00:14:24,540 --> 00:14:25,180 Yeah. 270 00:14:25,180 --> 00:14:25,800 Back there. 271 00:14:25,800 --> 00:14:27,175 AUDIENCE: Wouldn't the worst case 272 00:14:27,175 --> 00:14:28,800 though be when it's at 127. 273 00:14:28,800 --> 00:14:32,890 SRINI DEVADAS: Yes, that's absolutely correct. 274 00:14:32,890 --> 00:14:35,670 So 19 is not the worst case. 275 00:14:35,670 --> 00:14:38,430 I mean it's looking good, but this was exactly 276 00:14:38,430 --> 00:14:41,490 the point I was trying to make right from the beginning. 277 00:14:41,490 --> 00:14:42,630 You are not done yet. 278 00:14:42,630 --> 00:14:45,630 I mean, you cannot just go off and say that 19 is the worst 279 00:14:45,630 --> 00:14:46,872 case for this algorithm. 280 00:14:46,872 --> 00:14:48,330 The worst case for this algorithm-- 281 00:14:48,330 --> 00:14:51,810 this particular algorithm occurs at a different point 282 00:14:51,810 --> 00:14:55,380 from the original algorithm that went midway. 283 00:14:55,380 --> 00:14:56,960 Every algorithm is different. 284 00:14:56,960 --> 00:15:00,210 The parameters are different here. 285 00:15:00,210 --> 00:15:02,960 And so I'm just calling a different algorithm. 286 00:15:02,960 --> 00:15:06,390 But the case that would happen is 287 00:15:06,390 --> 00:15:08,490 I guess you would have 112 here. 288 00:15:08,490 --> 00:15:11,760 And then you'd go 1, 2, so how many? 289 00:15:11,760 --> 00:15:14,537 So let's say it gets to 128. 290 00:15:14,537 --> 00:15:16,620 And if it doesn't break, then you're clearly done. 291 00:15:16,620 --> 00:15:18,600 So that's not the worst case. 292 00:15:18,600 --> 00:15:24,990 So if it gets to 128 and it breaks, then so how many drops 293 00:15:24,990 --> 00:15:25,650 have we done? 294 00:15:25,650 --> 00:15:28,530 128 divided 16 is what? 295 00:15:28,530 --> 00:15:29,400 Is 8. 296 00:15:29,400 --> 00:15:30,720 Are you sure? 297 00:15:30,720 --> 00:15:31,740 You're all sure? 298 00:15:31,740 --> 00:15:32,240 OK. 299 00:15:32,240 --> 00:15:33,780 Then I'm sure. 300 00:15:33,780 --> 00:15:36,600 So that's eight drops. 301 00:15:36,600 --> 00:15:42,720 And now you need to do 113 to 127. 302 00:15:42,720 --> 00:15:45,210 And that we know is 15. 303 00:15:45,210 --> 00:15:47,420 So that's 23. 304 00:15:47,420 --> 00:15:48,850 All right. 305 00:15:48,850 --> 00:15:53,320 So the best you have done here is 23. 306 00:15:53,320 --> 00:15:56,470 But are we satisfied? 307 00:15:56,470 --> 00:15:57,970 We're never satisfied. 308 00:15:57,970 --> 00:16:00,690 We always want to do a little bit better. 309 00:16:00,690 --> 00:16:03,640 So how did she pick 16? 310 00:16:03,640 --> 00:16:05,220 Why did you come up with 16, Lucy? 311 00:16:05,220 --> 00:16:06,686 It looked like a nice number? 312 00:16:06,686 --> 00:16:08,630 AUDIENCE: I just figured it divides 128. 313 00:16:08,630 --> 00:16:11,970 SRINI DEVADAS: You just figured, oh, nice. 314 00:16:11,970 --> 00:16:15,280 So if you'd had 17, then we would have had real trouble 315 00:16:15,280 --> 00:16:16,640 with all these numbers. 316 00:16:16,640 --> 00:16:17,140 Yeah. 317 00:16:17,140 --> 00:16:17,470 Go ahead. 318 00:16:17,470 --> 00:16:17,970 Josh. 319 00:16:17,970 --> 00:16:20,620 AUDIENCE: Think you can do a little bit better 320 00:16:20,620 --> 00:16:23,460 if you get closer to the square root of 128? 321 00:16:23,460 --> 00:16:25,190 SRINI DEVADAS: Ah, beautiful, beautiful. 322 00:16:25,190 --> 00:16:26,450 I love you guys. 323 00:16:26,450 --> 00:16:29,140 You guys make it so easy. 324 00:16:29,140 --> 00:16:30,640 So that's the next thing I was going 325 00:16:30,640 --> 00:16:34,300 to try and pry out of you, but you got it out 326 00:16:34,300 --> 00:16:37,720 without much effort from me. 327 00:16:37,720 --> 00:16:42,580 So you can kind of get analytical here as opposed 328 00:16:42,580 --> 00:16:47,770 to doing things empirically, which was try a number 329 00:16:47,770 --> 00:16:50,420 and figure out what happened, which is kind of what we did. 330 00:16:50,420 --> 00:16:52,570 And you can be a little more analytical about this 331 00:16:52,570 --> 00:16:55,960 and you could say, hey, suppose I 332 00:16:55,960 --> 00:16:59,980 want to find this number k, which is kind of the 64-- 333 00:16:59,980 --> 00:17:01,900 started with 64, went to 16. 334 00:17:01,900 --> 00:17:04,810 And now, we're thinking we can do better than 16 even. 335 00:17:04,810 --> 00:17:10,810 And we start dropping things at k, 2k, 3k. 336 00:17:10,810 --> 00:17:15,099 And I want to get this exactly right. 337 00:17:15,099 --> 00:17:17,069 So let me look at my notes. 338 00:17:17,069 --> 00:17:18,930 And you go dot, dot, dot-- 339 00:17:18,930 --> 00:17:26,250 n divided by k minus 1 times k and n divided by k times 340 00:17:26,250 --> 00:17:28,140 k, which is obviously m. 341 00:17:28,140 --> 00:17:30,630 And so I'm trying to, I'm trying to write it-- 342 00:17:30,630 --> 00:17:32,490 I'm trying to get this k. 343 00:17:32,490 --> 00:17:33,810 I want to write something. 344 00:17:33,810 --> 00:17:36,120 I want to minimize at some quantity 345 00:17:36,120 --> 00:17:38,670 and discover the k that is going to give me my best 346 00:17:38,670 --> 00:17:41,460 case in the worst case-- 347 00:17:41,460 --> 00:17:43,500 minimize the worst case, right? 348 00:17:43,500 --> 00:17:46,050 So that's really what I want. 349 00:17:46,050 --> 00:17:49,930 So if you look at what happens here, 350 00:17:49,930 --> 00:17:54,420 you're in a situation where you have to now, 351 00:17:54,420 --> 00:17:55,530 you have a count, right? 352 00:17:55,530 --> 00:17:57,160 I mean, well, what do you want to-- 353 00:17:57,160 --> 00:18:03,420 we've decided that k is this general strategy. 354 00:18:03,420 --> 00:18:05,280 And we don't quite know what k is. 355 00:18:05,280 --> 00:18:09,900 And we have to do some analysis of the worst-case situation 356 00:18:09,900 --> 00:18:13,740 in order to find the k that would work well for us. 357 00:18:13,740 --> 00:18:18,930 And so the way you do this is by, essentially, the analysis 358 00:18:18,930 --> 00:18:20,430 we've done so far. 359 00:18:20,430 --> 00:18:28,380 And kind of we have a nice, the point that was made here, 360 00:18:28,380 --> 00:18:32,100 I think, it was Kevin who said we have to go to the last drop, 361 00:18:32,100 --> 00:18:36,370 because that's kind of the case where it's the worst case. 362 00:18:36,370 --> 00:18:37,860 And that's similar here as well. 363 00:18:37,860 --> 00:18:40,950 Because if you go up all the way here 364 00:18:40,950 --> 00:18:43,560 and then it breaks, if it doesn't break here, 365 00:18:43,560 --> 00:18:44,520 you're done. 366 00:18:44,520 --> 00:18:48,960 If it breaks here, then you've got, you've done a lot of drops 367 00:18:48,960 --> 00:18:51,180 before it broke that first ball. 368 00:18:51,180 --> 00:18:54,750 And so that's the situation which you can pull in 369 00:18:54,750 --> 00:18:56,850 as your worst-case situation. 370 00:18:56,850 --> 00:18:59,880 So that's exactly what we did before. 371 00:18:59,880 --> 00:19:05,110 And so if you really think about it, 372 00:19:05,110 --> 00:19:20,290 assume first ball doesn't break until the last drop is 373 00:19:20,290 --> 00:19:22,390 the worst-case situation. 374 00:19:22,390 --> 00:19:26,530 And so what does that mean from a symbolic standpoint 375 00:19:26,530 --> 00:19:29,815 of with respect to the number of drops of-- 376 00:19:29,815 --> 00:19:34,240 so the number of drops in this case of the first ball? 377 00:19:34,240 --> 00:19:37,781 Can someone give me an equation for it? 378 00:19:37,781 --> 00:19:38,280 Yeah. 379 00:19:38,280 --> 00:19:39,204 Go ahead. 380 00:19:39,204 --> 00:19:40,128 AUDIENCE: n over k. 381 00:19:40,128 --> 00:19:41,128 SRINI DEVADAS: n over k. 382 00:19:41,128 --> 00:19:41,930 You a Kevin too? 383 00:19:41,930 --> 00:19:42,230 Yeah. 384 00:19:42,230 --> 00:19:42,730 OK. 385 00:19:44,860 --> 00:19:45,981 So n over k. 386 00:19:45,981 --> 00:19:46,480 Right. 387 00:19:46,480 --> 00:19:48,470 So that's what happened there. 388 00:19:48,470 --> 00:19:51,010 Now, well, it broke, right? 389 00:19:51,010 --> 00:19:52,610 That's what we decided here. 390 00:19:52,610 --> 00:20:08,242 And so now, you need to search n k plus 1 to-- 391 00:20:08,242 --> 00:20:10,480 well, you got to go all the way there. 392 00:20:10,480 --> 00:20:14,820 I'm sorry, it broke there, so you got to go n minus 1. 393 00:20:20,310 --> 00:20:22,535 This is the same as what I wrote before, 394 00:20:22,535 --> 00:20:23,910 but it's just that I had numbers. 395 00:20:23,910 --> 00:20:25,660 And it was kind of easier to write numbers 396 00:20:25,660 --> 00:20:26,940 than write these symbols. 397 00:20:26,940 --> 00:20:31,740 But we have to do this because we don't know what k is. 398 00:20:31,740 --> 00:20:35,720 So if you look at how many drops are-- 399 00:20:35,720 --> 00:20:39,720 and if you're just going to do this, you get-- 400 00:20:39,720 --> 00:20:45,750 if you notice, this gets simplified to n. 401 00:20:45,750 --> 00:20:48,710 Yeah, so this is n, and then you have 402 00:20:48,710 --> 00:20:55,570 minus k plus 1, all the way to n minus 1. 403 00:20:55,570 --> 00:20:59,630 So if you subtract, and this is a plus 1 here. 404 00:20:59,630 --> 00:21:00,860 Boy, this is confusing. 405 00:21:00,860 --> 00:21:02,300 I'm not good at math. 406 00:21:02,300 --> 00:21:03,800 I'm definitely not good at math. 407 00:21:03,800 --> 00:21:12,746 So this is k minus 1 drops in the worst case. 408 00:21:12,746 --> 00:21:14,640 All right. 409 00:21:14,640 --> 00:21:18,940 So you now need to add these two things together. 410 00:21:18,940 --> 00:21:26,810 So what we'd like to do is minimize n divide it 411 00:21:26,810 --> 00:21:29,600 by k, which is this plus that. 412 00:21:36,030 --> 00:21:38,220 We want to minimize this function. 413 00:21:38,220 --> 00:21:39,710 And it's a constant. 414 00:21:39,710 --> 00:21:41,610 k is a variable. 415 00:21:41,610 --> 00:21:44,580 And for a given n, we want to discover the k that 416 00:21:44,580 --> 00:21:45,960 minimizes this quantity. 417 00:21:45,960 --> 00:21:48,390 And it goes back to the intuition 418 00:21:48,390 --> 00:21:52,110 that I think Josh had about the square root of n. 419 00:21:52,110 --> 00:21:54,571 So again, high school calculus here, which I've forgotten, 420 00:21:54,571 --> 00:21:56,820 which hopefully you haven't because you still need it. 421 00:21:56,820 --> 00:21:57,320 I don't. 422 00:22:00,780 --> 00:22:03,930 And you end up essentially saying 423 00:22:03,930 --> 00:22:06,450 you can turn this into two raised 424 00:22:06,450 --> 00:22:13,710 to the square root of n minus 1 drops in the worst case. 425 00:22:13,710 --> 00:22:16,420 All right. 426 00:22:16,420 --> 00:22:23,120 So you can take the floor of that, and that's 11. 427 00:22:23,120 --> 00:22:24,860 So that's that square root of n. 428 00:22:24,860 --> 00:22:28,250 So 2 times 11 minus 1 is 21. 429 00:22:28,250 --> 00:22:29,530 So you can do 21. 430 00:22:29,530 --> 00:22:32,944 So you think 21 is the optimum? 431 00:22:32,944 --> 00:22:33,600 Yeah, Kevin. 432 00:22:33,600 --> 00:22:35,058 AUDIENCE: Can we do a little better 433 00:22:35,058 --> 00:22:36,360 if we make it go smaller? 434 00:22:36,360 --> 00:22:37,300 SRINI DEVADAS: Yes. 435 00:22:37,300 --> 00:22:39,340 You can do a little bit better. 436 00:22:39,340 --> 00:22:40,740 I don't want to do this just yet. 437 00:22:40,740 --> 00:22:41,070 Hopefully. 438 00:22:41,070 --> 00:22:41,740 We'll have time. 439 00:22:41,740 --> 00:22:44,970 We'll get to that because I want to go to the d greater than 2 440 00:22:44,970 --> 00:22:47,200 case, but you're absolutely right. 441 00:22:47,200 --> 00:22:51,360 It turns out that you can do, this would give you 21, 442 00:22:51,360 --> 00:22:54,490 as I mentioned, 22 minus 1. 443 00:22:54,490 --> 00:22:57,060 It turns out, you can do better but by a few drops. 444 00:22:57,060 --> 00:23:01,740 And the assumption we made here, what is the assumption that we 445 00:23:01,740 --> 00:23:03,600 made here? 446 00:23:03,600 --> 00:23:05,689 When I wrote this, what assumption did I make, 447 00:23:05,689 --> 00:23:07,730 which is kind of similar to what Kevin just said, 448 00:23:07,730 --> 00:23:10,250 but not exactly? 449 00:23:10,250 --> 00:23:14,830 Would someone point out the assumption that was made here? 450 00:23:14,830 --> 00:23:16,189 Not you. 451 00:23:16,189 --> 00:23:16,730 Someone else. 452 00:23:20,380 --> 00:23:20,880 All right. 453 00:23:20,880 --> 00:23:21,330 Ganatra. 454 00:23:21,330 --> 00:23:21,829 Go for it. 455 00:23:21,829 --> 00:23:23,400 AUDIENCE: The intervals are the same. 456 00:23:23,400 --> 00:23:24,233 SRINI DEVADAS: Yeah. 457 00:23:24,233 --> 00:23:26,220 The intervals are equal. 458 00:23:26,220 --> 00:23:30,150 So this, look, it's a different problem. 459 00:23:30,150 --> 00:23:35,120 If it didn't break here, then I got a smaller-- 460 00:23:35,120 --> 00:23:36,950 I have two balls. 461 00:23:36,950 --> 00:23:38,780 And I had a smaller problem. 462 00:23:38,780 --> 00:23:41,764 So why would I use the same k? 463 00:23:41,764 --> 00:23:43,430 I mean, that kind of makes sense, right? 464 00:23:43,430 --> 00:23:46,550 So now, it's more complicated and hopefully, we'll 465 00:23:46,550 --> 00:23:47,530 get to that. 466 00:23:47,530 --> 00:23:49,896 But I want to do other things first. 467 00:23:49,896 --> 00:23:50,770 So that's the reason. 468 00:23:50,770 --> 00:23:54,720 So at least you know why 21 isn't optimal, right? 469 00:23:54,720 --> 00:23:58,370 It's because of this constraint that we placed on ourselves 470 00:23:58,370 --> 00:24:01,310 in order to do this analysis to get to the point 471 00:24:01,310 --> 00:24:06,500 where we had 2 times square root of n minus 1 that corresponds 472 00:24:06,500 --> 00:24:09,740 to the equal interval constraint. 473 00:24:09,740 --> 00:24:10,290 So good. 474 00:24:10,290 --> 00:24:10,790 All right. 475 00:24:10,790 --> 00:24:12,590 Any questions about this? 476 00:24:12,590 --> 00:24:14,090 So as you can imagine, I'm not going 477 00:24:14,090 --> 00:24:18,410 to show you code that computes 2 times square root of n minus 1. 478 00:24:18,410 --> 00:24:20,720 So that doesn't make sense, but we 479 00:24:20,720 --> 00:24:24,230 do have code to look at that corresponds to the case 480 00:24:24,230 --> 00:24:27,320 where d could be 3, d could be 4. 481 00:24:27,320 --> 00:24:30,050 And then obviously, it gets more interesting in terms 482 00:24:30,050 --> 00:24:31,670 of what your strategy is. 483 00:24:31,670 --> 00:24:34,490 And we won't go with optimizing. 484 00:24:34,490 --> 00:24:38,690 In fact, there's an open problem as to what the optimum strategy 485 00:24:38,690 --> 00:24:40,530 would be for arbitrary d-- 486 00:24:40,530 --> 00:24:42,440 d meaning the number of balls-- 487 00:24:42,440 --> 00:24:44,780 if it's 5, 6, whatever-- 488 00:24:44,780 --> 00:24:47,180 partly because of what I just talked about with respect 489 00:24:47,180 --> 00:24:50,480 to you need to utilize unequal intervals. 490 00:24:50,480 --> 00:24:52,850 But the natural thing to do now is 491 00:24:52,850 --> 00:24:57,350 to move to d equals a arbitrary number of balls 492 00:24:57,350 --> 00:25:03,230 and stick with kind of the same algorithmic approach of having 493 00:25:03,230 --> 00:25:05,090 equal intervals. 494 00:25:05,090 --> 00:25:07,190 But the question is, what are these intervals? 495 00:25:07,190 --> 00:25:10,640 What and how would we find the size of these intervals? 496 00:25:10,640 --> 00:25:12,630 Because it's kind of not immediately obvious 497 00:25:12,630 --> 00:25:14,132 if I told you d was 5. 498 00:25:14,132 --> 00:25:16,340 And you don't want to just keep the three balls away, 499 00:25:16,340 --> 00:25:18,800 and sell them later, and just use the two, right? 500 00:25:18,800 --> 00:25:21,270 That doesn't give you your minimum. 501 00:25:21,270 --> 00:25:23,370 It doesn't minimize your worst case. 502 00:25:23,370 --> 00:25:27,460 You want to use these balls. 503 00:25:27,460 --> 00:25:27,990 Cool. 504 00:25:27,990 --> 00:25:28,590 All right. 505 00:25:28,590 --> 00:25:38,930 So let's talk a little bit about if I had d equals 3, 506 00:25:38,930 --> 00:25:41,690 I mean again, you can use your intuition. 507 00:25:41,690 --> 00:25:45,740 What do you think we should do? 508 00:25:45,740 --> 00:25:48,260 What happened for d equals 2 with respect to that interval 509 00:25:48,260 --> 00:25:51,590 and how is it related to n? 510 00:25:51,590 --> 00:25:55,550 So what do you think, just wild guess, maybe even? 511 00:25:55,550 --> 00:25:57,290 What do you think we need to do if we 512 00:25:57,290 --> 00:26:00,200 had d equals 3 for that very first ball in terms 513 00:26:00,200 --> 00:26:05,075 of what we need to look at in relation to the interval size? 514 00:26:07,791 --> 00:26:08,290 Yeah. 515 00:26:08,290 --> 00:26:08,790 Back there. 516 00:26:08,790 --> 00:26:10,340 AUDIENCE: We need a cube root of 128. 517 00:26:10,340 --> 00:26:11,610 SRINI DEVADAS: Ah, cube root, cube root. 518 00:26:11,610 --> 00:26:12,580 What's your name? 519 00:26:12,580 --> 00:26:12,840 AUDIENCE: Ryan. 520 00:26:12,840 --> 00:26:13,680 SRINI DEVADAS: Ryan. 521 00:26:13,680 --> 00:26:15,150 So Ryan says, you know, cube root. 522 00:26:15,150 --> 00:26:17,260 And yeah, that's the correct intuition. 523 00:26:17,260 --> 00:26:19,756 It's a little bit unclear, you know, what happens. 524 00:26:19,756 --> 00:26:21,630 You know, so you do cube roots, and maybe you 525 00:26:21,630 --> 00:26:24,560 have what's the cube root of 128? 526 00:26:24,560 --> 00:26:27,030 5, yeah, that's right. 527 00:26:27,030 --> 00:26:27,940 I think it's 5. 528 00:26:27,940 --> 00:26:28,440 OK. 529 00:26:28,440 --> 00:26:29,670 It's close to 5, you know. 530 00:26:29,670 --> 00:26:31,950 It's 6 or 5 or 6, right? 531 00:26:31,950 --> 00:26:35,760 And so maybe I do 6, and then I do 12. 532 00:26:35,760 --> 00:26:39,190 And it's a little bit strange. 533 00:26:39,190 --> 00:26:42,270 So it turns out, it's actually the 6 and 12, 534 00:26:42,270 --> 00:26:45,690 and you start with 128 when you have three balls, 535 00:26:45,690 --> 00:26:48,690 doesn't seem like it's going to give you the worst case, right? 536 00:26:48,690 --> 00:26:50,890 Because, you know, like in the worst case, 537 00:26:50,890 --> 00:26:54,100 you might be dropping the ball 6, 12, 18, 538 00:26:54,100 --> 00:26:56,640 24, et cetera, et cetera. 539 00:26:56,640 --> 00:26:59,160 So cube root is right, but it's something a little bit 540 00:26:59,160 --> 00:27:01,530 different, because it gave us this interval that 541 00:27:01,530 --> 00:27:03,640 was kind of too small. 542 00:27:03,640 --> 00:27:05,820 So how would I get something that's bigger? 543 00:27:05,820 --> 00:27:07,830 You'd want things that are a little bit bigger. 544 00:27:07,830 --> 00:27:10,900 I mean, I think you're going in the right direction, 545 00:27:10,900 --> 00:27:12,840 but I don't want 6 and 12 and 18. 546 00:27:12,840 --> 00:27:15,720 I mean, clearly, that's going to give me like 25 drops 547 00:27:15,720 --> 00:27:19,456 and that's even worse than the two ball case, which really 548 00:27:19,456 --> 00:27:21,830 means you should lose your job and never get another one, 549 00:27:21,830 --> 00:27:22,330 right? 550 00:27:22,330 --> 00:27:25,680 So if you do something that bad, right? 551 00:27:25,680 --> 00:27:29,700 So that's-- what would you do that's kind of different from 552 00:27:29,700 --> 00:27:31,330 cube root, but related to cube root? 553 00:27:34,020 --> 00:27:34,970 Yeah, go ahead Fadi. 554 00:27:34,970 --> 00:27:36,730 AUDIENCE: 128 raised to 2/3 maybe? 555 00:27:36,730 --> 00:27:37,046 SRINI DEVADAS: Yeah, yeah. 556 00:27:37,046 --> 00:27:37,546 Two thirds. 557 00:27:37,546 --> 00:27:38,150 Exactly right. 558 00:27:38,150 --> 00:27:40,370 So what you want to do is, you want to start-- 559 00:27:40,370 --> 00:27:42,880 it makes sense that when you have lots of balls, 560 00:27:42,880 --> 00:27:44,640 in terms of being able to use them, 561 00:27:44,640 --> 00:27:47,117 that you start with larger intervals and you shrink them. 562 00:27:47,117 --> 00:27:48,950 That's exactly what happened with two balls. 563 00:27:48,950 --> 00:27:52,410 You started with k in the case where we had two balls. 564 00:27:52,410 --> 00:27:55,050 And then we are down to one when we have one ball. 565 00:27:55,050 --> 00:27:56,707 So that's really what you're doing. 566 00:27:56,707 --> 00:27:58,290 When you have one ball, all you can do 567 00:27:58,290 --> 00:28:01,520 is go one floor at a time. 568 00:28:01,520 --> 00:28:03,580 When you have two balls, you go k, 569 00:28:03,580 --> 00:28:05,504 where k happened to be square root of n. 570 00:28:05,504 --> 00:28:07,170 And then we had three balls, you kind of 571 00:28:07,170 --> 00:28:10,620 want to start with like n raised to 2/3 572 00:28:10,620 --> 00:28:13,110 in terms of the interval. 573 00:28:13,110 --> 00:28:16,050 And then if I had four balls, the argument 574 00:28:16,050 --> 00:28:18,870 would be that I would start with n raised to 3/4. 575 00:28:18,870 --> 00:28:23,010 And then I would go to square root of n. 576 00:28:23,010 --> 00:28:25,090 And then I would go so on and so forth. 577 00:28:25,090 --> 00:28:26,850 And what is that reminding you of? 578 00:28:26,850 --> 00:28:29,880 What is this, in terms of these powers 579 00:28:29,880 --> 00:28:35,520 and getting up to not quite n, but 5, 6th n, so on. 580 00:28:35,520 --> 00:28:38,960 What does that remind you of from number theory 581 00:28:38,960 --> 00:28:43,460 or just sort of regular arithmetic? 582 00:28:43,460 --> 00:28:44,269 AUDIENCE: Bases. 583 00:28:44,269 --> 00:28:45,810 SRINI DEVADAS: Yeah, different bases. 584 00:28:45,810 --> 00:28:48,676 It's different radix arithmetic, right? 585 00:28:48,676 --> 00:28:51,050 So it turns out, the way you want to solve this problem-- 586 00:28:51,050 --> 00:28:55,200 and this is in keeping with the intuition you 587 00:28:55,200 --> 00:28:59,430 have with respect to the cube roots, and so on, and so forth. 588 00:28:59,430 --> 00:29:01,890 The way we want to do this is, we want 589 00:29:01,890 --> 00:29:05,175 to think about d digit numbers. 590 00:29:12,790 --> 00:29:17,040 And the numbers themselves, and d is the number of balls. 591 00:29:20,020 --> 00:29:21,290 And so that's the d digits. 592 00:29:21,290 --> 00:29:24,890 But what is the radix of these numbers? 593 00:29:24,890 --> 00:29:30,360 The radix of these numbers are going to be related to the k, 594 00:29:30,360 --> 00:29:31,920 to the intervals. 595 00:29:31,920 --> 00:29:34,340 So you essentially want to say, you know, 596 00:29:34,340 --> 00:29:36,470 I'm counting in multiples of k-- 597 00:29:36,470 --> 00:29:38,990 1k, 2k, 3k, et cetera. 598 00:29:38,990 --> 00:29:41,150 So if I had like radix k, that means 599 00:29:41,150 --> 00:29:44,720 I'm just incrementing that particular digit. 600 00:29:44,720 --> 00:29:48,920 You know, if I go, if I had radix 12, 601 00:29:48,920 --> 00:29:57,270 and I had 1 0 in radix 12, then this would be-- 602 00:30:02,120 --> 00:30:07,310 so when I go 2 0, then the most significant digit 603 00:30:07,310 --> 00:30:08,120 got incremented. 604 00:30:08,120 --> 00:30:11,540 And so this is plus 12. 605 00:30:11,540 --> 00:30:12,980 This is 12 times larger-- 606 00:30:12,980 --> 00:30:17,510 I'm sorry-- 12 larger than two 0's, 12 larger than 1 0 607 00:30:17,510 --> 00:30:19,300 in radix 12. 608 00:30:19,300 --> 00:30:20,240 Make sense? 609 00:30:20,240 --> 00:30:29,110 Just like 20 is 10 larger than 10 in decimal. 610 00:30:29,110 --> 00:30:31,610 So that's the way you want to think about this. 611 00:30:31,610 --> 00:30:37,790 And so you're going to do r-ary numbers. 612 00:30:37,790 --> 00:30:39,290 And the first thing that you need 613 00:30:39,290 --> 00:30:41,270 to do is, you need to figure out, 614 00:30:41,270 --> 00:30:44,080 based on the number of floors that you have, figure out, 615 00:30:44,080 --> 00:30:45,260 you've given d. 616 00:30:45,260 --> 00:30:47,799 You're given n, the number of floors. 617 00:30:47,799 --> 00:30:49,340 And you're given the number of balls. 618 00:30:49,340 --> 00:30:51,800 And you got to figure out what radix you're working in. 619 00:30:51,800 --> 00:30:53,810 And this is going to be a generalization 620 00:30:53,810 --> 00:30:57,820 of the square root of n, as you'll see. 621 00:30:57,820 --> 00:31:02,930 We want to choose a radix such that r 622 00:31:02,930 --> 00:31:08,840 raised to d is greater than n. 623 00:31:08,840 --> 00:31:16,800 Choose r such that r raised to d is greater than n. 624 00:31:16,800 --> 00:31:19,080 And so that comes back to, obviously, 625 00:31:19,080 --> 00:31:22,350 if you needed, if d were 2, then it makes sense 626 00:31:22,350 --> 00:31:24,150 that r would be square root of n, 627 00:31:24,150 --> 00:31:26,490 which is in keeping with what we talked about. 628 00:31:26,490 --> 00:31:30,027 But if d is 3, then that radix-- 629 00:31:30,027 --> 00:31:31,860 not necessarily the interval that we choose, 630 00:31:31,860 --> 00:31:34,777 because the intervals are going to be different-- 631 00:31:34,777 --> 00:31:36,610 is going to be more like the cube root of n, 632 00:31:36,610 --> 00:31:41,080 which is also proposed. 633 00:31:41,080 --> 00:31:42,810 Ryan mentioned that. 634 00:31:42,810 --> 00:31:50,310 And so in this case, let's say that n were 128 and d equals 4. 635 00:31:50,310 --> 00:31:56,430 Then we would choose r equals 4, because 4 raised to 4 636 00:31:56,430 --> 00:31:59,550 is 256, which is greater than 128. 637 00:31:59,550 --> 00:32:05,610 And we can't choose 3, because 3 raised to 4 is only 81. 638 00:32:05,610 --> 00:32:08,940 And 81 isn't greater than 128. 639 00:32:08,940 --> 00:32:11,840 So you'd have to choose 4. 640 00:32:11,840 --> 00:32:16,600 So what we're now saying is, we want to somehow figure out-- 641 00:32:16,600 --> 00:32:19,300 we're not quite there in an algorithm yet. 642 00:32:19,300 --> 00:32:22,830 But we have a kind of a path forward 643 00:32:22,830 --> 00:32:28,470 in that we want to look at that representation, 644 00:32:28,470 --> 00:32:33,750 that r-ary representation of these d digit numbers. 645 00:32:33,750 --> 00:32:37,710 And we're going to essentially increment. 646 00:32:37,710 --> 00:32:41,400 And so with our goal, roughly speaking, 647 00:32:41,400 --> 00:32:45,030 we're going to go with this representation. 648 00:32:45,030 --> 00:32:48,360 And we're going to start incrementing digits. 649 00:32:48,360 --> 00:32:51,430 So we just go 0, 1, 2 in terms of the digits. 650 00:32:51,430 --> 00:32:53,995 But because the representation is r-ary 651 00:32:53,995 --> 00:32:55,620 that means something different in terms 652 00:32:55,620 --> 00:32:58,230 of the number of floors, depending on what r is. 653 00:32:58,230 --> 00:33:01,410 I mean, just like I said, if we're 654 00:33:01,410 --> 00:33:06,512 this were ternary representation then in terms of floors, 655 00:33:06,512 --> 00:33:07,095 this is only-- 656 00:33:09,841 --> 00:33:13,620 this would be, for ternary, this would be three floors. 657 00:33:13,620 --> 00:33:19,230 2 0 would be three floors away from 1 0. 658 00:33:19,230 --> 00:33:22,040 In decimal, 2 0 is 10 floors away from 1 0, 659 00:33:22,040 --> 00:33:23,040 and so on, and so forth. 660 00:33:23,040 --> 00:33:24,251 That makes sense? 661 00:33:24,251 --> 00:33:24,750 Yep. 662 00:33:24,750 --> 00:33:28,689 Ask me questions if you're confused. 663 00:33:28,689 --> 00:33:29,730 So let's take an example. 664 00:33:29,730 --> 00:33:34,120 So and then we'll look at code that actually builds 665 00:33:34,120 --> 00:33:38,520 this algorithm from scratch. 666 00:33:38,520 --> 00:33:40,350 So we'll take our example of d equals 667 00:33:40,350 --> 00:33:44,460 4 for our canonical 128 floor Shanghai Tower, 668 00:33:44,460 --> 00:33:48,240 because you do want to get a sense of how things improve. 669 00:33:48,240 --> 00:33:50,760 We did improve from obviously d equals 1 670 00:33:50,760 --> 00:33:55,320 through d equals 2 with the equal interval assumption. 671 00:33:55,320 --> 00:33:59,550 But with this radix idea, we'd like 672 00:33:59,550 --> 00:34:03,060 to see how much better we can do with d equals 4. 673 00:34:03,060 --> 00:34:06,780 And we want to do substantially better than 21 drops 674 00:34:06,780 --> 00:34:10,429 or whatever we had back there. 675 00:34:10,429 --> 00:34:12,870 So r is 4. 676 00:34:12,870 --> 00:34:16,230 d is 4-- oh, I'm sorry-- d is 4. 677 00:34:16,230 --> 00:34:18,570 n equals 128. 678 00:34:18,570 --> 00:34:24,338 And what we're going to do is, the smallest number is 0000. 679 00:34:24,338 --> 00:34:30,460 And in brackets, I'm going write what the decimal representation 680 00:34:30,460 --> 00:34:33,719 is, because I think that does give you some intuition. 681 00:34:33,719 --> 00:34:37,104 You get a sense of how this translates into floors. 682 00:34:37,104 --> 00:34:38,520 And after a while, if you're going 683 00:34:38,520 --> 00:34:40,770 to tell someone to do this, you want to give them 684 00:34:40,770 --> 00:34:42,150 floor numbers in decimal. 685 00:34:42,150 --> 00:34:43,710 Because the Shanghai Tower doesn't 686 00:34:43,710 --> 00:34:48,179 have some weird representation for the floors 687 00:34:48,179 --> 00:34:51,150 when you get into an elevator or anything like that. 688 00:34:51,150 --> 00:34:53,070 So this is floor 0. 689 00:34:53,070 --> 00:35:00,000 And now, the highest number is 3333, which happens to be 255. 690 00:35:00,000 --> 00:35:02,250 I'm not going to write floor each time. 691 00:35:02,250 --> 00:35:04,230 But when I put things in brackets here, 692 00:35:04,230 --> 00:35:05,801 that's the decimal representation. 693 00:35:05,801 --> 00:35:06,300 OK. 694 00:35:06,300 --> 00:35:07,230 So that's 255. 695 00:35:07,230 --> 00:35:08,970 So that's obviously too big. 696 00:35:08,970 --> 00:35:09,940 We don't need that. 697 00:35:09,940 --> 00:35:12,570 But we're stuck with having to choose 698 00:35:12,570 --> 00:35:16,220 r equals 4 for the reasons I described to you before. 699 00:35:16,220 --> 00:35:19,500 So there's somewhere in here there's a 128. 700 00:35:19,500 --> 00:35:24,150 And I don't have it right up in front of me 701 00:35:24,150 --> 00:35:28,950 as to what 128 would correspond to. 702 00:35:28,950 --> 00:35:36,920 But 1233, for example, would be floor 111. 703 00:35:36,920 --> 00:35:38,380 So the way this works, and you all 704 00:35:38,380 --> 00:35:43,060 know you're radix arithmetic, it's 705 00:35:43,060 --> 00:35:51,310 3 times 1, plus 3 times 4, plus 2 times 4 square, plus 1 times 706 00:35:51,310 --> 00:35:52,960 4 cubed. 707 00:35:52,960 --> 00:35:53,950 That's what you have. 708 00:35:53,950 --> 00:35:55,630 And that's how you get 111. 709 00:35:55,630 --> 00:35:59,350 So what you have to do in order to do this right 710 00:35:59,350 --> 00:36:04,390 is to start with the most significant digit, which 711 00:36:04,390 --> 00:36:05,644 is the left-most digit. 712 00:36:05,644 --> 00:36:07,810 And that makes sense, because we want our intervals, 713 00:36:07,810 --> 00:36:09,910 when we have lots of balls, to be large. 714 00:36:09,910 --> 00:36:13,250 And that as you move and the balls break, 715 00:36:13,250 --> 00:36:17,410 you're going to start shifting rightward. 716 00:36:17,410 --> 00:36:19,960 And you're going to be incrementing different things. 717 00:36:19,960 --> 00:36:25,169 And that's essentially going to give us give us our algorithm. 718 00:36:25,169 --> 00:36:26,710 And so we'll talk about-- let me just 719 00:36:26,710 --> 00:36:29,110 give you an example of an algorithm. 720 00:36:29,110 --> 00:36:34,360 And we'll look at how the algorithm is implemented. 721 00:36:34,360 --> 00:36:37,120 As I mentioned, it turns into 10 lines of code, 722 00:36:37,120 --> 00:36:40,180 because all you're doing is translating 723 00:36:40,180 --> 00:36:42,400 into this representation. 724 00:36:42,400 --> 00:36:44,920 And you're just incrementing bits of the representation. 725 00:36:44,920 --> 00:36:46,120 And it's interactive. 726 00:36:46,120 --> 00:36:50,550 You can play with it later today or whenever. 727 00:36:50,550 --> 00:36:52,720 And you can sort of think of a hardness coefficient 728 00:36:52,720 --> 00:36:54,970 in your head then see how things work out. 729 00:36:54,970 --> 00:36:56,905 But before we end, we obviously will 730 00:36:56,905 --> 00:37:00,670 have to do a worst-case analysis of this algorithm 731 00:37:00,670 --> 00:37:04,930 because that's the whole point of this exercise. 732 00:37:04,930 --> 00:37:08,290 So in this case, where do you think I should-- 733 00:37:08,290 --> 00:37:11,560 so I don't want to drop, obviously, at floor 0. 734 00:37:11,560 --> 00:37:13,480 I mean, that's there's no notion of that. 735 00:37:13,480 --> 00:37:16,500 So where do I drop at, at the beginning? 736 00:37:16,500 --> 00:37:21,020 The first drop is at, given what I told you? 737 00:37:21,020 --> 00:37:23,580 You can tell me in any representation. 738 00:37:23,580 --> 00:37:25,220 Where would I drop the first ball 739 00:37:25,220 --> 00:37:27,350 if I have four balls and 128 floors? 740 00:37:32,480 --> 00:37:36,590 I said that I want to start with something that, obviously, 741 00:37:36,590 --> 00:37:38,450 there's some larger interval here. 742 00:37:38,450 --> 00:37:41,090 And I want to start on the left-hand side. 743 00:37:41,090 --> 00:37:43,520 And I want to increment the leftmost bit, which 744 00:37:43,520 --> 00:37:45,260 is the most significant bit. 745 00:37:45,260 --> 00:37:50,090 So I would start with 1000. 746 00:37:50,090 --> 00:38:00,560 So the first drop is at 1000, which is at floor 64. 747 00:38:00,560 --> 00:38:01,970 That's the first drop. 748 00:38:01,970 --> 00:38:09,374 Now, if in fact this does not break-- 749 00:38:09,374 --> 00:38:11,540 all right, so let's assume that this does not break. 750 00:38:14,970 --> 00:38:18,090 Then what do I do next? 751 00:38:18,090 --> 00:38:20,470 2000, right? 752 00:38:20,470 --> 00:38:25,030 And this would be 128. 753 00:38:25,030 --> 00:38:26,770 Let's assume that this breaks here. 754 00:38:29,400 --> 00:38:30,240 All right. 755 00:38:30,240 --> 00:38:32,370 So now, what do I do? 756 00:38:32,370 --> 00:38:33,000 It broke. 757 00:38:33,000 --> 00:38:35,190 I lost a ball. 758 00:38:35,190 --> 00:38:37,045 Now I have three balls left still. 759 00:38:37,045 --> 00:38:37,920 That's the good news. 760 00:38:37,920 --> 00:38:40,584 And how many-- so what do I need, where do I need to move? 761 00:38:40,584 --> 00:38:42,000 If I have three balls left, I need 762 00:38:42,000 --> 00:38:45,220 to be in a representation that has how many digits? 763 00:38:45,220 --> 00:38:46,420 Three digits. 764 00:38:46,420 --> 00:38:47,500 So I need to move. 765 00:38:47,500 --> 00:38:51,550 I a broke at 2000, so I know that it's less than 2000. 766 00:38:51,550 --> 00:38:54,820 So I need to go back to this, because this is the highest 767 00:38:54,820 --> 00:38:55,810 floor. 768 00:38:55,810 --> 00:38:58,710 In general, whenever things break, 769 00:38:58,710 --> 00:39:02,110 you go back to the highest floor that you know 770 00:39:02,110 --> 00:39:03,940 that the ball didn't break. 771 00:39:03,940 --> 00:39:05,740 I mean, that's the canonical strategy 772 00:39:05,740 --> 00:39:07,240 that we've followed all through. 773 00:39:07,240 --> 00:39:09,970 When the ball breaks, you go back 774 00:39:09,970 --> 00:39:11,560 and say, oh, but I do know that there 775 00:39:11,560 --> 00:39:15,130 was a highest floor in which the ball did not break. 776 00:39:15,130 --> 00:39:19,770 And I have to start with 1 plus that, right? 777 00:39:19,770 --> 00:39:22,820 So what I'm going to do here is I need to go up 778 00:39:22,820 --> 00:39:32,530 and because it broke, I need to look at 1001. 779 00:39:32,530 --> 00:39:34,900 The interval that I need to look at 780 00:39:34,900 --> 00:39:38,920 is 1001, because it's one more than that. 781 00:39:38,920 --> 00:39:44,410 And all the way to 1333, which is one less than that. 782 00:39:47,010 --> 00:39:51,750 So and this, you may feel a little bit better 783 00:39:51,750 --> 00:39:54,510 looking at this in decimal. 784 00:39:54,510 --> 00:39:55,210 I certainly do. 785 00:39:57,880 --> 00:40:01,560 It's 65, 127, which makes perfect sense. 786 00:40:01,560 --> 00:40:06,210 So far, we've played around with the balls, the first two balls. 787 00:40:06,210 --> 00:40:09,030 And so we haven't really seen anything very dramatically 788 00:40:09,030 --> 00:40:13,800 different from the two-ball case, but more is coming. 789 00:40:13,800 --> 00:40:15,690 Let's say this does not break. 790 00:40:15,690 --> 00:40:24,240 If this does not break, then what would I do now? 791 00:40:24,240 --> 00:40:25,830 If it does not break-- 792 00:40:25,830 --> 00:40:31,590 I'm sorry, my bad, my bad. 793 00:40:31,590 --> 00:40:33,990 I haven't even told you. 794 00:40:33,990 --> 00:40:35,820 I've just talked about the interval. 795 00:40:35,820 --> 00:40:38,570 And I haven't said what drop you need to make, right? 796 00:40:38,570 --> 00:40:41,040 So ignore what I said just now. 797 00:40:41,040 --> 00:40:44,190 I think that the next question is, the ball broke. 798 00:40:44,190 --> 00:40:45,360 So now I have a second ball. 799 00:40:48,010 --> 00:40:50,350 And I want the first drop of the second ball. 800 00:40:53,260 --> 00:40:55,227 And I want to look at this. 801 00:40:55,227 --> 00:40:56,560 I want to look at this interval. 802 00:40:56,560 --> 00:40:58,820 And it has to be somewhere inside this interval. 803 00:40:58,820 --> 00:40:59,903 But it's not going to be-- 804 00:40:59,903 --> 00:41:06,880 I'm not going to drop this at this floor, 65 or 1333. 805 00:41:06,880 --> 00:41:09,250 I want to pick something in between, right? 806 00:41:09,250 --> 00:41:13,390 And so now looking at this, tell me-- 807 00:41:13,390 --> 00:41:15,630 according to what you know so far-- 808 00:41:15,630 --> 00:41:18,400 where you think I should drop the second ball 809 00:41:18,400 --> 00:41:19,914 for the first time? 810 00:41:19,914 --> 00:41:20,798 Go ahead. 811 00:41:20,798 --> 00:41:21,950 AUDIENCE: 96. 812 00:41:21,950 --> 00:41:22,700 SRINI DEVADAS: 96. 813 00:41:22,700 --> 00:41:23,995 And how did you get that? 814 00:41:23,995 --> 00:41:26,690 AUDIENCE: Just like I did the half point. 815 00:41:26,690 --> 00:41:28,135 SRINI DEVADAS: And you put 1100? 816 00:41:28,135 --> 00:41:28,760 AUDIENCE: Yeah. 817 00:41:28,760 --> 00:41:29,460 SRINI DEVADAS: Perfect. 818 00:41:29,460 --> 00:41:30,196 What's your name? 819 00:41:30,196 --> 00:41:31,090 AUDIENCE: Evan. 820 00:41:31,090 --> 00:41:31,923 SRINI DEVADAS: Evan. 821 00:41:31,923 --> 00:41:33,560 So Evan says 96. 822 00:41:33,560 --> 00:41:37,880 I guess he thinks in decimal naturally, like most of us. 823 00:41:37,880 --> 00:41:41,540 But the way you would code this, by the way, 824 00:41:41,540 --> 00:41:45,830 is by incrementing this digit. 825 00:41:45,830 --> 00:41:49,040 And you'll get 1100. 826 00:41:49,040 --> 00:41:51,920 And in this case, let's say it does not break. 827 00:41:55,110 --> 00:41:58,770 Then where do you go, Evan? 828 00:41:58,770 --> 00:41:59,977 AUDIENCE: Above floor 96. 829 00:41:59,977 --> 00:42:00,810 SRINI DEVADAS: Yeah. 830 00:42:00,810 --> 00:42:02,150 After 96, it doesn't break. 831 00:42:02,150 --> 00:42:03,732 What would you do? 832 00:42:03,732 --> 00:42:06,930 AUDIENCE: Then you would go to what is that? 833 00:42:06,930 --> 00:42:08,590 SRINI DEVADAS: Well, it's- 834 00:42:08,590 --> 00:42:10,050 AUDIENCE: In decimal? 835 00:42:10,050 --> 00:42:12,400 SRINI DEVADAS: No, in this representation-- 836 00:42:14,806 --> 00:42:15,930 whatever is easier for you. 837 00:42:15,930 --> 00:42:17,680 AUDIENCE: It would be 1200. 838 00:42:17,680 --> 00:42:19,260 SRINI DEVADAS: Perfect, 1200, which 839 00:42:19,260 --> 00:42:22,080 happens to be I think 112. 840 00:42:22,080 --> 00:42:23,490 So you go to floor 112. 841 00:42:23,490 --> 00:42:29,216 This was 96, and then you needed to add another 16 to that-- 842 00:42:29,216 --> 00:42:33,030 yep, 112 and so on. 843 00:42:33,030 --> 00:42:35,060 So I won't bore you with this. 844 00:42:35,060 --> 00:42:37,600 You get a sense of what's going on here. 845 00:42:37,600 --> 00:42:41,790 And obviously with these things, you can wave your hands 846 00:42:41,790 --> 00:42:44,100 and you get a sense of what's going on. 847 00:42:44,100 --> 00:42:48,780 But ultimately, you know, what's great about computer science 848 00:42:48,780 --> 00:42:53,040 and programming is you've got to teach a dumb computer to do 849 00:42:53,040 --> 00:42:54,720 exactly the right thing. 850 00:42:54,720 --> 00:42:57,270 And there's no two things about it in terms of you 851 00:42:57,270 --> 00:42:58,810 understand this algorithm. 852 00:42:58,810 --> 00:43:00,270 I didn't understand this algorithm 853 00:43:00,270 --> 00:43:02,630 until I coded it, fully. 854 00:43:02,630 --> 00:43:04,380 I mean, maybe I still don't understand it, 855 00:43:04,380 --> 00:43:07,950 considering the mistakes I'm making. 856 00:43:07,950 --> 00:43:12,960 But that's just because I've lost a bunch of gray cells. 857 00:43:12,960 --> 00:43:15,300 So let me show you what the code looks like. 858 00:43:15,300 --> 00:43:18,270 It's 15 lines of code. 859 00:43:18,270 --> 00:43:19,290 It's not that much. 860 00:43:19,290 --> 00:43:23,850 And as you can imagine, the representation is-- 861 00:43:23,850 --> 00:43:25,100 I could have used many things. 862 00:43:25,100 --> 00:43:27,910 I just used a list for each of these digits. 863 00:43:27,910 --> 00:43:30,360 I'm incrementing these digits. 864 00:43:30,360 --> 00:43:34,630 And I won't spend a whole lot of time on the code, 865 00:43:34,630 --> 00:43:39,780 but what I want to do before we end here is talk about-- 866 00:43:39,780 --> 00:43:43,230 actually, let me do that first, before we go to the code. 867 00:43:43,230 --> 00:43:45,930 I want to talk about the worst case here, 868 00:43:45,930 --> 00:43:48,000 because that's what this is all about. 869 00:43:48,000 --> 00:43:51,440 I want to get an equation, just like we did for 2 times 870 00:43:51,440 --> 00:43:54,540 square root of n minus 1 and all of these different things, I 871 00:43:54,540 --> 00:43:57,420 want to get an equation for the worst case in terms 872 00:43:57,420 --> 00:44:01,920 of the number of drops in relation to r and d, 873 00:44:01,920 --> 00:44:03,720 which are obviously related to n. 874 00:44:03,720 --> 00:44:06,567 Because r raised to d has to be greater than n. 875 00:44:06,567 --> 00:44:08,400 So if you kind of look at this, and it turns 876 00:44:08,400 --> 00:44:11,580 out this is not a difficult analysis-- 877 00:44:11,580 --> 00:44:13,134 if you think about it, and you start 878 00:44:13,134 --> 00:44:14,550 thinking about worst case, and you 879 00:44:14,550 --> 00:44:17,940 think about how we're incrementing these things, 880 00:44:17,940 --> 00:44:19,170 there's phases. 881 00:44:19,170 --> 00:44:23,267 And if you can think about a phase as when a ball breaks, 882 00:44:23,267 --> 00:44:24,850 you know, that's when a phase is done. 883 00:44:24,850 --> 00:44:28,050 And you move to the second phase. 884 00:44:28,050 --> 00:44:32,450 Tell me how many drops I have? 885 00:44:32,450 --> 00:44:34,894 You break it up in terms of the number of phases 886 00:44:34,894 --> 00:44:36,810 or the number of balls, whichever way you want 887 00:44:36,810 --> 00:44:39,360 to think about it, and use the same kind 888 00:44:39,360 --> 00:44:41,700 of worst-case analysis we've done with respect 889 00:44:41,700 --> 00:44:48,520 to what happens in the worst case. 890 00:44:48,520 --> 00:44:50,480 You have to keep doing a bunch of drops. 891 00:44:50,480 --> 00:44:54,720 And really, for these algorithms, the floor 127, 892 00:44:54,720 --> 00:44:56,910 the hardness coefficient of-- 893 00:44:56,910 --> 00:45:01,340 actually, the hardness coefficient of 127 894 00:45:01,340 --> 00:45:02,490 is the hard one, right? 895 00:45:02,490 --> 00:45:04,740 It's the difficult one, because you kind of go through 896 00:45:04,740 --> 00:45:06,360 and you go all the way there. 897 00:45:06,360 --> 00:45:08,220 And you do all of these things before-- you 898 00:45:08,220 --> 00:45:11,700 drop the ball a lot of times before it breaks. 899 00:45:11,700 --> 00:45:13,650 And you do that for every ball. 900 00:45:13,650 --> 00:45:15,540 That would be the worst case, right? 901 00:45:15,540 --> 00:45:17,520 So that's kind of where things are at. 902 00:45:17,520 --> 00:45:20,760 So from a symbolic standpoint, what 903 00:45:20,760 --> 00:45:23,760 is the worst case in the first phase 904 00:45:23,760 --> 00:45:27,870 in terms of the number of drops of the first ball, 905 00:45:27,870 --> 00:45:31,546 from a symbolic standpoint? 906 00:45:31,546 --> 00:45:33,170 It's the first digit I'm talking about. 907 00:45:33,170 --> 00:45:35,110 It's the first ball I'm talking about. 908 00:45:35,110 --> 00:45:39,180 And I'm dropping this ball, as you can see, 909 00:45:39,180 --> 00:45:40,560 at these different points. 910 00:45:40,560 --> 00:45:46,800 So in general, how many drops would I have before I'm either, 911 00:45:46,800 --> 00:45:50,320 I may be done completely because I know it doesn't break a floor 912 00:45:50,320 --> 00:45:52,220 128 or what have you. 913 00:45:52,220 --> 00:45:55,480 But what is the worst case when that ball breaks? 914 00:45:55,480 --> 00:45:57,554 Give me an equation. 915 00:45:57,554 --> 00:45:58,054 Someone. 916 00:46:01,350 --> 00:46:02,220 So I drop it once. 917 00:46:02,220 --> 00:46:03,430 I drop it twice. 918 00:46:03,430 --> 00:46:06,200 Am I going to drop it like, if r is 5, 919 00:46:06,200 --> 00:46:09,436 am I going to drop it 10 times? 920 00:46:09,436 --> 00:46:10,810 What am I doing in terms of the-- 921 00:46:10,810 --> 00:46:13,985 what is the upper limit in terms of this digit? 922 00:46:13,985 --> 00:46:14,859 AUDIENCE: r minus 1. 923 00:46:14,859 --> 00:46:15,900 SRINI DEVADAS: r minus 1. 924 00:46:15,900 --> 00:46:18,150 And where am I starting from? 925 00:46:18,150 --> 00:46:19,500 1, so there you go. 926 00:46:19,500 --> 00:46:20,530 What's the answer? 927 00:46:20,530 --> 00:46:21,377 AUDIENCE: r minus 1. 928 00:46:21,377 --> 00:46:22,710 SRINI DEVADAS: r minus 1, right? 929 00:46:22,710 --> 00:46:23,740 So that's it. 930 00:46:23,740 --> 00:46:30,870 So it turns out that I have r minus 1 drops in the worst 931 00:46:30,870 --> 00:46:38,020 case for the first ball. 932 00:46:38,020 --> 00:46:41,520 And, I mean, it's the same representation. 933 00:46:41,520 --> 00:46:44,920 Every digit is the same radix, obviously. 934 00:46:44,920 --> 00:46:48,220 And now, that ball broke and I moved to the second digit 935 00:46:48,220 --> 00:46:49,660 with the second ball. 936 00:46:49,660 --> 00:46:51,850 And I'm going to start again. 937 00:46:51,850 --> 00:46:56,170 And in the worst case, I could have r minus 1 drops for that 938 00:46:56,170 --> 00:46:59,519 as well, right, because I'm going to go increment from 0. 939 00:46:59,519 --> 00:47:01,060 The first one I'm going to do is a 1. 940 00:47:01,060 --> 00:47:04,370 And then I'm going to do a 2 and all the way to r minus 1. 941 00:47:04,370 --> 00:47:10,790 So r minus 1 drops for the second ball. 942 00:47:10,790 --> 00:47:14,550 And then I go all the way to d balls. 943 00:47:14,550 --> 00:47:21,726 So my answer-- yeah, go ahead. 944 00:47:21,726 --> 00:47:22,400 Over here. 945 00:47:22,400 --> 00:47:25,686 Ganatra. 946 00:47:25,686 --> 00:47:27,630 AUDIENCE: So r is 4 in this case. 947 00:47:27,630 --> 00:47:29,880 And we say the worse case is 3. 948 00:47:29,880 --> 00:47:31,543 But how do we get 3? 949 00:47:31,543 --> 00:47:34,210 Because if it doesn't break on either of those, 950 00:47:34,210 --> 00:47:38,631 on the 1000 and 2000, aren't we just done 951 00:47:38,631 --> 00:47:40,287 and that's only two drops? 952 00:47:40,287 --> 00:47:41,120 SRINI DEVADAS: Yeah. 953 00:47:41,120 --> 00:47:43,540 So it gets a little-- 954 00:47:43,540 --> 00:47:44,650 you're absolutely right. 955 00:47:44,650 --> 00:47:47,671 But if the number were 255-- 956 00:47:47,671 --> 00:47:49,420 so one of the things that's happening here 957 00:47:49,420 --> 00:47:53,440 is we've lost some precision because r raised to d needed 958 00:47:53,440 --> 00:47:55,780 to be this large number of 255. 959 00:47:55,780 --> 00:47:58,895 And this algorithm is actually going to work for 255 floors. 960 00:47:58,895 --> 00:48:00,320 AUDIENCE: Oh, it's because of n. 961 00:48:00,320 --> 00:48:01,800 SRINI DEVADAS: Yeah, because of n. 962 00:48:01,800 --> 00:48:04,719 So it's not, yeah, so that's kind of-- you know, 963 00:48:04,719 --> 00:48:06,010 it's a good question you asked. 964 00:48:06,010 --> 00:48:09,670 But unfortunately, you kind of want 965 00:48:09,670 --> 00:48:14,110 to choose a case where you have perfect precision in terms 966 00:48:14,110 --> 00:48:17,090 of r raised to d is exactly what you need it to be. 967 00:48:17,090 --> 00:48:18,700 And in that case, this analysis works. 968 00:48:18,700 --> 00:48:20,117 AUDIENCE: So it works the max end. 969 00:48:20,117 --> 00:48:21,699 SRINI DEVADAS: That's it, the max end. 970 00:48:21,699 --> 00:48:22,760 That's exactly right. 971 00:48:22,760 --> 00:48:25,110 So I mean, 255 is substantially larger than 128. 972 00:48:25,110 --> 00:48:27,160 So it kind of makes sense that, as you said, 973 00:48:27,160 --> 00:48:30,014 that you would be able to shave off certain things. 974 00:48:30,014 --> 00:48:31,680 And so you can kind of do that, but it's 975 00:48:31,680 --> 00:48:33,010 a little more involved. 976 00:48:33,010 --> 00:48:36,730 But I think the intuition is important. 977 00:48:36,730 --> 00:48:40,060 And the intuition is simply that your worst-case number 978 00:48:40,060 --> 00:48:46,240 of drops, assuming you're being a little bit imprecise, 979 00:48:46,240 --> 00:48:52,000 would simply be r minus 1 times d in the worst case. 980 00:48:52,000 --> 00:48:57,760 And this would be true for all n which is less than r 981 00:48:57,760 --> 00:49:01,370 raised to d, for all n. 982 00:49:01,370 --> 00:49:04,390 So now, just in terms of numbers, 983 00:49:04,390 --> 00:49:07,990 I had r equals 4 and d equals 4. 984 00:49:07,990 --> 00:49:12,730 And so you end up getting 12 drops for the 128 case, all 985 00:49:12,730 --> 00:49:15,710 the way to 255 as well. 986 00:49:15,710 --> 00:49:18,040 You need 12 drops in the worst case, 987 00:49:18,040 --> 00:49:23,260 even for 255 floors, which is really what you were asking. 988 00:49:23,260 --> 00:49:23,990 So good. 989 00:49:23,990 --> 00:49:24,490 Right. 990 00:49:24,490 --> 00:49:26,330 That makes sense? 991 00:49:26,330 --> 00:49:30,100 So I'm not going to spend a whole lot of time on this code, 992 00:49:30,100 --> 00:49:31,630 but that's it. 993 00:49:31,630 --> 00:49:33,940 It's converting. 994 00:49:33,940 --> 00:49:37,540 I could've used a Python library function for that, 995 00:49:37,540 --> 00:49:39,866 but that's simply a conversion because I 996 00:49:39,866 --> 00:49:40,990 wanted to print things out. 997 00:49:40,990 --> 00:49:45,472 I don't even need that, this particular subroutine, 998 00:49:45,472 --> 00:49:46,930 which is essentially something that 999 00:49:46,930 --> 00:49:52,870 is doing conversion from decimal to r-ary arithmetic. 1000 00:49:52,870 --> 00:49:57,130 And as you can see, you see I subtract and adding. 1001 00:49:57,130 --> 00:49:59,770 The adding this is the incrementation that I just 1002 00:49:59,770 --> 00:50:03,490 erased, but you do see it up there from 1 0 to 2 0. 1003 00:50:03,490 --> 00:50:04,960 That's why you add. 1004 00:50:04,960 --> 00:50:08,350 Sometimes you need to subtract, because things break. 1005 00:50:08,350 --> 00:50:12,220 And when things break, you need to go back to the highest floor 1006 00:50:12,220 --> 00:50:14,140 where the ball didn't break. 1007 00:50:14,140 --> 00:50:17,350 So that's where you do the subtraction. 1008 00:50:17,350 --> 00:50:22,780 And so this is how, if you ran this, 1009 00:50:22,780 --> 00:50:26,200 it's not the greatest user interface, I'll admit. 1010 00:50:26,200 --> 00:50:27,580 But that's not the point. 1011 00:50:33,560 --> 00:50:34,420 The Crystal. 1012 00:50:42,110 --> 00:50:47,950 So I'm going to do at 128 and 4, which is our example. 1013 00:50:47,950 --> 00:50:50,250 And it says radix shows it as ball 4. 1014 00:50:50,250 --> 00:50:52,090 Did the ball break? 1015 00:50:52,090 --> 00:50:53,740 I say no. 1016 00:50:53,740 --> 00:50:56,410 And then drop the ball. 1017 00:50:56,410 --> 00:50:59,390 Did the ball Break if I say no here, then I'm done. 1018 00:50:59,390 --> 00:51:01,480 So I'm going to go ahead and say yes. 1019 00:51:01,480 --> 00:51:06,370 And then I'm going to say no and then no. 1020 00:51:06,370 --> 00:51:07,720 I'm going to keep going-- 1021 00:51:07,720 --> 00:51:13,030 no, no, no, no-- because this is the worst case. 1022 00:51:20,770 --> 00:51:23,110 And I go to 11 in this case. 1023 00:51:23,110 --> 00:51:26,891 So you do get close in the sense that Ganatra was asking, 1024 00:51:26,891 --> 00:51:28,140 you know, could you shave off? 1025 00:51:28,140 --> 00:51:31,090 You only shaved off one because of the imprecision. 1026 00:51:31,090 --> 00:51:34,510 So there is a case where even though you would only 1027 00:51:34,510 --> 00:51:38,460 get 12 for 255, but you do get 11 for 128. 1028 00:51:38,460 --> 00:51:39,680 OK. 1029 00:51:39,680 --> 00:51:40,180 Cool. 1030 00:51:40,180 --> 00:51:40,680 Good. 1031 00:51:40,680 --> 00:51:43,000 So I'll let you look at the code. 1032 00:51:43,000 --> 00:51:47,894 If you guys can leave any time you want. 1033 00:51:47,894 --> 00:51:49,310 I just wanted to mention, I wanted 1034 00:51:49,310 --> 00:51:50,726 to go back to the one thing that I 1035 00:51:50,726 --> 00:51:53,860 didn't get that I mentioned, but I haven't gotten to do, 1036 00:51:53,860 --> 00:51:55,930 which is this is still-- 1037 00:51:55,930 --> 00:51:57,750 you could still do better. 1038 00:51:57,750 --> 00:52:00,760 You can still do better not because of what Ganatra 1039 00:52:00,760 --> 00:52:03,480 mentioned-- you know, that's just that imprecision thing, 1040 00:52:03,480 --> 00:52:05,860 but assuming even that was taken care 1041 00:52:05,860 --> 00:52:07,930 of because the numbers worked out, 1042 00:52:07,930 --> 00:52:11,200 You can do better because k can be made a different. 1043 00:52:11,200 --> 00:52:12,970 You could do unequal intervals. 1044 00:52:12,970 --> 00:52:15,820 So let me put that in the notes and you 1045 00:52:15,820 --> 00:52:16,930 can read it by yourself. 1046 00:52:16,930 --> 00:52:18,320 You had a question, Evan. 1047 00:52:18,320 --> 00:52:19,170 AUDIENCE: Yeah. 1048 00:52:19,170 --> 00:52:20,920 When you were going through the ball drops 1049 00:52:20,920 --> 00:52:23,770 and you kept answering no, that the ball did not break, 1050 00:52:23,770 --> 00:52:27,550 why did it start asking you or why did it start telling you 1051 00:52:27,550 --> 00:52:29,400 where the next ball dropped? 1052 00:52:29,400 --> 00:52:31,550 Because it went one, two, and then 1053 00:52:31,550 --> 00:52:33,810 it went also to ball three and ball four. 1054 00:52:33,810 --> 00:52:36,056 So will it just stay ball two the whole time it 1055 00:52:36,056 --> 00:52:36,680 if never broke? 1056 00:52:36,680 --> 00:52:38,180 SRINI DEVADAS: That's exactly right, 1057 00:52:38,180 --> 00:52:40,150 but that's because I'm lazy. 1058 00:52:42,740 --> 00:52:43,630 Yeah. 1059 00:52:43,630 --> 00:52:45,580 So it'd be a fine exercise to fix that. 1060 00:52:45,580 --> 00:52:47,437 I mean, that's not one of the exercises. 1061 00:52:47,437 --> 00:52:49,770 That's how you turn, you know after 30 years of teaching 1062 00:52:49,770 --> 00:52:51,269 whenever I get a question, I turn it 1063 00:52:51,269 --> 00:52:52,940 into an opportunity for homework. 1064 00:52:52,940 --> 00:52:53,631 OK. 1065 00:52:53,631 --> 00:52:54,130 Right. 1066 00:52:54,130 --> 00:52:54,820 That's how it works. 1067 00:52:54,820 --> 00:52:55,060 All right. 1068 00:52:55,060 --> 00:52:55,560 Good. 1069 00:52:55,560 --> 00:52:57,360 See you guys next time.