WEBVTT

00:00:08.516 --> 00:00:09.890
SRINI DEVADAS:
Let's get started.

00:00:09.890 --> 00:00:13.240
So today, we're going to look
at one of my favorite puzzles.

00:00:13.240 --> 00:00:17.260
I'll say right at the beginning,
that the coding associated

00:00:17.260 --> 00:00:20.440
with the puzzle is
fairly straightforward.

00:00:20.440 --> 00:00:24.940
But the analysis associated with
this puzzle is quite intricate.

00:00:24.940 --> 00:00:27.100
And in particular,
we're going to have

00:00:27.100 --> 00:00:30.970
to do worst-case analysis, which
is really a good thing for you

00:00:30.970 --> 00:00:36.160
to learn about, because
asymptotic analysis

00:00:36.160 --> 00:00:39.140
in algorithms is all
about worst case.

00:00:39.140 --> 00:00:39.640
All right.

00:00:39.640 --> 00:00:40.990
So keep that in mind.

00:00:40.990 --> 00:00:45.130
When you try to come up with
the answers to my questions,

00:00:45.130 --> 00:00:47.440
always keep in
mind that I'm going

00:00:47.440 --> 00:00:52.120
to be asking for what
happens in the worst case.

00:00:52.120 --> 00:00:55.070
In the context of our puzzle--

00:00:55.070 --> 00:00:58.000
which is called the
crystal ball puzzle,

00:00:58.000 --> 00:01:00.640
and it's fairly
popular in literature,

00:01:00.640 --> 00:01:03.550
though I've generalized
it a little bit--

00:01:03.550 --> 00:01:06.550
you have a situation
where you're

00:01:06.550 --> 00:01:10.690
supposed to figure out
the hardness coefficient

00:01:10.690 --> 00:01:12.920
of a crystal ball.

00:01:12.920 --> 00:01:14.860
And in the generalized
version, you

00:01:14.860 --> 00:01:17.770
have a set of identical
crystal balls.

00:01:17.770 --> 00:01:21.380
So how do you find this
hardness coefficient?

00:01:21.380 --> 00:01:28.410
Well, you go up to the Shanghai
Tower that has 128 floors--

00:01:28.410 --> 00:01:37.419
1 through 128-- and you drop
this ball from say, floor 1.

00:01:37.419 --> 00:01:39.210
So just you're standing
here and let's say,

00:01:39.210 --> 00:01:40.600
you just drop it from there.

00:01:40.600 --> 00:01:43.750
Or you go up to floor 2,
and you drop the ball.

00:01:43.750 --> 00:01:47.560
And the hardness
coefficient is defined

00:01:47.560 --> 00:01:53.080
as the highest floor from
which you can drop the ball

00:01:53.080 --> 00:01:56.200
and the ball does not break.

00:01:56.200 --> 00:02:04.240
So what this means
is if at floor f,

00:02:04.240 --> 00:02:15.490
the ball doesn't break,
and it breaks at f plus 1,

00:02:15.490 --> 00:02:19.480
then the hardness
coefficient is f.

00:02:19.480 --> 00:02:24.190
And you can assume a
monotonicity property

00:02:24.190 --> 00:02:30.130
that says that if it did
not break at floor f, then

00:02:30.130 --> 00:02:32.200
it won't break--

00:02:32.200 --> 00:02:39.250
this implies that it won't
break at any floor less than

00:02:39.250 --> 00:02:40.780
or equal to f.

00:02:40.780 --> 00:02:42.430
Which makes sense, right?

00:02:42.430 --> 00:02:44.660
And otherwise, it would
be kind of complicated.

00:02:44.660 --> 00:02:45.680
It would be random.

00:02:45.680 --> 00:02:49.060
And we won't be able to do
any interesting analysis.

00:02:49.060 --> 00:02:53.770
So the easy version
of the puzzle,

00:02:53.770 --> 00:02:56.470
of course, which is not
particularly interesting,

00:02:56.470 --> 00:03:02.090
is if you needed to
get this exactly--

00:03:02.090 --> 00:03:05.270
so your job depends on getting
that number exactly right--

00:03:05.270 --> 00:03:06.400
so there's no shortcuts.

00:03:06.400 --> 00:03:07.524
You've got to get it right.

00:03:07.524 --> 00:03:09.850
You can't be off
by one either way.

00:03:09.850 --> 00:03:15.670
And you have only one ball.

00:03:15.670 --> 00:03:17.270
So you only have one ball.

00:03:17.270 --> 00:03:19.319
So what can you do if
you only have one ball

00:03:19.319 --> 00:03:20.860
and you have to get
it exactly right?

00:03:20.860 --> 00:03:22.969
AUDIENCE: Just try every floor.

00:03:22.969 --> 00:03:24.260
SRINI DEVADAS: Try every floor.

00:03:24.260 --> 00:03:24.760
Right.

00:03:24.760 --> 00:03:29.510
And but more important,
try it from lower floors

00:03:29.510 --> 00:03:30.920
to higher floors, right?

00:03:30.920 --> 00:03:34.670
So you drop it at floor 1.

00:03:34.670 --> 00:03:42.900
So one ball, you
drop from floor 1.

00:03:42.900 --> 00:03:46.460
And if it breaks, then
your hardness coefficient

00:03:46.460 --> 00:03:50.760
is, you return 0.

00:03:50.760 --> 00:03:56.130
But then if you keep going and
let's say, you drop off dot,

00:03:56.130 --> 00:03:57.420
dot, dot--

00:03:57.420 --> 00:04:05.310
of from floor 65 and it breaks,
and you've gone in order,

00:04:05.310 --> 00:04:09.630
then the hardness
coefficient is 64.

00:04:09.630 --> 00:04:10.620
So that's easy enough.

00:04:10.620 --> 00:04:13.320
There's no optimization here.

00:04:13.320 --> 00:04:15.160
It's the only thing you can do.

00:04:15.160 --> 00:04:18.810
So that's why it's not
particularly interesting.

00:04:18.810 --> 00:04:21.149
Now if you have identical
crystal balls that

00:04:21.149 --> 00:04:23.940
are guaranteed, they
were manufactured

00:04:23.940 --> 00:04:27.885
in exactly the same way,
et cetera, et cetera, then

00:04:27.885 --> 00:04:29.760
obviously, things get
a lot more interesting,

00:04:29.760 --> 00:04:32.460
because you can try
to minimize something.

00:04:32.460 --> 00:04:34.230
And I'm going to
write it over here,

00:04:34.230 --> 00:04:38.940
because I want you to keep this
in mind throughout the lecture.

00:04:38.940 --> 00:04:48.870
Our goal here is to minimize
the worst-case number

00:04:48.870 --> 00:04:52.980
of drops for our problem.

00:04:52.980 --> 00:04:58.440
And we want to solve the
problem in the general case

00:04:58.440 --> 00:05:00.420
with d balls.

00:05:00.420 --> 00:05:02.190
And I should say,
d identical balls.

00:05:05.650 --> 00:05:07.930
But more important, the
reason I'm writing this up

00:05:07.930 --> 00:05:14.550
is, as I said before, we need
to focus on the worst case.

00:05:14.550 --> 00:05:19.010
So it may be the case that
for a given set of balls,

00:05:19.010 --> 00:05:23.520
your algorithm does very well,
in the sense that the hardness

00:05:23.520 --> 00:05:25.820
coefficient happened to be 42.

00:05:25.820 --> 00:05:34.480
And you picked the floor
as of 41 to begin with.

00:05:34.480 --> 00:05:36.727
And so you dropped from
41, and it doesn't break.

00:05:36.727 --> 00:05:38.560
And then you go to 42,
and it doesn't break.

00:05:38.560 --> 00:05:41.170
And you go to 43, and it breaks.

00:05:41.170 --> 00:05:42.820
And you go, oh, I
only needed two drops

00:05:42.820 --> 00:05:46.570
for this particular problem.

00:05:46.570 --> 00:05:50.920
But if the hardness
coefficient happened to be 65

00:05:50.920 --> 00:05:53.410
and you started from
floor 41, I mean

00:05:53.410 --> 00:05:56.500
you'd need 20-odd
number of drops.

00:05:56.500 --> 00:05:59.590
So you have to be
careful in that you

00:05:59.590 --> 00:06:02.200
have to analyze whatever
strategy you come up

00:06:02.200 --> 00:06:07.030
with in the case where the
hardness coefficient could

00:06:07.030 --> 00:06:08.590
be pretty much anything.

00:06:08.590 --> 00:06:12.640
I mean it could be 0 or it could
be 128 in our Shanghai Tower

00:06:12.640 --> 00:06:14.560
example.

00:06:14.560 --> 00:06:17.770
So the first
interesting question

00:06:17.770 --> 00:06:23.930
is, what happens when I
have two identical balls?

00:06:23.930 --> 00:06:30.010
So, oh, before we
do that, according

00:06:30.010 --> 00:06:37.330
to my metric here, what is
the worst-case number of drops

00:06:37.330 --> 00:06:39.530
for one ball?

00:06:39.530 --> 00:06:42.180
What is the worst-case
number of drops for one ball?

00:06:44.750 --> 00:06:46.690
Give me a number.

00:06:46.690 --> 00:06:48.762
Someone else.

00:06:48.762 --> 00:06:49.470
Yeah, back there.

00:06:49.470 --> 00:06:51.820
AUDIENCE: 128.

00:06:51.820 --> 00:06:54.670
SRINI DEVADAS:
128, exactly right.

00:06:54.670 --> 00:06:56.230
So it's 128,
because I could have

00:06:56.230 --> 00:06:59.590
had a ball that doesn't
break from the 128 floor.

00:06:59.590 --> 00:07:01.150
I don't know that to begin with.

00:07:01.150 --> 00:07:02.770
I have to start with floor 1.

00:07:02.770 --> 00:07:06.100
And I got to go 1, 2,
3, 4, et cetera, right?

00:07:06.100 --> 00:07:08.080
So the worst case
for one ball is 128.

00:07:08.080 --> 00:07:10.870
And you can't do
better than that.

00:07:10.870 --> 00:07:15.070
But of course, it gets more
interesting when d is large.

00:07:15.070 --> 00:07:19.030
So our goal now is to
discover an algorithm that

00:07:19.030 --> 00:07:22.640
is going to minimize the
worst-case number of drops

00:07:22.640 --> 00:07:25.060
for d equals 2.

00:07:25.060 --> 00:07:28.210
That's our current goal, right?

00:07:28.210 --> 00:07:32.470
And then we'll look at more,
even more general things.

00:07:32.470 --> 00:07:36.120
So how would you do things
if you had d equals 2?

00:07:39.400 --> 00:07:43.510
Just, it doesn't matter if you
don't get our algorithm right.

00:07:43.510 --> 00:07:44.330
We'll analyze it.

