Subsequence with minimal absolute value

北慕城南 提交于 2019-12-24 02:19:07

问题


This is an interview question. Given an integer array find a subsequence (not necessarily contiguous) for which the absolute value of the sum of its elements is minimized.

It looks like a DP problem.

  • Let S1[i] is a subsequence ending at a[i] for which its sum > 0 and abs(sum) is minimized.

  • Let S2[i] is a subsequence ending at a[i] for which its sum < 0 and abs(sum) is minimized.

  • S1[i] is the minimum of all S1[j] + a[i] for j < i if S1[j] + a[i] > 0 && a[i] < 0

  • S2[i] is the minimum of all S2[j] + a[i] for j < i if S2[j] + a[i] < 0 && a[i] > 0

Once we now S1[i] and S2[i] for all indexes it is easy to find the subsequence with the mininimal absolute value of its elements.

Does it make sense?


回答1:


I'm assuming you want the minimum absolute sum among non-empty subsequences. (Otherwise as mentioned in the comments, the empty subsequence has sum 0.)

Since the order of the elements doesn't matter, your question just asks: given a (multi)set of elements, what is the minimum absolute sum among all subsets. It is easy to see that the subset sum problem reduces to this problem. Since subset sum is NP-hard so is this problem. Therefore, it is a good bet that your polynomial time algorithm is wrong. Otherwise, P = NP.

In fact, a counterexample to your algorithm is the input sequence {-1, 2, -2}.

Standard approaches to the subset sum problem can be used to obtain pseudo-polynomial time algorithms for your problem.




回答2:


I wish I could follow your reasoning but I'm a little slow...also you asked for DP and here's Haskell again...but is this what you mean?

import Data.List (sortBy, subsequences)
import Data.Ord (comparing)

minValSub xs = 
  head $ sortBy (comparing snd) 
  $ map (\x -> (x, abs (sum x)) ) (filter (not . null) $ subsequences xs)


OUTPUT:
*Main> minValSub [1,2,3,-4,5]
([1,3,-4],0)



回答3:


I am assuming non-empty result sets.

Let the list of integers be S. Consider the smallest absolute value among them S[k]. If S[K] == 0 return it. Else the goal is to find a value smaller than S[K].

Divide the integers into two sets of positive and negative integers as you mentioned SP (non-negative) and SN. Now find the sum in SP which is as close to another sum in SN by less than S[K]. This can be done by sorting the elements in SP and SN separately on absolute values, keeping a sum and a head pointer on each list. You can fill in the details.

This could give something that is lesser than S[K], else report S[K].

For example: S = {1, -4, 2, -8, 5, -7} k = 0, S[k] = 1

SP (sorted) = {1, 2, 5} SN (sorted) = {-4, -7, -8}

Traversing the two arrays simultaneously you can get some candidates {1,2} and {-4} which will give the same result as S[k]. but {2,5} and {-7} will be better giving a net sum of 0.



来源:https://stackoverflow.com/questions/15343500/subsequence-with-minimal-absolute-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!