3

There is only one rule to follow: each group's sum should be greater or equal to the group on right side of it.

My guess is to build a tree with all the options for partitioning exist and then recursive backtracking.

For example, the array 14 13 2 11

The result : 3. 3 groups ({14}, {13}, {2, 11})

Do you think my guess is true? if not do you have other solution to the problem?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
itzikos
  • 375
  • 1
  • 5
  • 13
  • If all elements must be positive, I guess greedy approach will work here, though I might be missing something – amit Oct 23 '15 at 15:12
  • tried greedy approach from both sides and found input that ruled out the greedy approach – itzikos Oct 23 '15 at 15:16
  • Can you please show me counter example for greedy from right side? As I said, I might be missing something, and the counter example will show me what. (If negatives are allowed, I am convinced it's not going to work) – amit Oct 23 '15 at 15:18
  • I think 10,10,1,2,1,2 is a counterexample (greedy does 10,10,1|2,1|2 I guess since it gets stuck trying to make the last group after grabbing 10,1; optimal is 10|10|1,2,1|2). – David Eisenstat Oct 23 '15 at 15:20
  • Certainly there's an O(n^2)-time DP, and probably a faster one, so backtracking is not the best approach. – David Eisenstat Oct 23 '15 at 15:24
  • you're right David there is a solution that is a better than O(n^2*sqrt(n)) i have been told – itzikos Oct 23 '15 at 15:27
  • Are the array entries all nonnegative? – David Eisenstat Oct 23 '15 at 15:30
  • yes all numbers from 1 to 5n^2 – itzikos Oct 23 '15 at 15:31

1 Answers1

1

Here's an O(n^2)-time algorithm, where n is the length of the array. I retract my comment that there's "probably" a faster one.

There's a simpler O(n^4)-time algorithm that illustrates the main idea of the dynamic program. We prepare a table of entries T(i, j) where the i, j entry contains the maximum number of groups into which the array elements indexed [0, j) can be grouped suitably in such a way that the last group has indexes [i, j).

We have a recurrence

T(0, j) = 1 for all j
T(i, j) =          max          T(h, i) + 1,
          h : S(h, i) ≥ S(i, j)

where S(i, j) is the sum of array elements indexed [i, j), and an empty max is taken to be minus infinity. The answer is

max T(i, n).
 i

We get to O(n^4) because for O(n^2) table entries, we compute a maximum over O(n) sums, each of O(n) items.

We make two optimizations. The first is easy: update the sum S(h, i) incrementally as we vary h. This drops the cost to O(n^3). We can do the same for S(i, j), but to no effect yet assuming that we sensibly hoisted it out of the max loop.

The second depends on nonnegative entries. For particular i, j, the set of valid h is an interval like [0, k), possibly empty. For i fixed and j decreasing, the sum S(i, j) is nonincreasing, hence the interval does not shrink. This means that we can update the max incrementally as well, yielding an O(n^2)-time algorithm.

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