00:07:44.330 --> 00:07:45.560
You propose something.

00:07:45.560 --> 00:07:46.509
We'll analyze it.

00:07:46.509 --> 00:07:48.550
And then we'll decide if
we can do better or not.

00:07:48.550 --> 00:07:48.700
Yeah.

00:07:48.700 --> 00:07:49.525
Go ahead.

00:07:49.525 --> 00:07:52.500
AUDIENCE: Drop the ball from
the halfway point, say 64.

00:07:52.500 --> 00:07:54.855
If it breaks there, then
you can use the bottom half

00:07:54.855 --> 00:07:55.730
of what we are using.

00:07:55.730 --> 00:07:56.640
SRINI DEVADAS: Use
the bottom half.

00:07:56.640 --> 00:07:56.970
Excellent.

00:07:56.970 --> 00:07:57.678
What's your name?

00:07:57.678 --> 00:07:58.810
AUDIENCE: Dhaman.

00:07:58.810 --> 00:07:59.120
SRINI DEVADAS: Sorry.

00:07:59.120 --> 00:07:59.850
AUDIENCE: Dhaman.

00:07:59.850 --> 00:08:00.910
SRINI DEVADAS: Dhaman.

00:08:00.910 --> 00:08:06.570
So Dhaman says that we should
do binary search, which

00:08:06.570 --> 00:08:11.890
is very intuitive and
kind of makes sense

00:08:11.890 --> 00:08:15.040
in a lot of context,
including this one.

00:08:15.040 --> 00:08:21.970
And so let's drop from 64.

00:08:21.970 --> 00:08:25.450
And the reason she picked that
is because 128 divided by 2

00:08:25.450 --> 00:08:26.710
is 64.

00:08:26.710 --> 00:08:39.340
And if it breaks, then you need
to look at the interval, 1, 63

00:08:39.340 --> 00:08:41.890
because if it--

00:08:41.890 --> 00:08:42.439
I'm sorry.

00:08:42.439 --> 00:08:43.230
Yeah, that's right.

00:08:43.230 --> 00:08:46.690
If it breaks, you need to
look at the interval 1, 63

00:08:46.690 --> 00:08:47.790
with a second ball.

00:08:47.790 --> 00:08:50.350
I mean that first ball is gone.

00:08:50.350 --> 00:08:53.510
It's shards of crystal or
whatever you want to call it.

00:08:53.510 --> 00:08:55.120
So you just have
the second ball.

00:08:55.120 --> 00:08:58.860
And so now at this point,
you have no choice.

00:08:58.860 --> 00:09:00.940
You only have one ball left.

00:09:00.940 --> 00:09:02.860
As I mentioned, your
job depends on getting

00:09:02.860 --> 00:09:05.350
that hardness coefficient
right, so you can only

00:09:05.350 --> 00:09:09.910
use that first algorithm
in terms of starting from 1

00:09:09.910 --> 00:09:11.770
and going all the way to 63.

00:09:11.770 --> 00:09:13.840
And so what is the
worst-case number

00:09:13.840 --> 00:09:17.560
of drops given that
Dhaman picked the midpoint

00:09:17.560 --> 00:09:19.210
for this algorithm?

00:09:19.210 --> 00:09:21.880
Someone else.

00:09:21.880 --> 00:09:22.380
Yeah.

00:09:22.380 --> 00:09:22.880
Go ahead.

00:09:23.380 --> 00:09:25.860
AUDIENCE: It's 63.

00:09:25.860 --> 00:09:29.190
SRINI DEVADAS: It's 63.

00:09:29.190 --> 00:09:31.770
No actually, you're off by 1.

00:09:31.770 --> 00:09:36.780
Because I did drop the
ball here, so close.

00:09:36.780 --> 00:09:42.400
So the first ball was
dropped once, one drop.

00:09:42.400 --> 00:09:48.090
And this one, if in fact, the
hardness coefficient was 64,

00:09:48.090 --> 00:09:52.200
you drop the second
ball 63 times.

00:09:52.200 --> 00:09:56.960
And that would be the worst
case in this instance.

00:09:56.960 --> 00:10:00.300
Now, I've skipped
over one little thing.

00:10:00.300 --> 00:10:02.010
The fact that this
is the midpoint

00:10:02.010 --> 00:10:05.160
implies that this is, in
fact, the correct answer.

00:10:05.160 --> 00:10:06.960
But we do have to analyze.

00:10:06.960 --> 00:10:09.000
I mean remember, you know,
when you do analysis,

00:10:09.000 --> 00:10:11.920
you do have to cover the
entire spectrum here.

00:10:11.920 --> 00:10:18.030
So you do have to look at the
case where the drop from 64

00:10:18.030 --> 00:10:18.960
does not break.

00:10:22.680 --> 00:10:30.150
And in that case, you're
looking at 65 through 128.

00:10:30.150 --> 00:10:33.330
But you do have
two balls for this.

00:10:33.330 --> 00:10:34.410
It didn't break.

00:10:34.410 --> 00:10:37.110
So you still have
two balls for that.

00:10:37.110 --> 00:10:40.440
And so you can imagine that,
now that you have two balls

00:10:40.440 --> 00:10:43.860
and you have a small
interval, that you're

00:10:43.860 --> 00:10:46.230
going to be able to do
something like 32 for that.

00:10:46.230 --> 00:10:48.162
The bottom line is,
you don't even need to.

00:10:48.162 --> 00:10:49.620
You can get a little
bit lazy here.

00:10:49.620 --> 00:10:51.786
You don't really need to
analyze what the number is,

00:10:51.786 --> 00:10:53.870
because you know that
this number, 63 plus 1,

00:10:53.870 --> 00:10:55.230
is larger than that--

00:10:55.230 --> 00:10:56.984
because you wanted
worst case, right?

00:10:56.984 --> 00:10:58.650
So this is the kind
of thing that you'll

00:10:58.650 --> 00:11:01.440
have to do when you do
algorithmic analysis.

00:11:01.440 --> 00:11:05.820
Maybe not exactly in a
puzzle situation like this,

00:11:05.820 --> 00:11:08.730
but you have to look
at all the cases.

00:11:08.730 --> 00:11:11.879
And you have to find kind
of the max of all the cases.

00:11:11.879 --> 00:11:13.170
So is this the best you can do?

00:11:15.510 --> 00:11:16.010
Yeah.

00:11:16.010 --> 00:11:16.809
Go ahead.

00:11:16.809 --> 00:11:21.549
AUDIENCE: You could break the
128 into smaller sections using

00:11:21.549 --> 00:11:22.298
the first ball.

00:11:22.298 --> 00:11:24.294
So instead of
breaking it in half,

00:11:24.294 --> 00:11:26.496
you could do it every 16 floors.

00:11:26.496 --> 00:11:27.787
SRINI DEVADAS: Every 16 floors.

00:11:27.787 --> 00:11:28.287
Yeah.

00:11:28.287 --> 00:11:29.783
AUDIENCE: Starting
from the bottom,

00:11:29.783 --> 00:11:32.200
and then once the
first ball breaks,

00:11:32.200 --> 00:11:35.490
then you have a smaller interval
to test with the second ball.

00:11:35.490 --> 00:11:40.240
So let's say it
breaks at the 32nd.

00:11:40.240 --> 00:11:44.730
Then you have to only test for
16 to 32, instead of 1 to 32.

00:11:44.730 --> 00:11:45.980
SRINI DEVADAS: That's correct.

00:11:45.980 --> 00:11:46.500
Yeah.

00:11:46.500 --> 00:11:47.550
So it shows how you can do it--

00:11:47.550 --> 00:11:49.200
I mean, this is,
obviously there's not

00:11:49.200 --> 00:11:51.450
going to be a whole
lot of optimization

00:11:51.450 --> 00:11:53.640
here with d equals 2.

00:11:53.640 --> 00:11:56.670
But you'd be surprised
as to how much better you

00:11:56.670 --> 00:12:00.030
can do than 64, which
is obviously a big step

00:12:00.030 --> 00:12:03.930
forward from 128.

00:12:03.930 --> 00:12:07.690
But you can go a few
more steps as well.

00:12:07.690 --> 00:12:09.594
And so what was your name?

00:12:09.594 --> 00:12:10.380
AUDIENCE: Lucy.

00:12:10.380 --> 00:12:11.213
SRINI DEVADAS: Lucy.

00:12:11.213 --> 00:12:13.460
So Lucy has it right.

00:12:13.460 --> 00:12:18.420
It's a similar idea, except
you don't necessarily

00:12:18.420 --> 00:12:22.280
need to do the
intuitive binary search.

00:12:22.280 --> 00:12:26.220
It's actually kind of different
from that because of the way

00:12:26.220 --> 00:12:28.020
this problem is structured.

00:12:28.020 --> 00:12:34.860
And what her idea is is
that we'll look at something

00:12:34.860 --> 00:12:36.730
smaller, a smaller interval.

00:12:41.450 --> 00:12:47.520
And she picked 16, so let's
go 16, 32 is our algorithm.

00:12:47.520 --> 00:12:52.940
It's something that goes 16,
32, 48, 64, dot, dot, dot.

00:12:52.940 --> 00:12:57.500
And maybe gets up all the way
to I guess, yeah 128 and 16.

00:13:00.582 --> 00:13:03.270
So 128 is a multiple
of 16, right?

00:13:03.270 --> 00:13:05.990
Some math person
make sure I'm right.

00:13:09.290 --> 00:13:10.290
So what happens here?

00:13:10.290 --> 00:13:12.140
So let's analyze
this to make sure

00:13:12.140 --> 00:13:15.320
that we're good in
terms of an improvement.

00:13:15.320 --> 00:13:16.980
We have to be careful.

00:13:16.980 --> 00:13:20.990
So let's just
arbitrarily say that it

00:13:20.990 --> 00:13:25.310
broke at 64, because that's
kind of what happened over

00:13:25.310 --> 00:13:27.110
in that previous algorithm too.

00:13:27.110 --> 00:13:31.280
So that means I've done
four drops without a break.

00:13:34.750 --> 00:13:36.190
I'm sorry, four drops.

00:13:36.190 --> 00:13:41.650
Three drops without a
break and four drops total.

00:13:41.650 --> 00:13:42.970
So I've done four drops.

00:13:42.970 --> 00:13:47.060
But now the cool thing is I
don't have to start from 16.

00:13:47.060 --> 00:13:49.990
I know it didn't break at 48.

00:13:49.990 --> 00:13:58.200
So I can now look at four
drops till break of first ball.

00:13:58.200 --> 00:14:00.560
That is what I wanted to say.

00:14:00.560 --> 00:14:05.470
And now, I can look
at the interval 49.

00:14:05.470 --> 00:14:08.110
Because 48, it did not break--

00:14:08.110 --> 00:14:12.100
49 to 63, inclusive.

