For input data = {"a": 1, "b": 1, "c": 3}
, I would like to obtain the dict that looks like
{1: [["a"], ["b"]],
2: [["a", "b"]],
3: [["c"]],
4: [["a", "c"], ["b", "c"]],
5: [["a", "b", "c"]]}
where the key is all possible cumulative sum of the values of input dictionary and corresponding values are the list of all keys make the cumulative sum possible.
The difficulty here is that there could be multiple solutions to get the same cumulative sum (the above 1
is an example). Now I know I could do
{v: list(data.keys())[:idx+1] for v, idx in zip(np.cumsum(list(data.values())), range(len(data)))}
But this approach does not give complete solution I want. Concretely, for input data
, I could only get
{1: ['a'],
2: ['a', 'b'],
5: ['a', 'b', 'c']}
And when there are more duplicates in data
, the problem could become more difficult to resolve.
I am wondering if there is good solution to this problem (this problem seems to be NP-complete from my computation theory course).