The Chasm Think Like A Coder Ep 6

Ethic, Hedge, and Octavia
stand on the edge of a bottomless ravine.

It’s the only thing between
them and the tower

that houses the second
of three powerful artifacts.

They’ve got a brief window of time
to get across before the guards return.

With Hedge’s fuel gauge on empty
he won’t be able to fly Ethic across,

so the only option is to make a bridge.

Fortunately, the floating stacks of stones
nearby are bridge components—

invented by Octavia herself—
called hover-blocks.

Activate a pile with a burst of energy,

and they’ll self-assemble
to span the ravine as Ethic walks across.

But there is, of course, a catch.

The hover-blocks are only stable
when they’re perfectly palindromic.

Meaning they have to form
a sequence

that’s the same when viewed
forwards and backwards.

The stacks start in random orders,

but will always put themselves
into a palindromic configuration

if they can.

If they get to a point
where a palindrome isn’t possible,

the bridge will collapse,

and whoever’s on it
will fall into the ravine.

Let’s look at an example.

This stack would make itself stable.

First the A blocks
hold themselves in place.

Then the B’s.

And finally the C
would nestle right between the B’s.

However, suppose there was one more A.

First two A blocks form up, then two B’s,

but now the remaining C and A
have nowhere to go,

so the whole thing falls apart.

The Node of Power enables Hedge
to energize a single stack of blocks.

What instructions can Ethic give Hedge
to allow him to efficiently find

and power a stable palindromic stack?

Pause now to figure it out for yourself.

Examples of palindromes include ANNA,
RACECAR, and MADAM IM ADAM.

Counting the number of times
a given letter appears in a palindrome

will reveal a helpful pattern.

Pause now to figure it out for yourself.

Let’s first look at a naïve solution
to this problem.

A naïve solution is a simple,
brute-force approach that isn’t optimized—

but will get the job done.

Naïve solutions are helpful ways
to analyze problems,

and work as stepping stones
to better solutions.

In this case, a naïve solution
is to approach a pile of blocks,

try all the arrangements,

and see if one is a palindrome
by reading it forward and then backwards.

The problem with this approach

is that it would take
a tremendous amount of time.

If Hedge tried one combination
every second,

a stack of just 10 different blocks
would take him 42 days to exhaust.

That’s because the total time
is a function of the factorial

of the number of blocks there are.

10 blocks
have over 3 million combinations.

What this naïve solution shows
is that we need a much faster way

to tell whether a pile of blocks
can form a palindrome.

To start, it may be intuitively clear
that a pile of all different blocks

will never form one.

Why?

The first and last blocks
can’t be the same if there are no repeats.

So when can a given sequence
become a palindrome?

One way to figure that out
is to analyze a few existing palindromes.

In ANNA,
there are 2 A’s and 2 N’s.

RACECAR
has 2 R’s, 2 A’s, 2 C’s, and 1 E.

And MADAM IM ADAM
has 4 M’s, 4 A’s, 2 D’s, and 1 I.

The pattern here
is that most of the letters occur

an even number of times,

and there’s at most 1
that occurs just once.

Is that it?

What if RACECAR had 3 E’s instead of 1?

We could tack the new E’s onto the ends
and still get a palindrome,

so 3 is ok.

But make that 3 E’s and 3 C’s,
and there’s nowhere for the last C to go.

So the most generalized insight is that

at most one letter can appear
an odd number of times,

but the rest have to be even.

Hedge can count the letters in each stack
and organize them into a dictionary,

which is a tidy way
of storing information.

A loop could then go through and count
how many times odd numbers appear.

If there are less than 2 odd characters,
the stack can be made into a palindrome.

This approach is much, much faster
than the naïve solution.

Instead of factorial time,
it takes linear time.

That’s where the time increases

in proportion to
the number of blocks there are.

Now write a loop for Hedge
to approach the piles individually,

and stop when he finds a good one,
and you’ll be ready to go.

Here’s what happens:

Hedge is fast, but there are so many piles
it takes a long time.

Too long.

Ethic and Hedge are safe.

But Octavia is not so lucky.

Ethic、Hedge 和 Octavia
站在无底峡谷的边缘。

