アダプタ
コード2020日10の出現
Nulldevのアルゴリズムを使用してPart 2のシミュレータを試してください
課題:xのxを解決する
例入力
16
10
15
5
1
11
7
19
6
12
4
It represents:第1部
混乱、それから明快さ
アルゴリズムの記述
Process the input into a sorted array of numbers
Insert two values: one at the beginning and another at the end
Accumulate a 3-element array representing tallies for each 1-, 2- and 3-integer difference between each subsequent value
Return the product of the integers tallying all 1- and 3-integer differences
私のアルゴリズムの可視化を示します.第2部
私はそれを見たはずだ
私はそれを考え出したと思った
最初の例の入力を参照として使用します.
1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19
私の不完全な論理は解決に向かってこの経路に続きましたLegend:
0 numbers in-between = 1
1 number in-between = 2
2 numbers in-between = 4
1 - 4: 0 -> 1
4 - 7: 2 -> 4
7 - 10: 0 -> 1
10 - 12: 1 -> 2
12 - 15: 0 -> 1
15 - 16: 0 -> 1
16 - 19: 0 -> 1
1 * 4 * 1 * 2 * 1 * 1 * 1
4 * 2
8
残念なことに、この公式を第2の例で使用して、2000年を生産しました.私は、それが単純であるということを知っていました!
ありがたいことに、私のJavaScriptのソルバー、Nulldevは、私は完全に理解するために強要感じたエレガントな、サブ5行のソリューションを書いた.シミュレーションを使用する!
Create step, a 1-element array containing the value 1
For i from 0 to the maximum value in the sorted array of numbers from the input
Set j to i + 1
While the number at location j in the sorted array is less than or equal to i + 3
Set the item at location j in step equal to the sum of the item at location i in step and either the item at location j in step or 0
Increment j
Return the last value in step
私は私の頭でそれについて推論するとき、Nulldevのアルゴリズムが働いた方法について混乱しました.使用後も
console.log
最後に、いっぱいになるstep
配列では、各繰り返しで何が行われているのかまだわかりませんでした.私は、各々の反復をシミュレーションする必要があるということを知っていました.
私はそれがどのように働いたか見なければならなかった
if...else
の代わりに節for
and while
ループは難しすぎたNulldevの解決策を見てみると、私は1 - 2 - 4倍の論理で「何か」になっていた.しかし、明らかに私のアプローチは浅かった.
私はパート2を解決していないかもしれませんが、私はこのパズルを楽しんで麺を楽しんでいたし、別のソルバーのアルゴリズムをシミュレートした.
Reference
この問題について(アダプタ), 我々は、より多くの情報をここで見つけました https://dev.to/rmion/adapter-array-15acテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol