Skip to content

Conversation

@hroc135
Copy link
Owner

@hroc135 hroc135 commented Dec 28, 2024

#### 2b
- メモ付き再帰
- 時間・空間計算量はともにO(n)
- ??メモがないとO(2^n)時間になる??
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

フィボナッチになりそうですね。黄金比の n 乗なので 1.6^n くらいです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、ピンと来てないです、、

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo がないとしましょう。

robHelper(100) と呼ぶと、robHelper(99) と robHelper(98) が呼ばれて、再帰的に木ができあがりますね。一番下の葉は robHelper(0) と robHelper(1) です。

robHelper(0) と robHelper(1) は、かかった計算コストの請求書を呼び出した人たちに提出します。呼び出した人たちは請求書をホチキスで止めて呼び出し元に送ります。

最後、請求書の束ができあがりますね。請求書は何枚あって、ホチキスは何回止められましたか。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もう調べられているかもしれませんが、念の為。
https://ja.wikipedia.org/wiki/%E3%83%95%E3%82%A3%E3%83%9C%E3%83%8A%E3%83%83%E3%83%81%E6%95%B0#cite_note-algo-3

1.6というのは、上のφのことですね。
フィボナッチ数列の一般項は、Fn = Fn-1 + Fn-2という漸化式を解くと出てきます。
(φ, 1-φを解に持つ特性方程式x^2 = x + 1を考えて、Fn-φFn-1 = (1-φ)(Fn-1-φFn-2)を解く)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、返信遅くなりました

@oda
robHelper(n)における請求書の数をbill(n)という関数で表すと、bill(100) = bill(99) + bill(98) になり、bill(1) = bill(0) = 1 より、フィボナッチ数列からbill(100) ≒ 1.6^100 となるということですね。
ホチキスについてもほぼ同様でstapler(n) = stapler(n-1) + stapler(n-2), stapler(0) = stapler(1) = 0, stapler(2) = stapler(3) = 1 よりstapler(100) ≒ 1.6^(100-2) となります
なのでrobHelper関数が1.6^n回呼ばれ、return max(twoBefore+nums[tailIndex], oneBefore)が1.6^(n-2)回計算されるということですね

図示した時に二分木に見えることに囚われてしまい、bill(100) = bill(99) + bill(98)に気づくのに時間がかかりました

@TORUS0818
数学の解説ありがとうございます!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい。stapler(3)は2でしょうか。まあ、ホチキスの数は、請求書-1ですね。

- しかし、入力[2,1,1,2]でWA
- 2個飛ばしの場合を想定できていなかった
- よく考えたらテストケースが少なすぎる。手抜きしない
- テストケースを考える時にパターンをすべて網羅できているかよく考えたほうがいい
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

手下を各々の家の前に立たせて伝言をさせたら、自分の前を盗む場合と盗まない場合の話をすると思うんですよね。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほどです。ちなみにodaさんはよく例えを使われますがそれは人に説明をするために実世界の例を作り出されていますか?それとも普段のコーディングのときから何かに例えていらっしゃるんですか?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

喩えていないですね。ただ、普段から、もうちょっと抽象的なものに人格を感じているので、あだ名をつけているくらいの感じです。


#### 2b
- メモ付き再帰
- 時間・空間計算量はともにO(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

たまに再帰上限について呟いておくといいかもしれません。

continue
}
if i == 1 {
twoBefore, oneBefore = oneBefore, max(oneBefore, n)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代入を一気にするのは、少し見にくく感じます
https://discord.com/channels/1084280443945353267/1195700948786491403/1195724750895452220

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmpTwoBefore := twoBefore

みたいに値を一時的に記憶するための変数が必要になると勘違いしていました。単に以下のように2行に分けて全く問題なかったですね。ありがとうございます

twoBefore = oneBefore
oneBefore = max(oneBefore, n)

continue
}
currentMax := max(twoBefore+n, oneBefore)
twoBefore, oneBefore = oneBefore, currentMax
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxtwoBeforeや、twoBeforeなどの変数名から、「2つ前を最後に取った場合の最大値」「もし配列が2つ前までだった場合の最大値」などの細かい違いを読み取るのは難しいですね。
ただ、この辺ちゃんと説明しようと思うと長くないそうで難しいです。
コメントに書くか、もしくはStep3のrobbedLastなどの変数名は意味的にわかりやすく感じます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants