Conversation
nodchip
reviewed
Sep 27, 2026
| ## step1 | ||
| 30m以上考えて分からなかったので Hint として以下を見て解いた。合計で1h以上かかった。 | ||
|
|
||
| k=2までは自力で考えられたが、k > 2 の答えに自力で辿り着けなかった。 |
There was a problem hiding this comment.
自分も自力でたどり着けませんでした。 k=2 の場合は、連鎖行列積問題に似ているように感じました。
https://en.wikipedia.org/wiki/Matrix_chain_multiplication
最後のマージの直前の状態を作るための最小をコストを先に求めたあと、マージのコストを追加する、という点がポイントのようですが、自力で発想できる気がしませんでした。
Owner
Author
There was a problem hiding this comment.
連鎖行列積問題は初めて知りました。二つの行列をまとめる処理や動的計画法で解ける点は似ていそうです。
よく復習しようと思います。
|
|
||
| prefix_sum = list(itertools.accumulate(stones, initial=0)) | ||
|
|
||
| dp = [[[float("inf")] * (k + 1) for _ in range(num_stones)] for _ in range(num_stones)] |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
Yuto729/leetcode#105 (comment)
min_cost あたりが良いと思います。
Owner
Author
There was a problem hiding this comment.
min_cost としました。dp はなるべく使わないように気をつけます。
| dp[i][i][1] = 0 | ||
|
|
||
| for length in range(2, num_stones + 1): | ||
| for i in range(num_stones - length + 1): |
There was a problem hiding this comment.
i と j の出現回数が多く、登場するたびに i と j が何を表すかを考える認知負荷が高そうだと感じました。 left と right あたりにしたほうが認知負荷が低くなりそうだと思いました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/minimum-cost-to-merge-stones/description/