00:14:12.100 --> 00:14:14.860
And so if you do
that, that's 15.

00:14:14.860 --> 00:14:16.280
It's 15.

00:14:16.280 --> 00:14:19.430
The difference is 14, but
it's inclusive, so it's 15.

00:14:19.430 --> 00:14:24.540
So 15 plus 4, that is 19 drops.

00:14:24.540 --> 00:14:25.180
Yeah.

00:14:25.180 --> 00:14:25.800
Back there.

00:14:25.800 --> 00:14:27.175
AUDIENCE: Wouldn't
the worst case

00:14:27.175 --> 00:14:28.800
though be when it's at 127.

00:14:28.800 --> 00:14:32.890
SRINI DEVADAS: Yes,
that's absolutely correct.

00:14:32.890 --> 00:14:35.670
So 19 is not the worst case.

00:14:35.670 --> 00:14:38.430
I mean it's looking good,
but this was exactly

00:14:38.430 --> 00:14:41.490
the point I was trying to
make right from the beginning.

00:14:41.490 --> 00:14:42.630
You are not done yet.

00:14:42.630 --> 00:14:45.630
I mean, you cannot just go off
and say that 19 is the worst

00:14:45.630 --> 00:14:46.872
case for this algorithm.

00:14:46.872 --> 00:14:48.330
The worst case for
this algorithm--

00:14:48.330 --> 00:14:51.810
this particular algorithm
occurs at a different point

00:14:51.810 --> 00:14:55.380
from the original
algorithm that went midway.

00:14:55.380 --> 00:14:56.960
Every algorithm is different.

00:14:56.960 --> 00:15:00.210
The parameters are
different here.

00:15:00.210 --> 00:15:02.960
And so I'm just calling
a different algorithm.

00:15:02.960 --> 00:15:06.390
But the case that
would happen is

00:15:06.390 --> 00:15:08.490
I guess you would have 112 here.

00:15:08.490 --> 00:15:11.760
And then you'd go
1, 2, so how many?

00:15:11.760 --> 00:15:14.537
So let's say it gets to 128.

00:15:14.537 --> 00:15:16.620
And if it doesn't break,
then you're clearly done.

00:15:16.620 --> 00:15:18.600
So that's not the worst case.

00:15:18.600 --> 00:15:24.990
So if it gets to 128 and it
breaks, then so how many drops

00:15:24.990 --> 00:15:25.650
have we done?

00:15:25.650 --> 00:15:28.530
128 divided 16 is what?

00:15:28.530 --> 00:15:29.400
Is 8.

00:15:29.400 --> 00:15:30.720
Are you sure?

00:15:30.720 --> 00:15:31.740
You're all sure?

00:15:31.740 --> 00:15:32.240
OK.

00:15:32.240 --> 00:15:33.780
Then I'm sure.

00:15:33.780 --> 00:15:36.600
So that's eight drops.

00:15:36.600 --> 00:15:42.720
And now you need
to do 113 to 127.

00:15:42.720 --> 00:15:45.210
And that we know is 15.

00:15:45.210 --> 00:15:47.420
So that's 23.

00:15:47.420 --> 00:15:48.850
All right.

00:15:48.850 --> 00:15:53.320
So the best you have
done here is 23.

00:15:53.320 --> 00:15:56.470
But are we satisfied?

00:15:56.470 --> 00:15:57.970
We're never satisfied.

00:15:57.970 --> 00:16:00.690
We always want to do
a little bit better.

00:16:00.690 --> 00:16:03.640
So how did she pick 16?

00:16:03.640 --> 00:16:05.220
Why did you come
up with 16, Lucy?

00:16:05.220 --> 00:16:06.686
It looked like a nice number?

00:16:06.686 --> 00:16:08.630
AUDIENCE: I just
figured it divides 128.

00:16:08.630 --> 00:16:11.970
SRINI DEVADAS: You
just figured, oh, nice.

00:16:11.970 --> 00:16:15.280
So if you'd had 17, then we
would have had real trouble

00:16:15.280 --> 00:16:16.640
with all these numbers.

00:16:16.640 --> 00:16:17.140
Yeah.

00:16:17.140 --> 00:16:17.470
Go ahead.

00:16:17.470 --> 00:16:17.970
Josh.

00:16:17.970 --> 00:16:20.620
AUDIENCE: Think you can
do a little bit better

00:16:20.620 --> 00:16:23.460
if you get closer to
the square root of 128?

00:16:23.460 --> 00:16:25.190
SRINI DEVADAS: Ah,
beautiful, beautiful.

00:16:25.190 --> 00:16:26.450
I love you guys.

00:16:26.450 --> 00:16:29.140
You guys make it so easy.

00:16:29.140 --> 00:16:30.640
So that's the next
thing I was going

00:16:30.640 --> 00:16:34.300
to try and pry out of
you, but you got it out

00:16:34.300 --> 00:16:37.720
without much effort from me.

00:16:37.720 --> 00:16:42.580
So you can kind of get
analytical here as opposed

00:16:42.580 --> 00:16:47.770
to doing things empirically,
which was try a number

00:16:47.770 --> 00:16:50.420
and figure out what happened,
which is kind of what we did.

00:16:50.420 --> 00:16:52.570
And you can be a little
more analytical about this

00:16:52.570 --> 00:16:55.960
and you could say,
hey, suppose I

00:16:55.960 --> 00:16:59.980
want to find this number k,
which is kind of the 64--

00:16:59.980 --> 00:17:01.900
started with 64, went to 16.

00:17:01.900 --> 00:17:04.810
And now, we're thinking we
can do better than 16 even.

00:17:04.810 --> 00:17:10.810
And we start dropping
things at k, 2k, 3k.

00:17:10.810 --> 00:17:15.099
And I want to get
this exactly right.

00:17:15.099 --> 00:17:17.069
So let me look at my notes.

00:17:17.069 --> 00:17:18.930
And you go dot, dot, dot--

00:17:18.930 --> 00:17:26.250
n divided by k minus 1 times
k and n divided by k times

00:17:26.250 --> 00:17:28.140
k, which is obviously m.

00:17:28.140 --> 00:17:30.630
And so I'm trying to,
I'm trying to write it--

00:17:30.630 --> 00:17:32.490
I'm trying to get this k.

00:17:32.490 --> 00:17:33.810
I want to write something.

00:17:33.810 --> 00:17:36.120
I want to minimize
at some quantity

00:17:36.120 --> 00:17:38.670
and discover the k that is
going to give me my best

00:17:38.670 --> 00:17:41.460
case in the worst case--

00:17:41.460 --> 00:17:43.500
minimize the worst case, right?

00:17:43.500 --> 00:17:46.050
So that's really what I want.

00:17:46.050 --> 00:17:49.930
So if you look at
what happens here,

00:17:49.930 --> 00:17:54.420
you're in a situation
where you have to now,

00:17:54.420 --> 00:17:55.530
you have a count, right?

00:17:55.530 --> 00:17:57.160
I mean, well, what
do you want to--

00:17:57.160 --> 00:18:03.420
we've decided that k is
this general strategy.

00:18:03.420 --> 00:18:05.280
And we don't quite
know what k is.

00:18:05.280 --> 00:18:09.900
And we have to do some analysis
of the worst-case situation

00:18:09.900 --> 00:18:13.740
in order to find the k that
would work well for us.

00:18:13.740 --> 00:18:18.930
And so the way you do this is
by, essentially, the analysis

00:18:18.930 --> 00:18:20.430
we've done so far.

00:18:20.430 --> 00:18:28.380
And kind of we have a nice,
the point that was made here,

00:18:28.380 --> 00:18:32.100
I think, it was Kevin who said
we have to go to the last drop,

00:18:32.100 --> 00:18:36.370
because that's kind of the
case where it's the worst case.

00:18:36.370 --> 00:18:37.860
And that's similar here as well.

00:18:37.860 --> 00:18:40.950
Because if you go
up all the way here

00:18:40.950 --> 00:18:43.560
and then it breaks, if
it doesn't break here,

00:18:43.560 --> 00:18:44.520
you're done.

00:18:44.520 --> 00:18:48.960
If it breaks here, then you've
got, you've done a lot of drops

00:18:48.960 --> 00:18:51.180
before it broke that first ball.

00:18:51.180 --> 00:18:54.750
And so that's the situation
which you can pull in

00:18:54.750 --> 00:18:56.850
as your worst-case situation.

00:18:56.850 --> 00:18:59.880
So that's exactly
what we did before.

00:18:59.880 --> 00:19:05.110
And so if you really
think about it,

00:19:05.110 --> 00:19:20.290
assume first ball doesn't
break until the last drop is

00:19:20.290 --> 00:19:22.390
the worst-case situation.

00:19:22.390 --> 00:19:26.530
And so what does that mean
from a symbolic standpoint

00:19:26.530 --> 00:19:29.815
of with respect to the
number of drops of--

00:19:29.815 --> 00:19:34.240
so the number of drops in
this case of the first ball?

00:19:34.240 --> 00:19:37.781
Can someone give me
an equation for it?

00:19:37.781 --> 00:19:38.280
Yeah.

00:19:38.280 --> 00:19:39.204
Go ahead.

00:19:39.204 --> 00:19:40.128
AUDIENCE: n over k.

00:19:40.128 --> 00:19:41.128
SRINI DEVADAS: n over k.

00:19:41.128 --> 00:19:41.930
You a Kevin too?

00:19:41.930 --> 00:19:42.230
Yeah.

00:19:42.230 --> 00:19:42.730
OK.

00:19:44.860 --> 00:19:45.981
So n over k.

00:19:45.981 --> 00:19:46.480
Right.

00:19:46.480 --> 00:19:48.470
So that's what happened there.

00:19:48.470 --> 00:19:51.010
Now, well, it broke, right?

00:19:51.010 --> 00:19:52.610
That's what we decided here.

00:19:52.610 --> 00:20:08.242
And so now, you need to
search n k plus 1 to--

00:20:08.242 --> 00:20:10.480
well, you got to go
all the way there.

00:20:10.480 --> 00:20:14.820
I'm sorry, it broke there,
so you got to go n minus 1.

00:20:20.310 --> 00:20:22.535
This is the same as
what I wrote before,

00:20:22.535 --> 00:20:23.910
but it's just that
I had numbers.

00:20:23.910 --> 00:20:25.660
And it was kind of
easier to write numbers

00:20:25.660 --> 00:20:26.940
than write these symbols.

00:20:26.940 --> 00:20:31.740
But we have to do this because
we don't know what k is.

00:20:31.740 --> 00:20:35.720
So if you look at
how many drops are--

00:20:35.720 --> 00:20:39.720
and if you're just going
to do this, you get--

00:20:39.720 --> 00:20:45.750
if you notice, this
gets simplified to n.

