The Chasm | Think Like A Coder, Ep 6

446,987 views ・ 2020-01-30

TED-Ed


Please double-click on the English subtitles below to play the video.

00:21
Ethic, Hedge, and Octavia stand on the edge of a bottomless ravine.
0
21937
4675
00:26
It’s the only thing between them and the tower
1
26612
2690
00:29
that houses the second of three powerful artifacts.
2
29302
3648
00:32
They’ve got a brief window of time to get across before the guards return.
3
32950
4980
00:37
With Hedge’s fuel gauge on empty he won’t be able to fly Ethic across,
4
37930
4705
00:42
so the only option is to make a bridge.
5
42635
3630
00:46
Fortunately, the floating stacks of stones nearby are bridge components—
6
46265
4655
00:50
invented by Octavia herself— called hover-blocks.
7
50920
4026
00:54
Activate a pile with a burst of energy,
8
54946
2552
00:57
and they’ll self-assemble to span the ravine as Ethic walks across.
9
57498
4516
01:02
But there is, of course, a catch.
10
62014
3578
01:05
The hover-blocks are only stable when they’re perfectly palindromic.
11
65592
4659
01:10
Meaning they have to form a sequence
12
70251
2260
01:12
that’s the same when viewed forwards and backwards.
13
72511
4263
01:16
The stacks start in random orders,
14
76774
2170
01:18
but will always put themselves into a palindromic configuration
15
78944
3660
01:22
if they can.
16
82604
1290
01:23
If they get to a point where a palindrome isn’t possible,
17
83894
2880
01:26
the bridge will collapse,
18
86774
1551
01:28
and whoever’s on it will fall into the ravine.
19
88325
3489
01:31
Let’s look at an example.
20
91814
1638
01:33
This stack would make itself stable.
21
93452
2460
01:35
First the A blocks hold themselves in place.
22
95912
3000
01:38
Then the B’s.
23
98912
1070
01:39
And finally the C would nestle right between the B’s.
24
99982
3690
01:43
However, suppose there was one more A.
25
103672
3450
01:47
First two A blocks form up, then two B’s,
26
107122
3120
01:50
but now the remaining C and A have nowhere to go,
27
110242
3370
01:53
so the whole thing falls apart.
28
113612
2460
01:56
The Node of Power enables Hedge to energize a single stack of blocks.
29
116072
4670
02:00
What instructions can Ethic give Hedge to allow him to efficiently find
30
120742
4334
02:05
and power a stable palindromic stack?
31
125076
3051
02:08
Pause now to figure it out for yourself.
32
128127
9970
02:18
Examples of palindromes include ANNA, RACECAR, and MADAM IM ADAM.
33
138097
5461
02:23
Counting the number of times a given letter appears in a palindrome
34
143558
3730
02:27
will reveal a helpful pattern.
35
147288
2532
02:29
Pause now to figure it out for yourself.
36
149820
4831
02:34
Let’s first look at a naïve solution to this problem.
37
154651
3490
02:38
A naïve solution is a simple, brute-force approach that isn’t optimized—
38
158141
4708
02:42
but will get the job done.
39
162849
1980
02:44
Naïve solutions are helpful ways to analyze problems,
40
164829
3491
02:48
and work as stepping stones to better solutions.
41
168320
3434
02:51
In this case, a naïve solution is to approach a pile of blocks,
42
171754
3770
02:55
try all the arrangements,
43
175524
1500
02:57
and see if one is a palindrome by reading it forward and then backwards.
44
177024
4727
03:01
The problem with this approach
45
181751
1480
03:03
is that it would take a tremendous amount of time.
46
183231
2493
03:05
If Hedge tried one combination every second,
47
185724
2850
03:08
a stack of just 10 different blocks would take him 42 days to exhaust.
48
188574
5198
03:13
That’s because the total time is a function of the factorial
49
193772
3830
03:17
of the number of blocks there are.
50
197602
2142
03:19
10 blocks have over 3 million combinations.
51
199744
3600
03:23
What this naïve solution shows is that we need a much faster way
52
203344
4280
03:27
to tell whether a pile of blocks can form a palindrome.
53
207624
3593
03:31
To start, it may be intuitively clear that a pile of all different blocks
54
211217
4716
03:35
will never form one.
55
215933
1420
03:37
Why?
56
217353
790
03:38
The first and last blocks can’t be the same if there are no repeats.
57
218143
5281
03:43
So when can a given sequence become a palindrome?
58
223424
5012
03:48
One way to figure that out is to analyze a few existing palindromes.
59
228436
4480
03:52
In ANNA, there are 2 A’s and 2 N’s.
60
232916
3254
03:56
RACECAR has 2 R’s, 2 A’s, 2 C’s, and 1 E.
61
236170
4886
04:01
And MADAM IM ADAM has 4 M’s, 4 A’s, 2 D’s, and 1 I.
62
241056
6730
04:07
The pattern here is that most of the letters occur
63
247786
3140
04:10
an even number of times,
64
250926
1774
04:12
and there’s at most 1 that occurs just once.
65
252700
3280
04:15
Is that it?
66
255980
1110
04:17
What if RACECAR had 3 E’s instead of 1?
67
257090
3260
04:20
We could tack the new E’s onto the ends and still get a palindrome,
68
260350
3710
04:24
so 3 is ok.
69
264060
1840
04:25
But make that 3 E’s and 3 C’s, and there’s nowhere for the last C to go.
70
265900
6064
04:31
So the most generalized insight is that
71
271964
2720
04:34
at most one letter can appear an odd number of times,
72
274684
4096
04:38
but the rest have to be even.
73
278780
3066
04:41
Hedge can count the letters in each stack and organize them into a dictionary,
74
281846
4314
04:46
which is a tidy way of storing information.
75
286160
2725
04:48
A loop could then go through and count how many times odd numbers appear.
76
288885
4579
04:53
If there are less than 2 odd characters, the stack can be made into a palindrome.
77
293464
5500
04:58
This approach is much, much faster than the naïve solution.
78
298964
3720
05:02
Instead of factorial time, it takes linear time.
79
302684
3420
05:06
That’s where the time increases
80
306104
1570
05:07
in proportion to the number of blocks there are.
81
307674
2710
05:10
Now write a loop for Hedge to approach the piles individually,
82
310384
3990
05:14
and stop when he finds a good one, and you’ll be ready to go.
83
314374
4155
05:18
Here’s what happens:
84
318529
1389
05:19
Hedge is fast, but there are so many piles it takes a long time.
85
319918
4046
05:23
Too long.
86
323964
1356
06:17
Ethic and Hedge are safe.
87
377897
1680
06:19
But Octavia is not so lucky.
88
379577
2423
About this website

This site will introduce you to YouTube videos that are useful for learning English. You will see English lessons taught by top-notch teachers from around the world. Double-click on the English subtitles displayed on each video page to play the video from there. The subtitles scroll in sync with the video playback. If you have any comments or requests, please contact us using this contact form.

https://forms.gle/WvT1wiN1qDtmnspy7