9
1
A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. A strictly increasing subsequence is a subsequence in which every element is bigger than the preceding one.
The heaviest increasing subsequence of a sequence is the strictly increasing subsequence that has the biggest element sum.
Implement a program or function in your language of choice that finds the element sum of the heaviest increasing subsequence of a given list of non-negative integers.
Examples:
[] -> 0 ([])
[3] -> 3 ([3])
[3, 2, 1] -> 3 ([3])
[3, 2, 5, 6] -> 14 ([3, 5, 6])
[9, 3, 2, 1, 4] -> 9 ([9])
[3, 4, 1, 4, 1] -> 7 ([3, 4])
[9, 1, 2, 3, 4] -> 10 ([1, 2, 3, 4])
[1, 2, 4, 3, 4] -> 10 ([1, 2, 3, 4])
[9, 1, 2, 3, 4, 5, 10] -> 25 ([1, 2, 3, 4, 5, 10])
[3, 2, 1, 2, 3] -> 6 ([1, 2, 3])
Note that you only have to give the element sum of the heaviest increasing subsequence, not the subsequence itself.
The asymptotically fastest code wins, with smaller code size in bytes as a tiebreaker.
How do you plan to deal with incomparable asymptotics? There are potentially two important variables: the length of the sequence, and the size of the largest element in the sequence. – Peter Taylor – 2015-08-01T10:41:07.460
@PeterTaylor I choose length of the sequence as the asymptotic. Your solution must not assume any bound on the integers, and in particular not loop or allocate memory based on the size of the numbers involved. You are forgiven if your language choice has bounded integers, but you must not make use of this fact in your solution. Does that satisfy your concerns? – orlp – 2015-08-01T10:46:38.990
Partially. It's still theoretically possible (although probably unlikely) that the fact that comparison of two unbounded integers takes size proportional to their log could be relevant. You might want to allow basic operations (addition, comparison, maybe multiplication) on the integers to be assumed to be O(1) time. – Peter Taylor – 2015-08-01T11:41:59.577
@PeterTaylor Is the transdichotomous model of computation specific enough?
– orlp – 2015-08-01T11:50:20.630Seems reasonable. – Peter Taylor – 2015-08-01T12:36:42.727
How do we measure asymptotic fastness? Do you mean big O? – Luis Mendo – 2015-08-01T14:57:21.903
@LuisMendo Yes, worst case performance. – orlp – 2015-08-01T15:13:30.770
What should
[1, 2, 4, 3, 4]
return?7
as in[1, 2, 4]
or[3, 4]
, or10
as in[1, 2, 3, 4]
? – Alexander Vogt – 2015-08-01T17:34:54.713@AlexanderVogt 10. – orlp – 2015-08-02T00:28:03.820
@PeterTaylor I'm not certain why you removed the code-golf tag, it's the secondary scoring requirement (and once O(n log n) answers start streaming in, the only one that matters). – orlp – 2015-08-02T00:30:34.480
Because it's only really appropriate where it's the primary scoring criterion. I did also consider removing both [tag:code-golf] and [tag:fastest-algorithm] and replacing them with [tag:code-challenge] since it's a bit of a hybrid. – Peter Taylor – 2015-08-02T05:48:52.837