这是
他们与

容纳
三件强大神器中第二件的塔之间的唯一东西。 在警卫返回之前,

他们有一个短暂的时间窗口
可以通过。

由于 Hedge 的油量计为空,
他将无法飞过 Ethic,

因此唯一的选择是建造一座桥梁。

幸运的是,附近的漂浮石堆

由 Octavia 自己发明的桥梁组件,
称为悬停块。

用一阵能量激活一堆,当 Ethic 走过时

,它们将自行组装
以跨越峡谷。

但是,当然,有一个问题。

悬停块只有
在完全回文时才会稳定。

这意味着它们必须形成
一个


向前和向后观察时相同的序列。

堆栈以随机顺序开始,

但如果可以的话,总是会将自己
置于回文配置

中。

如果他们到
了不可能回文的地步

,桥就会倒塌,任何在桥上的


都会掉进峡谷。

让我们看一个例子。

该堆栈将使自身稳定。

首先,A 块
将自己固定在适当的位置。

然后是B。

最后,C
将紧贴在 B 之间。

但是,假设还有一个 A。

首先形成了两个 A 块,然后形成了两个 B,

但是现在剩下的 C 和
A 无处可去,

所以整个事情就崩溃了。

权力节点使 Hedge
能够激活单个堆叠块。

Ethic 可以给 Hedge 什么指示
,让他能够有效地找到

稳定的回文堆栈并为其提供动力?

现在停下来自己弄清楚。

回文的例子包括 ANNA、
RACECAR 和 MADAM IM ADAM。

计算
给定字母在回文中出现的次数

将揭示一个有用的模式。

现在停下来自己弄清楚。

让我们首先看一下
这个问题的简单解决方案。

天真的解决方案是一种简单的
蛮力方法,没有经过优化,

但可以完成工作。

幼稚的解决方案是
分析问题的有用方法,

并且可以作为
更好的解决方案的垫脚石。

在这种情况下,一个天真的解决方案
是接近一堆块,

尝试所有排列,


通过向前和向后读取它来查看一个是否是回文。

这种方法的问题

在于它会
花费大量时间。

如果 Hedge 每秒尝试一个组合

那么只有 10 个不同的积木叠在一起,
他需要 42 天才能筋疲力尽。

这是因为总时间

块数的阶乘函数。

10 个区块
拥有超过 300 万种组合。

这个幼稚的解决方案
表明,我们需要一种更快的方法

来判断一堆块是否
可以形成回文。

首先,直觉上很清楚
,一堆不同的块

永远不会形成一个。

为什么?

如果没有重复,第一个和最后一个块
不能相同。

那么一个给定的序列什么时候可以
变成回文呢?

解决这个问题的一种方法
是分析一些现有的回文。

在 ANNA 中,
有 2 个 A 和 2 个 N。

RACECAR
有 2 个 R、2 个 A、2 个 C 和 1 个 E。

而 MADAM IM ADAM
有 4 个 M、4 个 A、2 个 D 和 1 个 I。

这里的模式
是大多数字母

出现偶数次,

并且 最多有 1
只发生一次。

是这样吗?

如果 RACECAR 有 3 个 E 而不是 1 个呢?

我们可以将新的 E 加到末端
,仍然得到一个回文,

所以 3 是可以的。

但是做出 3 个 E 和 3 个 C,
最后一个 C 无处可去。

所以最普遍的见解是

,一个字母最多可以
出现奇数次,

但其余的必须是偶数。

Hedge 可以对每个堆栈中的字母进行计数,
并将它们组织成字典,

这是一种
存储信息的整洁方式。

然后一个循环可以通过并计算
奇数出现的次数。

如果奇数字符少于 2 个,
则可以将堆栈变成回文。

这种方法
比天真的解决方案要快得多。

它需要线性时间,而不是阶乘时间。

这就是时间

与区块数量成比例增加的地方。

现在为 Hedge 编写一个循环
来单独接近这些桩,

并在他找到一个好的时停下来
,你就可以开始了。

事情是这样的:

对冲速度很快,但是有很多
桩需要很长时间。

太长。

Ethic 和 Hedge 是安全的。

但奥克塔维亚就没那么幸运了。