00:20:45.750 --> 00:20:48.710
Yeah, so this is n,
and then you have

00:20:48.710 --> 00:20:55.570
minus k plus 1, all
the way to n minus 1.

00:20:55.570 --> 00:20:59.630
So if you subtract, and
this is a plus 1 here.

00:20:59.630 --> 00:21:00.860
Boy, this is confusing.

00:21:00.860 --> 00:21:02.300
I'm not good at math.

00:21:02.300 --> 00:21:03.800
I'm definitely not good at math.

00:21:03.800 --> 00:21:12.746
So this is k minus 1
drops in the worst case.

00:21:12.746 --> 00:21:14.640
All right.

00:21:14.640 --> 00:21:18.940
So you now need to add
these two things together.

00:21:18.940 --> 00:21:26.810
So what we'd like to do
is minimize n divide it

00:21:26.810 --> 00:21:29.600
by k, which is this plus that.

00:21:36.030 --> 00:21:38.220
We want to minimize
this function.

00:21:38.220 --> 00:21:39.710
And it's a constant.

00:21:39.710 --> 00:21:41.610
k is a variable.

00:21:41.610 --> 00:21:44.580
And for a given n, we want
to discover the k that

00:21:44.580 --> 00:21:45.960
minimizes this quantity.

00:21:45.960 --> 00:21:48.390
And it goes back
to the intuition

00:21:48.390 --> 00:21:52.110
that I think Josh had
about the square root of n.

00:21:52.110 --> 00:21:54.571
So again, high school calculus
here, which I've forgotten,

00:21:54.571 --> 00:21:56.820
which hopefully you haven't
because you still need it.

00:21:56.820 --> 00:21:57.320
I don't.

00:22:00.780 --> 00:22:03.930
And you end up
essentially saying

00:22:03.930 --> 00:22:06.450
you can turn this
into two raised

00:22:06.450 --> 00:22:13.710
to the square root of n minus
1 drops in the worst case.

00:22:13.710 --> 00:22:16.420
All right.

00:22:16.420 --> 00:22:23.120
So you can take the floor
of that, and that's 11.

00:22:23.120 --> 00:22:24.860
So that's that square root of n.

00:22:24.860 --> 00:22:28.250
So 2 times 11 minus 1 is 21.

00:22:28.250 --> 00:22:29.530
So you can do 21.

00:22:29.530 --> 00:22:32.944
So you think 21 is the optimum?

00:22:32.944 --> 00:22:33.600
Yeah, Kevin.

00:22:33.600 --> 00:22:35.058
AUDIENCE: Can we
do a little better

00:22:35.058 --> 00:22:36.360
if we make it go smaller?

00:22:36.360 --> 00:22:37.300
SRINI DEVADAS: Yes.

00:22:37.300 --> 00:22:39.340
You can do a little bit better.

00:22:39.340 --> 00:22:40.740
I don't want to
do this just yet.

00:22:40.740 --> 00:22:41.070
Hopefully.

00:22:41.070 --> 00:22:41.740
We'll have time.

00:22:41.740 --> 00:22:44.970
We'll get to that because I want
to go to the d greater than 2

00:22:44.970 --> 00:22:47.200
case, but you're
absolutely right.

00:22:47.200 --> 00:22:51.360
It turns out that you can
do, this would give you 21,

00:22:51.360 --> 00:22:54.490
as I mentioned, 22 minus 1.

00:22:54.490 --> 00:22:57.060
It turns out, you can do
better but by a few drops.

00:22:57.060 --> 00:23:01.740
And the assumption we made here,
what is the assumption that we

00:23:01.740 --> 00:23:03.600
made here?

00:23:03.600 --> 00:23:05.689
When I wrote this, what
assumption did I make,

00:23:05.689 --> 00:23:07.730
which is kind of similar
to what Kevin just said,

00:23:07.730 --> 00:23:10.250
but not exactly?

00:23:10.250 --> 00:23:14.830
Would someone point out the
assumption that was made here?

00:23:14.830 --> 00:23:16.189
Not you.

00:23:16.189 --> 00:23:16.730
Someone else.

00:23:20.380 --> 00:23:20.880
All right.

00:23:20.880 --> 00:23:21.330
Ganatra.

00:23:21.330 --> 00:23:21.829
Go for it.

00:23:21.829 --> 00:23:23.400
AUDIENCE: The
intervals are the same.

00:23:23.400 --> 00:23:24.233
SRINI DEVADAS: Yeah.

00:23:24.233 --> 00:23:26.220
The intervals are equal.

00:23:26.220 --> 00:23:30.150
So this, look, it's
a different problem.

00:23:30.150 --> 00:23:35.120
If it didn't break here,
then I got a smaller--

00:23:35.120 --> 00:23:36.950
I have two balls.

00:23:36.950 --> 00:23:38.780
And I had a smaller problem.

00:23:38.780 --> 00:23:41.764
So why would I use the same k?

00:23:41.764 --> 00:23:43.430
I mean, that kind of
makes sense, right?

00:23:43.430 --> 00:23:46.550
So now, it's more complicated
and hopefully, we'll

00:23:46.550 --> 00:23:47.530
get to that.

00:23:47.530 --> 00:23:49.896
But I want to do
other things first.

00:23:49.896 --> 00:23:50.770
So that's the reason.

00:23:50.770 --> 00:23:54.720
So at least you know why
21 isn't optimal, right?

00:23:54.720 --> 00:23:58.370
It's because of this constraint
that we placed on ourselves

00:23:58.370 --> 00:24:01.310
in order to do this
analysis to get to the point

00:24:01.310 --> 00:24:06.500
where we had 2 times square root
of n minus 1 that corresponds

00:24:06.500 --> 00:24:09.740
to the equal
interval constraint.

00:24:09.740 --> 00:24:10.290
So good.

00:24:10.290 --> 00:24:10.790
All right.

00:24:10.790 --> 00:24:12.590
Any questions about this?

00:24:12.590 --> 00:24:14.090
So as you can
imagine, I'm not going

00:24:14.090 --> 00:24:18.410
to show you code that computes 2
times square root of n minus 1.

00:24:18.410 --> 00:24:20.720
So that doesn't
make sense, but we

00:24:20.720 --> 00:24:24.230
do have code to look at
that corresponds to the case

00:24:24.230 --> 00:24:27.320
where d could be
3, d could be 4.

00:24:27.320 --> 00:24:30.050
And then obviously, it gets
more interesting in terms

00:24:30.050 --> 00:24:31.670
of what your strategy is.

00:24:31.670 --> 00:24:34.490
And we won't go with optimizing.

00:24:34.490 --> 00:24:38.690
In fact, there's an open problem
as to what the optimum strategy

00:24:38.690 --> 00:24:40.530
would be for arbitrary d--

00:24:40.530 --> 00:24:42.440
d meaning the number of balls--

00:24:42.440 --> 00:24:44.780
if it's 5, 6, whatever--

00:24:44.780 --> 00:24:47.180
partly because of what I just
talked about with respect

00:24:47.180 --> 00:24:50.480
to you need to utilize
unequal intervals.

00:24:50.480 --> 00:24:52.850
But the natural
thing to do now is

00:24:52.850 --> 00:24:57.350
to move to d equals a
arbitrary number of balls

00:24:57.350 --> 00:25:03.230
and stick with kind of the same
algorithmic approach of having

00:25:03.230 --> 00:25:05.090
equal intervals.

00:25:05.090 --> 00:25:07.190
But the question is,
what are these intervals?

00:25:07.190 --> 00:25:10.640
What and how would we find
the size of these intervals?

00:25:10.640 --> 00:25:12.630
Because it's kind of
not immediately obvious

00:25:12.630 --> 00:25:14.132
if I told you d was 5.

00:25:14.132 --> 00:25:16.340
And you don't want to just
keep the three balls away,

00:25:16.340 --> 00:25:18.800
and sell them later, and
just use the two, right?

00:25:18.800 --> 00:25:21.270
That doesn't give
you your minimum.

00:25:21.270 --> 00:25:23.370
It doesn't minimize
your worst case.

00:25:23.370 --> 00:25:27.460
You want to use these balls.

00:25:27.460 --> 00:25:27.990
Cool.

00:25:27.990 --> 00:25:28.590
All right.

00:25:28.590 --> 00:25:38.930
So let's talk a little bit
about if I had d equals 3,

00:25:38.930 --> 00:25:41.690
I mean again, you can
use your intuition.

00:25:41.690 --> 00:25:45.740
What do you think we should do?

00:25:45.740 --> 00:25:48.260
What happened for d equals 2
with respect to that interval

00:25:48.260 --> 00:25:51.590
and how is it related to n?

00:25:51.590 --> 00:25:55.550
So what do you think, just
wild guess, maybe even?

00:25:55.550 --> 00:25:57.290
What do you think
we need to do if we

00:25:57.290 --> 00:26:00.200
had d equals 3 for that
very first ball in terms

00:26:00.200 --> 00:26:05.075
of what we need to look at in
relation to the interval size?

00:26:07.791 --> 00:26:08.290
Yeah.

00:26:08.290 --> 00:26:08.790
Back there.

00:26:08.790 --> 00:26:10.340
AUDIENCE: We need
a cube root of 128.

00:26:10.340 --> 00:26:11.610
SRINI DEVADAS: Ah,
cube root, cube root.

00:26:11.610 --> 00:26:12.580
What's your name?

00:26:12.580 --> 00:26:12.840
AUDIENCE: Ryan.

00:26:12.840 --> 00:26:13.680
SRINI DEVADAS: Ryan.

00:26:13.680 --> 00:26:15.150
So Ryan says, you
know, cube root.

00:26:15.150 --> 00:26:17.260
And yeah, that's the
correct intuition.

00:26:17.260 --> 00:26:19.756
It's a little bit unclear,
you know, what happens.

00:26:19.756 --> 00:26:21.630
You know, so you do cube
roots, and maybe you

00:26:21.630 --> 00:26:24.560
have what's the
cube root of 128?

00:26:24.560 --> 00:26:27.030
5, yeah, that's right.

00:26:27.030 --> 00:26:27.940
I think it's 5.

00:26:27.940 --> 00:26:28.440
OK.

00:26:28.440 --> 00:26:29.670
It's close to 5, you know.

00:26:29.670 --> 00:26:31.950
It's 6 or 5 or 6, right?

00:26:31.950 --> 00:26:35.760
And so maybe I do
6, and then I do 12.

00:26:35.760 --> 00:26:39.190
And it's a little bit strange.

00:26:39.190 --> 00:26:42.270
So it turns out, it's
actually the 6 and 12,

00:26:42.270 --> 00:26:45.690
and you start with 128
when you have three balls,

00:26:45.690 --> 00:26:48.690
doesn't seem like it's going to
give you the worst case, right?

00:26:48.690 --> 00:26:50.890
Because, you know,
like in the worst case,

