Re: Minimum sub-sequence sum
- From: daw@xxxxxxxxxxxxxxxxxxxxxxxx (David Wagner)
- Date: Tue, 22 Jul 2008 03:49:23 +0000 (UTC)
Michael Jarrod wrote:
I have a sequence of integers (positive/negative values) and I would
like to find the sub sequence of consecutive values that yields the
smallest sum, I can think of a very simple O(n^3) complex solution,
however I was wondering are there any better solutions?
Yes, there are better algorithms. This is a classic homework problem
in algorithms. Are you taking an algorithms course? Is this is a
homework problem?
Rather than telling you the answer, I will let you have the experience
of finding the algorithm yourself -- algorithms is an area that is best
learnt by solving algorithm design problems yourself rather than just
looking up the solution at the back of the book. (While the process of
searching for a better algorithm yourself can be frustrating and painful,
it's the best way I know to learn the subject.)
You should find it very easy to design a O(n^2) algorithm. But can
you do even better?
If you're stuck, here are some warmup problems to consider that may help.
Suppose you wanted to find the prefix of your sequence that yields the
smallest sum. In other words, this is a variant of your problem where
we only consider consecutive subsequences that include the start of the
original sequence (i.e., we look for a value i such that the sum of the
elements at indices 0,1,..,i gives the smallest possible sum). How fast
could you do it? What about another variant where you are given the
original sequence and also a number j, and want to find the value i such
that the sum of the elements at indices 0,1,..,i is minimized? How fast
can you do it? Suppose you wanted to compute the solution to the latter
problem for every value of j (so that the output of your algorithm is
N answers, one for each possible value of j)? How fast can you do that?
.
- Follow-Ups:
- Re: Minimum sub-sequence sum
- From: Michael Jarrod
- Re: Minimum sub-sequence sum
- References:
- Minimum sub-sequence sum
- From: Michael Jarrod
- Minimum sub-sequence sum
- Prev by Date: Minimum sub-sequence sum
- Next by Date: Re: Minimum sub-sequence sum
- Previous by thread: Minimum sub-sequence sum
- Next by thread: Re: Minimum sub-sequence sum
- Index(es):
Relevant Pages
|