Conversation
Mike0121
commented
Apr 24, 2024
- Combination Sum https://leetcode.com/problems/combination-sum/description/?envType=list&envId=rbx9vwti
競技プロ就活部PR用/39. Combination Sum
Outdated
There was a problem hiding this comment.
current_combinationは、all_valid_combinationのように外に置けます。一般にパラメータの数は少ない方がいいです。
There was a problem hiding this comment.
可能な限り関数化してしまった方が良いかと勝手に思っていたので、覚えておきます。
一般に関数のパラメータの数は少ない方がいい
There was a problem hiding this comment.
関数化した方がいいのはその通りなのですが、パラメータの数についての指摘でした。この関数ならindex, current_target, current_combinationの3個ありますが、current_combinationはいつも同じです。def find_vaild_combination(index, current_target, current_combination):
競技プロ就活部PR用/39. Combination Sum
Outdated
There was a problem hiding this comment.
変化のある方を比較演算子の左側に置く方がわかりやすいかもしれません。index == len(candidates)
There was a problem hiding this comment.
ありがとうございます。こちらはリーダブルコードでも同様の指摘があった記憶があり、修正しておきます。
変化のある方を比較演算子の左側に置く方がわかりやすい
競技プロ就活部PR用/39. Combination Sum
Outdated
競技プロ就活部PR用/39. Combination Sum
Outdated
There was a problem hiding this comment.
空間計算量合っていますか?例えば、targetが1,000,000で、candidatesが[1, 2]だったらどうですか?
There was a problem hiding this comment.
ご指摘ありがとうございます。
自信がないので、改めてアドバイスをもとに考えを書きますので、再度確認していただけたら嬉しいです。
最悪空間計算量は、O(target/min(candidates))でしょうか?
長さ1,000,000の[1, 1, 1, 1, ....1]のリストになるので、この場合、O(1,000,000)になります。
また、targetが13、candidates が例えば、[2, 100]の場合、最悪空間計算量は長さ[2, 2, 2, 2, 2, 2]のリストを試す必要があるので、このようになると考えました。
最悪時間計算量は、現状こんがらがっています。
There was a problem hiding this comment.
答えの数がどれだけかは分からないのですが、答えを保存するスペースを無視すると空間計算量はそれで合ってると思います。
時間計算量については、探索空間をN-ary木で考えると分かるかと思います。詳細はhttps://leetcode.com/problems/combination-sum/editorial を参照。
There was a problem hiding this comment.
答えの数ですが、candidates = [1..target] の場合、これは分割数というものですね。
分割数の極限は、ハーディとラマヌジャンが求めています。
a(n) ~ 1/(4nsqrt(3)) * e^(Pi * sqrt(2n/3)) as n -> infinity (Hardy and Ramanujan)
https://oeis.org/A000041
だいたい、13^sqrt(n) / 7n くらいですか。
There was a problem hiding this comment.
ありがとうございます。
時間計算量をコードに沿った二分木で考えていたので複雑になってしまったかと思います。
選択肢がcandidate数分(N)あり、最大の木の深さがtarget/min(candidates)のため、最大で計算は、根ノード分を考慮しN^(target/min(candidates) + 1)繰り返されるイメージです。
答えの数の分割数も少し調べて見ました。色々な求め方(表現形式)が合って面白かったです。
競技プロ就活部PR用/39. Combination Sum
Outdated
競技プロ就活部PR用/39. Combination Sum
Outdated
There was a problem hiding this comment.
PEPは未確認ですが、演算子の周りにスペースを入れるのが一般的ではないでしょうか?index + 1、current_target - candidates[index]
There was a problem hiding this comment.
PEPを確認して、ご指摘の通りでした。
今週中に忘れないうちにPEPに目を通しておきます。
代入(や他の)演算子を揃えるために、演算子の周囲に1つ以上のスペースを入れる
ソース: https://pep8-ja.readthedocs.io/ja/latest/#id9
There was a problem hiding this comment.
```pythonと書くとsyntax highlightingがつきます
There was a problem hiding this comment.
typo: find_all_combiantions -> find_all_combinations
There was a problem hiding this comment.
ありがとうございます、これはダメですね。以後、より厳密にtypoの見直しを気をつけます。