00:26:50.890 --> 00:26:54.100
you might be dropping
the ball 6, 12, 18,

00:26:54.100 --> 00:26:56.640
24, et cetera, et cetera.

00:26:56.640 --> 00:26:59.160
So cube root is right, but
it's something a little bit

00:26:59.160 --> 00:27:01.530
different, because it
gave us this interval that

00:27:01.530 --> 00:27:03.640
was kind of too small.

00:27:03.640 --> 00:27:05.820
So how would I get
something that's bigger?

00:27:05.820 --> 00:27:07.830
You'd want things that
are a little bit bigger.

00:27:07.830 --> 00:27:10.900
I mean, I think you're going
in the right direction,

00:27:10.900 --> 00:27:12.840
but I don't want
6 and 12 and 18.

00:27:12.840 --> 00:27:15.720
I mean, clearly, that's going
to give me like 25 drops

00:27:15.720 --> 00:27:19.456
and that's even worse than the
two ball case, which really

00:27:19.456 --> 00:27:21.830
means you should lose your
job and never get another one,

00:27:21.830 --> 00:27:22.330
right?

00:27:22.330 --> 00:27:25.680
So if you do something
that bad, right?

00:27:25.680 --> 00:27:29.700
So that's-- what would you do
that's kind of different from

00:27:29.700 --> 00:27:31.330
cube root, but
related to cube root?

00:27:34.020 --> 00:27:34.970
Yeah, go ahead Fadi.

00:27:34.970 --> 00:27:36.730
AUDIENCE: 128
raised to 2/3 maybe?

00:27:36.730 --> 00:27:37.046
SRINI DEVADAS: Yeah, yeah.

00:27:37.046 --> 00:27:37.546
Two thirds.

00:27:37.546 --> 00:27:38.150
Exactly right.

00:27:38.150 --> 00:27:40.370
So what you want to do
is, you want to start--

00:27:40.370 --> 00:27:42.880
it makes sense that when
you have lots of balls,

00:27:42.880 --> 00:27:44.640
in terms of being
able to use them,

00:27:44.640 --> 00:27:47.117
that you start with larger
intervals and you shrink them.

00:27:47.117 --> 00:27:48.950
That's exactly what
happened with two balls.

00:27:48.950 --> 00:27:52.410
You started with k in the
case where we had two balls.

00:27:52.410 --> 00:27:55.050
And then we are down to
one when we have one ball.

00:27:55.050 --> 00:27:56.707
So that's really
what you're doing.

00:27:56.707 --> 00:27:58.290
When you have one
ball, all you can do

00:27:58.290 --> 00:28:01.520
is go one floor at a time.

00:28:01.520 --> 00:28:03.580
When you have two
balls, you go k,

00:28:03.580 --> 00:28:05.504
where k happened to
be square root of n.

00:28:05.504 --> 00:28:07.170
And then we had three
balls, you kind of

00:28:07.170 --> 00:28:10.620
want to start with
like n raised to 2/3

00:28:10.620 --> 00:28:13.110
in terms of the interval.

00:28:13.110 --> 00:28:16.050
And then if I had four
balls, the argument

00:28:16.050 --> 00:28:18.870
would be that I would
start with n raised to 3/4.

00:28:18.870 --> 00:28:23.010
And then I would go
to square root of n.

00:28:23.010 --> 00:28:25.090
And then I would go
so on and so forth.

00:28:25.090 --> 00:28:26.850
And what is that
reminding you of?

00:28:26.850 --> 00:28:29.880
What is this, in
terms of these powers

00:28:29.880 --> 00:28:35.520
and getting up to not quite
n, but 5, 6th n, so on.

00:28:35.520 --> 00:28:38.960
What does that remind
you of from number theory

00:28:38.960 --> 00:28:43.460
or just sort of
regular arithmetic?

00:28:43.460 --> 00:28:44.269
AUDIENCE: Bases.

00:28:44.269 --> 00:28:45.810
SRINI DEVADAS: Yeah,
different bases.

00:28:45.810 --> 00:28:48.676
It's different radix
arithmetic, right?

00:28:48.676 --> 00:28:51.050
So it turns out, the way you
want to solve this problem--

00:28:51.050 --> 00:28:55.200
and this is in keeping
with the intuition you

00:28:55.200 --> 00:28:59.430
have with respect to the cube
roots, and so on, and so forth.

00:28:59.430 --> 00:29:01.890
The way we want to
do this is, we want

00:29:01.890 --> 00:29:05.175
to think about d digit numbers.

00:29:12.790 --> 00:29:17.040
And the numbers themselves,
and d is the number of balls.

00:29:20.020 --> 00:29:21.290
And so that's the d digits.

00:29:21.290 --> 00:29:24.890
But what is the radix
of these numbers?

00:29:24.890 --> 00:29:30.360
The radix of these numbers are
going to be related to the k,

00:29:30.360 --> 00:29:31.920
to the intervals.

00:29:31.920 --> 00:29:34.340
So you essentially
want to say, you know,

00:29:34.340 --> 00:29:36.470
I'm counting in multiples of k--

00:29:36.470 --> 00:29:38.990
1k, 2k, 3k, et cetera.

00:29:38.990 --> 00:29:41.150
So if I had like
radix k, that means

00:29:41.150 --> 00:29:44.720
I'm just incrementing
that particular digit.

00:29:44.720 --> 00:29:48.920
You know, if I go,
if I had radix 12,

00:29:48.920 --> 00:29:57.270
and I had 1 0 in radix
12, then this would be--

00:30:02.120 --> 00:30:07.310
so when I go 2 0, then
the most significant digit

00:30:07.310 --> 00:30:08.120
got incremented.

00:30:08.120 --> 00:30:11.540
And so this is plus 12.

00:30:11.540 --> 00:30:12.980
This is 12 times larger--

00:30:12.980 --> 00:30:17.510
I'm sorry-- 12 larger than
two 0's, 12 larger than 1 0

00:30:17.510 --> 00:30:19.300
in radix 12.

00:30:19.300 --> 00:30:20.240
Make sense?

00:30:20.240 --> 00:30:29.110
Just like 20 is 10 larger
than 10 in decimal.

00:30:29.110 --> 00:30:31.610
So that's the way you
want to think about this.

00:30:31.610 --> 00:30:37.790
And so you're going
to do r-ary numbers.

00:30:37.790 --> 00:30:39.290
And the first
thing that you need

00:30:39.290 --> 00:30:41.270
to do is, you need
to figure out,

00:30:41.270 --> 00:30:44.080
based on the number of floors
that you have, figure out,

00:30:44.080 --> 00:30:45.260
you've given d.

00:30:45.260 --> 00:30:47.799
You're given n, the
number of floors.

00:30:47.799 --> 00:30:49.340
And you're given
the number of balls.

00:30:49.340 --> 00:30:51.800
And you got to figure out
what radix you're working in.

00:30:51.800 --> 00:30:53.810
And this is going to
be a generalization

00:30:53.810 --> 00:30:57.820
of the square root
of n, as you'll see.

00:30:57.820 --> 00:31:02.930
We want to choose
a radix such that r

00:31:02.930 --> 00:31:08.840
raised to d is greater than n.

00:31:08.840 --> 00:31:16.800
Choose r such that r raised
to d is greater than n.

00:31:16.800 --> 00:31:19.080
And so that comes
back to, obviously,

00:31:19.080 --> 00:31:22.350
if you needed, if d were
2, then it makes sense

00:31:22.350 --> 00:31:24.150
that r would be
square root of n,

00:31:24.150 --> 00:31:26.490
which is in keeping with
what we talked about.

00:31:26.490 --> 00:31:30.027
But if d is 3, then that radix--

00:31:30.027 --> 00:31:31.860
not necessarily the
interval that we choose,

00:31:31.860 --> 00:31:34.777
because the intervals are
going to be different--

00:31:34.777 --> 00:31:36.610
is going to be more
like the cube root of n,

00:31:36.610 --> 00:31:41.080
which is also proposed.

00:31:41.080 --> 00:31:42.810
Ryan mentioned that.

00:31:42.810 --> 00:31:50.310
And so in this case, let's say
that n were 128 and d equals 4.

00:31:50.310 --> 00:31:56.430
Then we would choose r equals
4, because 4 raised to 4

00:31:56.430 --> 00:31:59.550
is 256, which is
greater than 128.

00:31:59.550 --> 00:32:05.610
And we can't choose 3, because
3 raised to 4 is only 81.

00:32:05.610 --> 00:32:08.940
And 81 isn't greater than 128.

00:32:08.940 --> 00:32:11.840
So you'd have to choose 4.

00:32:11.840 --> 00:32:16.600
So what we're now saying is,
we want to somehow figure out--

00:32:16.600 --> 00:32:19.300
we're not quite there
in an algorithm yet.

00:32:19.300 --> 00:32:22.830
But we have a kind
of a path forward

00:32:22.830 --> 00:32:28.470
in that we want to look
at that representation,

00:32:28.470 --> 00:32:33.750
that r-ary representation
of these d digit numbers.

00:32:33.750 --> 00:32:37.710
And we're going to
essentially increment.

00:32:37.710 --> 00:32:41.400
And so with our goal,
roughly speaking,

00:32:41.400 --> 00:32:45.030
we're going to go with
this representation.

00:32:45.030 --> 00:32:48.360
And we're going to start
incrementing digits.

00:32:48.360 --> 00:32:51.430
So we just go 0, 1, 2
in terms of the digits.

00:32:51.430 --> 00:32:53.995
But because the
representation is r-ary

00:32:53.995 --> 00:32:55.620
that means something
different in terms

00:32:55.620 --> 00:32:58.230
of the number of floors,
depending on what r is.

00:32:58.230 --> 00:33:01.410
I mean, just like
I said, if we're

00:33:01.410 --> 00:33:06.512
this were ternary representation
then in terms of floors,

00:33:06.512 --> 00:33:07.095
this is only--

00:33:09.841 --> 00:33:13.620
this would be, for ternary,
this would be three floors.

00:33:13.620 --> 00:33:19.230
2 0 would be three
floors away from 1 0.

00:33:19.230 --> 00:33:22.040
In decimal, 2 0 is 10
floors away from 1 0,

00:33:22.040 --> 00:33:23.040
and so on, and so forth.

00:33:23.040 --> 00:33:24.251
That makes sense?

00:33:24.251 --> 00:33:24.750
Yep.

00:33:24.750 --> 00:33:28.689
Ask me questions
if you're confused.

00:33:28.689 --> 00:33:29.730
So let's take an example.

00:33:29.730 --> 00:33:34.120
So and then we'll look at
code that actually builds

00:33:34.120 --> 00:33:38.520
this algorithm from scratch.

00:33:38.520 --> 00:33:40.350
So we'll take our
example of d equals

