質問はleetcodeです。私は以下のコードを思いついたが、時間の複雑さを知るのは難しい。任意のアイデアはどのようにその時間の複雑さを計算する? (どのような辞書メモリなしの場合)Distinct Subsequencesの時間複雑度を計算する
public int NumDistinct (string s, string t)
{
if (string.IsNullOrEmpty (s) && string.IsNullOrEmpty (t))
return 1;
else if (string.IsNullOrEmpty (s) || string.IsNullOrEmpty (t))
return 0;
return FindSequences (s, 0, t, 0);
}
Dictionary<string, int> memoery = new Dictionary<string, int>();
private int FindSequences (string s, int idxs, string t, int idxt)
{
if (idxt == t.Length)
return 1;
else if (idxs == s.Length)
return 0;
string key = string.Format ("{0}-{1}", idxs, idxt);
if (memoery.ContainsKey (key))
return memoery [key];
int result = 0;
if (s [idxs] == t [idxt]) {
result = FindSequences (s, idxs + 1, t, idxt + 1) + FindSequences (s, idxs + 1, t, idxt);
} else {
result = FindSequences (s, idxs + 1, t, idxt);
}
memoery.Add (key, result);
return result;
}
「辞書メモ」を削除した場合、時間の複雑さは何ですか? –
jojo
'memoisation step'を削除しているので、時間の複雑さは'指数関数 'つまり' O(2^SizeOf(t)) ' – uSeemSurprised
となります。 – jojo