Skip to content

Minimum Cost To Merge Stones - #171

Open
tom4649 wants to merge 2 commits into
mainfrom
1000.Minimum-Cost-to-Merge-Stones
Open

tom4649 wants to merge 2 commits into
mainfrom
1000.Minimum-Cost-to-Merge-Stones

Conversation

@tom4649

@tom4649 tom4649 commented Sep 27, 2026

Copy link
Copy Markdown
Owner

## step1
30m以上考えて分からなかったので Hint として以下を見て解いた。合計で1h以上かかった。

k=2までは自力で考えられたが、k > 2 の答えに自力で辿り着けなかった。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分も自力でたどり着けませんでした。 k=2 の場合は、連鎖行列積問題に似ているように感じました。
https://en.wikipedia.org/wiki/Matrix_chain_multiplication

最後のマージの直前の状態を作るための最小をコストを先に求めたあと、マージのコストを追加する、という点がポイントのようですが、自力で発想できる気がしませんでした。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

連鎖行列積問題は初めて知りました。二つの行列をまとめる処理や動的計画法で解ける点は似ていそうです。

よく復習しようと思います。


prefix_sum = list(itertools.accumulate(stones, initial=0))

dp = [[[float("inf")] * (k + 1) for _ in range(num_stones)] for _ in range(num_stones)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
Yuto729/leetcode#105 (comment)

min_cost あたりが良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

min_cost としました。dp はなるべく使わないように気をつけます。

dp[i][i][1] = 0

for length in range(2, num_stones + 1):
for i in range(num_stones - length + 1):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i と j の出現回数が多く、登場するたびに i と j が何を表すかを考える認知負荷が高そうだと感じました。 left と right あたりにしたほうが認知負荷が低くなりそうだと思いました。

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.

2 participants