00:33:40.350 --> 00:33:44.460
4 for our canonical 128
floor Shanghai Tower,

00:33:44.460 --> 00:33:48.240
because you do want to get a
sense of how things improve.

00:33:48.240 --> 00:33:50.760
We did improve from
obviously d equals 1

00:33:50.760 --> 00:33:55.320
through d equals 2 with the
equal interval assumption.

00:33:55.320 --> 00:33:59.550
But with this radix
idea, we'd like

00:33:59.550 --> 00:34:03.060
to see how much better we
can do with d equals 4.

00:34:03.060 --> 00:34:06.780
And we want to do substantially
better than 21 drops

00:34:06.780 --> 00:34:10.429
or whatever we had back there.

00:34:10.429 --> 00:34:12.870
So r is 4.

00:34:12.870 --> 00:34:16.230
d is 4-- oh, I'm sorry-- d is 4.

00:34:16.230 --> 00:34:18.570
n equals 128.

00:34:18.570 --> 00:34:24.338
And what we're going to do is,
the smallest number is 0000.

00:34:24.338 --> 00:34:30.460
And in brackets, I'm going write
what the decimal representation

00:34:30.460 --> 00:34:33.719
is, because I think that
does give you some intuition.

00:34:33.719 --> 00:34:37.104
You get a sense of how this
translates into floors.

00:34:37.104 --> 00:34:38.520
And after a while,
if you're going

00:34:38.520 --> 00:34:40.770
to tell someone to do
this, you want to give them

00:34:40.770 --> 00:34:42.150
floor numbers in decimal.

00:34:42.150 --> 00:34:43.710
Because the Shanghai
Tower doesn't

00:34:43.710 --> 00:34:48.179
have some weird
representation for the floors

00:34:48.179 --> 00:34:51.150
when you get into an elevator
or anything like that.

00:34:51.150 --> 00:34:53.070
So this is floor 0.

00:34:53.070 --> 00:35:00.000
And now, the highest number is
3333, which happens to be 255.

00:35:00.000 --> 00:35:02.250
I'm not going to
write floor each time.

00:35:02.250 --> 00:35:04.230
But when I put things
in brackets here,

00:35:04.230 --> 00:35:05.801
that's the decimal
representation.

00:35:05.801 --> 00:35:06.300
OK.

00:35:06.300 --> 00:35:07.230
So that's 255.

00:35:07.230 --> 00:35:08.970
So that's obviously too big.

00:35:08.970 --> 00:35:09.940
We don't need that.

00:35:09.940 --> 00:35:12.570
But we're stuck with
having to choose

00:35:12.570 --> 00:35:16.220
r equals 4 for the reasons
I described to you before.

00:35:16.220 --> 00:35:19.500
So there's somewhere
in here there's a 128.

00:35:19.500 --> 00:35:24.150
And I don't have it
right up in front of me

00:35:24.150 --> 00:35:28.950
as to what 128
would correspond to.

00:35:28.950 --> 00:35:36.920
But 1233, for example,
would be floor 111.

00:35:36.920 --> 00:35:38.380
So the way this
works, and you all

00:35:38.380 --> 00:35:43.060
know you're radix
arithmetic, it's

00:35:43.060 --> 00:35:51.310
3 times 1, plus 3 times 4, plus
2 times 4 square, plus 1 times

00:35:51.310 --> 00:35:52.960
4 cubed.

00:35:52.960 --> 00:35:53.950
That's what you have.

00:35:53.950 --> 00:35:55.630
And that's how you get 111.

00:35:55.630 --> 00:35:59.350
So what you have to do
in order to do this right

00:35:59.350 --> 00:36:04.390
is to start with the most
significant digit, which

00:36:04.390 --> 00:36:05.644
is the left-most digit.

00:36:05.644 --> 00:36:07.810
And that makes sense, because
we want our intervals,

00:36:07.810 --> 00:36:09.910
when we have lots of
balls, to be large.

00:36:09.910 --> 00:36:13.250
And that as you move
and the balls break,

00:36:13.250 --> 00:36:17.410
you're going to start
shifting rightward.

00:36:17.410 --> 00:36:19.960
And you're going to be
incrementing different things.

00:36:19.960 --> 00:36:25.169
And that's essentially going to
give us give us our algorithm.

00:36:25.169 --> 00:36:26.710
And so we'll talk
about-- let me just

00:36:26.710 --> 00:36:29.110
give you an example
of an algorithm.

00:36:29.110 --> 00:36:34.360
And we'll look at how the
algorithm is implemented.

00:36:34.360 --> 00:36:37.120
As I mentioned, it turns
into 10 lines of code,

00:36:37.120 --> 00:36:40.180
because all you're
doing is translating

00:36:40.180 --> 00:36:42.400
into this representation.

00:36:42.400 --> 00:36:44.920
And you're just incrementing
bits of the representation.

00:36:44.920 --> 00:36:46.120
And it's interactive.

00:36:46.120 --> 00:36:50.550
You can play with it
later today or whenever.

00:36:50.550 --> 00:36:52.720
And you can sort of think
of a hardness coefficient

00:36:52.720 --> 00:36:54.970
in your head then see
how things work out.

00:36:54.970 --> 00:36:56.905
But before we end,
we obviously will

00:36:56.905 --> 00:37:00.670
have to do a worst-case
analysis of this algorithm

00:37:00.670 --> 00:37:04.930
because that's the whole
point of this exercise.

00:37:04.930 --> 00:37:08.290
So in this case, where
do you think I should--

00:37:08.290 --> 00:37:11.560
so I don't want to drop,
obviously, at floor 0.

00:37:11.560 --> 00:37:13.480
I mean, that's there's
no notion of that.

00:37:13.480 --> 00:37:16.500
So where do I drop
at, at the beginning?

00:37:16.500 --> 00:37:21.020
The first drop is at,
given what I told you?

00:37:21.020 --> 00:37:23.580
You can tell me in
any representation.

00:37:23.580 --> 00:37:25.220
Where would I drop
the first ball

00:37:25.220 --> 00:37:27.350
if I have four balls
and 128 floors?

00:37:32.480 --> 00:37:36.590
I said that I want to start
with something that, obviously,

00:37:36.590 --> 00:37:38.450
there's some larger
interval here.

00:37:38.450 --> 00:37:41.090
And I want to start
on the left-hand side.

00:37:41.090 --> 00:37:43.520
And I want to increment
the leftmost bit, which

00:37:43.520 --> 00:37:45.260
is the most significant bit.

00:37:45.260 --> 00:37:50.090
So I would start with 1000.

00:37:50.090 --> 00:38:00.560
So the first drop is at
1000, which is at floor 64.

00:38:00.560 --> 00:38:01.970
That's the first drop.

00:38:01.970 --> 00:38:09.374
Now, if in fact this
does not break--

00:38:09.374 --> 00:38:11.540
all right, so let's assume
that this does not break.

00:38:14.970 --> 00:38:18.090
Then what do I do next?

00:38:18.090 --> 00:38:20.470
2000, right?

00:38:20.470 --> 00:38:25.030
And this would be 128.

00:38:25.030 --> 00:38:26.770
Let's assume that
this breaks here.

00:38:29.400 --> 00:38:30.240
All right.

00:38:30.240 --> 00:38:32.370
So now, what do I do?

00:38:32.370 --> 00:38:33.000
It broke.

00:38:33.000 --> 00:38:35.190
I lost a ball.

00:38:35.190 --> 00:38:37.045
Now I have three
balls left still.

00:38:37.045 --> 00:38:37.920
That's the good news.

00:38:37.920 --> 00:38:40.584
And how many-- so what do I
need, where do I need to move?

00:38:40.584 --> 00:38:42.000
If I have three
balls left, I need

00:38:42.000 --> 00:38:45.220
to be in a representation
that has how many digits?

00:38:45.220 --> 00:38:46.420
Three digits.

00:38:46.420 --> 00:38:47.500
So I need to move.

00:38:47.500 --> 00:38:51.550
I a broke at 2000, so I know
that it's less than 2000.

00:38:51.550 --> 00:38:54.820
So I need to go back to this,
because this is the highest

00:38:54.820 --> 00:38:55.810
floor.

00:38:55.810 --> 00:38:58.710
In general, whenever
things break,

00:38:58.710 --> 00:39:02.110
you go back to the highest
floor that you know

00:39:02.110 --> 00:39:03.940
that the ball didn't break.

00:39:03.940 --> 00:39:05.740
I mean, that's the
canonical strategy

00:39:05.740 --> 00:39:07.240
that we've followed all through.

00:39:07.240 --> 00:39:09.970
When the ball
breaks, you go back

00:39:09.970 --> 00:39:11.560
and say, oh, but I
do know that there

00:39:11.560 --> 00:39:15.130
was a highest floor in which
the ball did not break.

00:39:15.130 --> 00:39:19.770
And I have to start
with 1 plus that, right?

00:39:19.770 --> 00:39:22.820
So what I'm going to do
here is I need to go up

00:39:22.820 --> 00:39:32.530
and because it broke,
I need to look at 1001.

00:39:32.530 --> 00:39:34.900
The interval that
I need to look at

00:39:34.900 --> 00:39:38.920
is 1001, because it's
one more than that.

00:39:38.920 --> 00:39:44.410
And all the way to 1333,
which is one less than that.

00:39:47.010 --> 00:39:51.750
So and this, you may
feel a little bit better

00:39:51.750 --> 00:39:54.510
looking at this in decimal.

00:39:54.510 --> 00:39:55.210
I certainly do.

00:39:57.880 --> 00:40:01.560
It's 65, 127, which
makes perfect sense.

00:40:01.560 --> 00:40:06.210
So far, we've played around with
the balls, the first two balls.

00:40:06.210 --> 00:40:09.030
And so we haven't really seen
anything very dramatically

00:40:09.030 --> 00:40:13.800
different from the two-ball
case, but more is coming.

00:40:13.800 --> 00:40:15.690
Let's say this does not break.

00:40:15.690 --> 00:40:24.240
If this does not break,
then what would I do now?

00:40:24.240 --> 00:40:25.830
If it does not break--

00:40:25.830 --> 00:40:31.590
I'm sorry, my bad, my bad.

00:40:31.590 --> 00:40:33.990
I haven't even told you.

00:40:33.990 --> 00:40:35.820
I've just talked
about the interval.

00:40:35.820 --> 00:40:38.570
And I haven't said what drop
you need to make, right?

00:40:38.570 --> 00:40:41.040
So ignore what I said just now.

00:40:41.040 --> 00:40:44.190
I think that the next
question is, the ball broke.

00:40:44.190 --> 00:40:45.360
So now I have a second ball.

00:40:48.010 --> 00:40:50.350
And I want the first
drop of the second ball.

00:40:53.260 --> 00:40:55.227
And I want to look at this.

