3

I have a stack with integer values objects. I want to buy a truck with capacity N( N is unknown) to be sure that I can transport all the objects in maximum X laps.

X is known. In other words, I have to partition the stack( the order of objects must be maintained) In maximum X subsets with sum lower than N and find that minimum N.

Can you help me with an algorithm or an idea, please? Thanks.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120

1 Answers1

2

If, as you state, "the order of objects must be maintained," then we can solve this with binary-search on N, in O(|objects| * log m), where m is the total sum.

The paper @hlt linked to in a comment, about multi way number partitioning, would apply if the objects' order could be rearranged. In this case, where the order is fixed, we can just try different Ns, packing the partitions as much as we can. If the N we pick is too small, we'll end up surpassing X. This makes N ordered and therefore searchable.

גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61