Skip to content

solve: 39. Combination Sum#52

Open
t9a-dev wants to merge 1 commit intomainfrom
39.Combination-Sum
Open

solve: 39. Combination Sum#52
t9a-dev wants to merge 1 commit intomainfrom
39.Combination-Sum

Conversation

@t9a-dev
Copy link
Copy Markdown
Owner

@t9a-dev t9a-dev commented Feb 19, 2026

問題: 39. Combination Sum
次に解く問題: 22. Generate Parentheses
ファイルの構成: ./src/bin/<各ステップ>.rs

Comment thread src/bin/step3.rs

if candidate_sum <= target_sum {
combination.push(candidate);
Self::make_combinations(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私は

for i in range(candidate_start, len(candidates)):
   ...
    combination.append(sorted_candidates[i])
    make_combinations(i, sum, combination)
    combination.pop()

のような形しか検討していなかったのですが、t9a-devさんのこの書き方の方が脳への収まりがよく感じました。各ステップで判断すべきことが、今のcandidateを(もう)一回使うか、先に進むか、の二択で直感的に理解しやすいように思いました。

Comment thread src/bin/step2.rs
https://discord.com/channels/1084280443945353267/1233295449985650688/1242146721094570034
- step1で時間計算量の見積もりに躓いてかなり時間使ったが、簡単に理解できるものではないという知識があれば避けられたなと思った。
candidates[i]の最小が1の場合は、分割数の極限の振る舞いというものを知っている必要がある。ただし、ソフトウェアエンジニアの常識には含まれていなさそう。
今回の問題では制約によりcandidates[i]の最小が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.

あ、最小が2の場合も分割数で表せてですね
a[i] を i を1以上の数の和で表す方法、b[i] を i を2以上の数の和で表す方法とすると、
a[i+1] の中身は2つに分けられますね。1を1回以上使っているものと0回使っているものです。
0回使っているやつの群れは b[i+1] いますね。1回以上使っているやつの群れから1を一つずつ供出してもらうと、これは a[i] ですね。
つまり、a[i+1] = b[i+1] + a[i] です。要するに、極限の振る舞いは分割数の極限の微分になります。

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.

3 participants