00:40:55.227 --> 00:40:56.560
I want to look at this interval.

00:40:56.560 --> 00:40:58.820
And it has to be somewhere
inside this interval.

00:40:58.820 --> 00:40:59.903
But it's not going to be--

00:40:59.903 --> 00:41:06.880
I'm not going to drop this
at this floor, 65 or 1333.

00:41:06.880 --> 00:41:09.250
I want to pick something
in between, right?

00:41:09.250 --> 00:41:13.390
And so now looking
at this, tell me--

00:41:13.390 --> 00:41:15.630
according to what
you know so far--

00:41:15.630 --> 00:41:18.400
where you think I should
drop the second ball

00:41:18.400 --> 00:41:19.914
for the first time?

00:41:19.914 --> 00:41:20.798
Go ahead.

00:41:20.798 --> 00:41:21.950
AUDIENCE: 96.

00:41:21.950 --> 00:41:22.700
SRINI DEVADAS: 96.

00:41:22.700 --> 00:41:23.995
And how did you get that?

00:41:23.995 --> 00:41:26.690
AUDIENCE: Just like
I did the half point.

00:41:26.690 --> 00:41:28.135
SRINI DEVADAS: And you put 1100?

00:41:28.135 --> 00:41:28.760
AUDIENCE: Yeah.

00:41:28.760 --> 00:41:29.460
SRINI DEVADAS: Perfect.

00:41:29.460 --> 00:41:30.196
What's your name?

00:41:30.196 --> 00:41:31.090
AUDIENCE: Evan.

00:41:31.090 --> 00:41:31.923
SRINI DEVADAS: Evan.

00:41:31.923 --> 00:41:33.560
So Evan says 96.

00:41:33.560 --> 00:41:37.880
I guess he thinks in decimal
naturally, like most of us.

00:41:37.880 --> 00:41:41.540
But the way you would
code this, by the way,

00:41:41.540 --> 00:41:45.830
is by incrementing this digit.

00:41:45.830 --> 00:41:49.040
And you'll get 1100.

00:41:49.040 --> 00:41:51.920
And in this case, let's
say it does not break.

00:41:55.110 --> 00:41:58.770
Then where do you go, Evan?

00:41:58.770 --> 00:41:59.977
AUDIENCE: Above floor 96.

00:41:59.977 --> 00:42:00.810
SRINI DEVADAS: Yeah.

00:42:00.810 --> 00:42:02.150
After 96, it doesn't break.

00:42:02.150 --> 00:42:03.732
What would you do?

00:42:03.732 --> 00:42:06.930
AUDIENCE: Then you would
go to what is that?

00:42:06.930 --> 00:42:08.590
SRINI DEVADAS: Well, it's-

00:42:08.590 --> 00:42:10.050
AUDIENCE: In decimal?

00:42:10.050 --> 00:42:12.400
SRINI DEVADAS: No, in
this representation--

00:42:14.806 --> 00:42:15.930
whatever is easier for you.

00:42:15.930 --> 00:42:17.680
AUDIENCE: It would be 1200.

00:42:17.680 --> 00:42:19.260
SRINI DEVADAS:
Perfect, 1200, which

00:42:19.260 --> 00:42:22.080
happens to be I think 112.

00:42:22.080 --> 00:42:23.490
So you go to floor 112.

00:42:23.490 --> 00:42:29.216
This was 96, and then you needed
to add another 16 to that--

00:42:29.216 --> 00:42:33.030
yep, 112 and so on.

00:42:33.030 --> 00:42:35.060
So I won't bore you with this.

00:42:35.060 --> 00:42:37.600
You get a sense of
what's going on here.

00:42:37.600 --> 00:42:41.790
And obviously with these
things, you can wave your hands

00:42:41.790 --> 00:42:44.100
and you get a sense
of what's going on.

00:42:44.100 --> 00:42:48.780
But ultimately, you know, what's
great about computer science

00:42:48.780 --> 00:42:53.040
and programming is you've got
to teach a dumb computer to do

00:42:53.040 --> 00:42:54.720
exactly the right thing.

00:42:54.720 --> 00:42:57.270
And there's no two things
about it in terms of you

00:42:57.270 --> 00:42:58.810
understand this algorithm.

00:42:58.810 --> 00:43:00.270
I didn't understand
this algorithm

00:43:00.270 --> 00:43:02.630
until I coded it, fully.

00:43:02.630 --> 00:43:04.380
I mean, maybe I still
don't understand it,

00:43:04.380 --> 00:43:07.950
considering the
mistakes I'm making.

00:43:07.950 --> 00:43:12.960
But that's just because I've
lost a bunch of gray cells.

00:43:12.960 --> 00:43:15.300
So let me show you what
the code looks like.

00:43:15.300 --> 00:43:18.270
It's 15 lines of code.

00:43:18.270 --> 00:43:19.290
It's not that much.

00:43:19.290 --> 00:43:23.850
And as you can imagine,
the representation is--

00:43:23.850 --> 00:43:25.100
I could have used many things.

00:43:25.100 --> 00:43:27.910
I just used a list for
each of these digits.

00:43:27.910 --> 00:43:30.360
I'm incrementing these digits.

00:43:30.360 --> 00:43:34.630
And I won't spend a whole
lot of time on the code,

00:43:34.630 --> 00:43:39.780
but what I want to do before
we end here is talk about--

00:43:39.780 --> 00:43:43.230
actually, let me do that first,
before we go to the code.

00:43:43.230 --> 00:43:45.930
I want to talk about
the worst case here,

00:43:45.930 --> 00:43:48.000
because that's what
this is all about.

00:43:48.000 --> 00:43:51.440
I want to get an equation,
just like we did for 2 times

00:43:51.440 --> 00:43:54.540
square root of n minus 1 and
all of these different things, I

00:43:54.540 --> 00:43:57.420
want to get an equation
for the worst case in terms

00:43:57.420 --> 00:44:01.920
of the number of drops
in relation to r and d,

00:44:01.920 --> 00:44:03.720
which are obviously
related to n.

00:44:03.720 --> 00:44:06.567
Because r raised to d
has to be greater than n.

00:44:06.567 --> 00:44:08.400
So if you kind of look
at this, and it turns

00:44:08.400 --> 00:44:11.580
out this is not a
difficult analysis--

00:44:11.580 --> 00:44:13.134
if you think about
it, and you start

00:44:13.134 --> 00:44:14.550
thinking about
worst case, and you

00:44:14.550 --> 00:44:17.940
think about how we're
incrementing these things,

00:44:17.940 --> 00:44:19.170
there's phases.

00:44:19.170 --> 00:44:23.267
And if you can think about a
phase as when a ball breaks,

00:44:23.267 --> 00:44:24.850
you know, that's
when a phase is done.

00:44:24.850 --> 00:44:28.050
And you move to
the second phase.

00:44:28.050 --> 00:44:32.450
Tell me how many drops I have?

00:44:32.450 --> 00:44:34.894
You break it up in terms
of the number of phases

00:44:34.894 --> 00:44:36.810
or the number of balls,
whichever way you want

00:44:36.810 --> 00:44:39.360
to think about it,
and use the same kind

00:44:39.360 --> 00:44:41.700
of worst-case analysis
we've done with respect

00:44:41.700 --> 00:44:48.520
to what happens
in the worst case.

00:44:48.520 --> 00:44:50.480
You have to keep doing
a bunch of drops.

00:44:50.480 --> 00:44:54.720
And really, for these
algorithms, the floor 127,

00:44:54.720 --> 00:44:56.910
the hardness coefficient of--

00:44:56.910 --> 00:45:01.340
actually, the hardness
coefficient of 127

00:45:01.340 --> 00:45:02.490
is the hard one, right?

00:45:02.490 --> 00:45:04.740
It's the difficult one,
because you kind of go through

00:45:04.740 --> 00:45:06.360
and you go all the way there.

00:45:06.360 --> 00:45:08.220
And you do all of these
things before-- you

00:45:08.220 --> 00:45:11.700
drop the ball a lot of
times before it breaks.

00:45:11.700 --> 00:45:13.650
And you do that for every ball.

00:45:13.650 --> 00:45:15.540
That would be the
worst case, right?

00:45:15.540 --> 00:45:17.520
So that's kind of
where things are at.

00:45:17.520 --> 00:45:20.760
So from a symbolic
standpoint, what

00:45:20.760 --> 00:45:23.760
is the worst case
in the first phase

00:45:23.760 --> 00:45:27.870
in terms of the number of
drops of the first ball,

00:45:27.870 --> 00:45:31.546
from a symbolic standpoint?

00:45:31.546 --> 00:45:33.170
It's the first digit
I'm talking about.

00:45:33.170 --> 00:45:35.110
It's the first ball
I'm talking about.

00:45:35.110 --> 00:45:39.180
And I'm dropping this
ball, as you can see,

00:45:39.180 --> 00:45:40.560
at these different points.

00:45:40.560 --> 00:45:46.800
So in general, how many drops
would I have before I'm either,

00:45:46.800 --> 00:45:50.320
I may be done completely because
I know it doesn't break a floor

00:45:50.320 --> 00:45:52.220
128 or what have you.

00:45:52.220 --> 00:45:55.480
But what is the worst case
when that ball breaks?

00:45:55.480 --> 00:45:57.554
Give me an equation.

00:45:57.554 --> 00:45:58.054
Someone.

00:46:01.350 --> 00:46:02.220
So I drop it once.

00:46:02.220 --> 00:46:03.430
I drop it twice.

00:46:03.430 --> 00:46:06.200
Am I going to drop
it like, if r is 5,

00:46:06.200 --> 00:46:09.436
am I going to drop it 10 times?

00:46:09.436 --> 00:46:10.810
What am I doing
in terms of the--

00:46:10.810 --> 00:46:13.985
what is the upper limit
in terms of this digit?

00:46:13.985 --> 00:46:14.859
AUDIENCE: r minus 1.

00:46:14.859 --> 00:46:15.900
SRINI DEVADAS: r minus 1.

00:46:15.900 --> 00:46:18.150
And where am I starting from?

00:46:18.150 --> 00:46:19.500
1, so there you go.

00:46:19.500 --> 00:46:20.530
What's the answer?

00:46:20.530 --> 00:46:21.377
AUDIENCE: r minus 1.

00:46:21.377 --> 00:46:22.710
SRINI DEVADAS: r minus 1, right?

00:46:22.710 --> 00:46:23.740
So that's it.

00:46:23.740 --> 00:46:30.870
So it turns out that I have
r minus 1 drops in the worst

00:46:30.870 --> 00:46:38.020
case for the first ball.

00:46:38.020 --> 00:46:41.520
And, I mean, it's the
same representation.

00:46:41.520 --> 00:46:44.920
Every digit is the
same radix, obviously.

