algorithm - Number of increasing subsequences in sequence of [0-9] elements -
in case sequence of unknown range value elements, question number of increasing subsequences in given sequence give solution in o(n^2).
i've heard of solution in o(9*n) in case of sequence composed of elements in interval [0,9] only. if know algorithm please let me know that.
here algorithm:
1)let's call dp[i]
= number of increasing subsequences have last element i
(0 <= <= 9
). filled zeros.
2)now can iterate on sequence , compute dp
in following way:
let's assume current element d
(0 <= d <= 9
). dp
can updated this:
for prev = 0...d - 1 dp[d] += dp[prev] //continue 1 of previous sequences. dp[d] += 1 //start new sequence consists of 1 element.
3)after iterating on elements of sequence,
answer sum of dp[i]
0 <= <= 9
.
note algorithm has desired complexity under assumption arithmetic operations have o(1)
time complexity(that might not case because number of increasing subsequences can large).
Comments
Post a Comment