00:46:44.920 --> 00:46:48.220
And now, that ball broke and
I moved to the second digit

00:46:48.220 --> 00:46:49.660
with the second ball.

00:46:49.660 --> 00:46:51.850
And I'm going to start again.

00:46:51.850 --> 00:46:56.170
And in the worst case, I could
have r minus 1 drops for that

00:46:56.170 --> 00:46:59.519
as well, right, because I'm
going to go increment from 0.

00:46:59.519 --> 00:47:01.060
The first one I'm
going to do is a 1.

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.

00:47:04.370 --> 00:47:10.790
So r minus 1 drops
for the second ball.

00:47:10.790 --> 00:47:14.550
And then I go all
the way to d balls.

00:47:14.550 --> 00:47:21.726
So my answer-- yeah, go ahead.

00:47:21.726 --> 00:47:22.400
Over here.

00:47:22.400 --> 00:47:25.686
Ganatra.

00:47:25.686 --> 00:47:27.630
AUDIENCE: So r is
4 in this case.

00:47:27.630 --> 00:47:29.880
And we say the worse case is 3.

00:47:29.880 --> 00:47:31.543
But how do we get 3?

00:47:31.543 --> 00:47:34.210
Because if it doesn't
break on either of those,

00:47:34.210 --> 00:47:38.631
on the 1000 and 2000,
aren't we just done

00:47:38.631 --> 00:47:40.287
and that's only two drops?

00:47:40.287 --> 00:47:41.120
SRINI DEVADAS: Yeah.

00:47:41.120 --> 00:47:43.540
So it gets a little--

00:47:43.540 --> 00:47:44.650
you're absolutely right.

00:47:44.650 --> 00:47:47.671
But if the number were 255--

00:47:47.671 --> 00:47:49.420
so one of the things
that's happening here

00:47:49.420 --> 00:47:53.440
is we've lost some precision
because r raised to d needed

00:47:53.440 --> 00:47:55.780
to be this large number of 255.

00:47:55.780 --> 00:47:58.895
And this algorithm is actually
going to work for 255 floors.

00:47:58.895 --> 00:48:00.320
AUDIENCE: Oh, it's because of n.

00:48:00.320 --> 00:48:01.800
SRINI DEVADAS:
Yeah, because of n.

00:48:01.800 --> 00:48:04.719
So it's not, yeah, so
that's kind of-- you know,

00:48:04.719 --> 00:48:06.010
it's a good question you asked.

00:48:06.010 --> 00:48:09.670
But unfortunately,
you kind of want

00:48:09.670 --> 00:48:14.110
to choose a case where you
have perfect precision in terms

00:48:14.110 --> 00:48:17.090
of r raised to d is exactly
what you need it to be.

00:48:17.090 --> 00:48:18.700
And in that case,
this analysis works.

00:48:18.700 --> 00:48:20.117
AUDIENCE: So it
works the max end.

00:48:20.117 --> 00:48:21.699
SRINI DEVADAS: That's
it, the max end.

00:48:21.699 --> 00:48:22.760
That's exactly right.

00:48:22.760 --> 00:48:25.110
So I mean, 255 is
substantially larger than 128.

00:48:25.110 --> 00:48:27.160
So it kind of makes
sense that, as you said,

00:48:27.160 --> 00:48:30.014
that you would be able to
shave off certain things.

00:48:30.014 --> 00:48:31.680
And so you can kind
of do that, but it's

00:48:31.680 --> 00:48:33.010
a little more involved.

00:48:33.010 --> 00:48:36.730
But I think the
intuition is important.

00:48:36.730 --> 00:48:40.060
And the intuition is simply
that your worst-case number

00:48:40.060 --> 00:48:46.240
of drops, assuming you're
being a little bit imprecise,

00:48:46.240 --> 00:48:52.000
would simply be r minus 1
times d in the worst case.

00:48:52.000 --> 00:48:57.760
And this would be true for
all n which is less than r

00:48:57.760 --> 00:49:01.370
raised to d, for all n.

00:49:01.370 --> 00:49:04.390
So now, just in
terms of numbers,

00:49:04.390 --> 00:49:07.990
I had r equals 4 and d equals 4.

00:49:07.990 --> 00:49:12.730
And so you end up getting 12
drops for the 128 case, all

00:49:12.730 --> 00:49:15.710
the way to 255 as well.

00:49:15.710 --> 00:49:18.040
You need 12 drops
in the worst case,

00:49:18.040 --> 00:49:23.260
even for 255 floors, which is
really what you were asking.

00:49:23.260 --> 00:49:23.990
So good.

00:49:23.990 --> 00:49:24.490
Right.

00:49:24.490 --> 00:49:26.330
That makes sense?

00:49:26.330 --> 00:49:30.100
So I'm not going to spend a
whole lot of time on this code,

00:49:30.100 --> 00:49:31.630
but that's it.

00:49:31.630 --> 00:49:33.940
It's converting.

00:49:33.940 --> 00:49:37.540
I could've used a Python
library function for that,

00:49:37.540 --> 00:49:39.866
but that's simply a
conversion because I

00:49:39.866 --> 00:49:40.990
wanted to print things out.

00:49:40.990 --> 00:49:45.472
I don't even need that,
this particular subroutine,

00:49:45.472 --> 00:49:46.930
which is essentially
something that

00:49:46.930 --> 00:49:52.870
is doing conversion from
decimal to r-ary arithmetic.

00:49:52.870 --> 00:49:57.130
And as you can see, you
see I subtract and adding.

00:49:57.130 --> 00:49:59.770
The adding this is the
incrementation that I just

00:49:59.770 --> 00:50:03.490
erased, but you do see it
up there from 1 0 to 2 0.

00:50:03.490 --> 00:50:04.960
That's why you add.

00:50:04.960 --> 00:50:08.350
Sometimes you need to
subtract, because things break.

00:50:08.350 --> 00:50:12.220
And when things break, you need
to go back to the highest floor

00:50:12.220 --> 00:50:14.140
where the ball didn't break.

00:50:14.140 --> 00:50:17.350
So that's where you
do the subtraction.

00:50:17.350 --> 00:50:22.780
And so this is how,
if you ran this,

00:50:22.780 --> 00:50:26.200
it's not the greatest user
interface, I'll admit.

00:50:26.200 --> 00:50:27.580
But that's not the point.

00:50:33.560 --> 00:50:34.420
The Crystal.

00:50:42.110 --> 00:50:47.950
So I'm going to do at 128
and 4, which is our example.

00:50:47.950 --> 00:50:50.250
And it says radix
shows it as ball 4.

00:50:50.250 --> 00:50:52.090
Did the ball break?

00:50:52.090 --> 00:50:53.740
I say no.

00:50:53.740 --> 00:50:56.410
And then drop the ball.

00:50:56.410 --> 00:50:59.390
Did the ball Break if I
say no here, then I'm done.

00:50:59.390 --> 00:51:01.480
So I'm going to go
ahead and say yes.

00:51:01.480 --> 00:51:06.370
And then I'm going to
say no and then no.

00:51:06.370 --> 00:51:07.720
I'm going to keep going--

00:51:07.720 --> 00:51:13.030
no, no, no, no-- because
this is the worst case.

00:51:20.770 --> 00:51:23.110
And I go to 11 in this case.

00:51:23.110 --> 00:51:26.891
So you do get close in the
sense that Ganatra was asking,

00:51:26.891 --> 00:51:28.140
you know, could you shave off?

00:51:28.140 --> 00:51:31.090
You only shaved off one
because of the imprecision.

00:51:31.090 --> 00:51:34.510
So there is a case where
even though you would only

00:51:34.510 --> 00:51:38.460
get 12 for 255, but
you do get 11 for 128.

00:51:38.460 --> 00:51:39.680
OK.

00:51:39.680 --> 00:51:40.180
Cool.

00:51:40.180 --> 00:51:40.680
Good.

00:51:40.680 --> 00:51:43.000
So I'll let you
look at the code.

00:51:43.000 --> 00:51:47.894
If you guys can leave
any time you want.

00:51:47.894 --> 00:51:49.310
I just wanted to
mention, I wanted

00:51:49.310 --> 00:51:50.726
to go back to the
one thing that I

00:51:50.726 --> 00:51:53.860
didn't get that I mentioned,
but I haven't gotten to do,

00:51:53.860 --> 00:51:55.930
which is this is still--

00:51:55.930 --> 00:51:57.750
you could still do better.

00:51:57.750 --> 00:52:00.760
You can still do better
not because of what Ganatra

00:52:00.760 --> 00:52:03.480
mentioned-- you know, that's
just that imprecision thing,

00:52:03.480 --> 00:52:05.860
but assuming even
that was taken care

00:52:05.860 --> 00:52:07.930
of because the
numbers worked out,

00:52:07.930 --> 00:52:11.200
You can do better because
k can be made a different.

00:52:11.200 --> 00:52:12.970
You could do unequal intervals.

00:52:12.970 --> 00:52:15.820
So let me put that
in the notes and you

00:52:15.820 --> 00:52:16.930
can read it by yourself.

00:52:16.930 --> 00:52:18.320
You had a question, Evan.

00:52:18.320 --> 00:52:19.170
AUDIENCE: Yeah.

00:52:19.170 --> 00:52:20.920
When you were going
through the ball drops

00:52:20.920 --> 00:52:23.770
and you kept answering no,
that the ball did not break,

00:52:23.770 --> 00:52:27.550
why did it start asking you or
why did it start telling you

00:52:27.550 --> 00:52:29.400
where the next ball dropped?

00:52:29.400 --> 00:52:31.550
Because it went
one, two, and then

00:52:31.550 --> 00:52:33.810
it went also to ball
three and ball four.

00:52:33.810 --> 00:52:36.056
So will it just stay ball
two the whole time it

00:52:36.056 --> 00:52:36.680
if never broke?

00:52:36.680 --> 00:52:38.180
SRINI DEVADAS:
That's exactly right,

00:52:38.180 --> 00:52:40.150
but that's because I'm lazy.

00:52:42.740 --> 00:52:43.630
Yeah.

00:52:43.630 --> 00:52:45.580
So it'd be a fine
exercise to fix that.

00:52:45.580 --> 00:52:47.437
I mean, that's not
one of the exercises.

00:52:47.437 --> 00:52:49.770
That's how you turn, you know
after 30 years of teaching

00:52:49.770 --> 00:52:51.269
whenever I get a
question, I turn it

00:52:51.269 --> 00:52:52.940
into an opportunity
for homework.

00:52:52.940 --> 00:52:53.631
OK.

00:52:53.631 --> 00:52:54.130
Right.

00:52:54.130 --> 00:52:54.820
That's how it works.

00:52:54.820 --> 00:52:55.060
All right.

00:52:55.060 --> 00:52:55.560
Good.

00:52:55.560 --> 00:52:57.360
See you guys next time.