← Back to context

Comment by NAR8789

1 year ago

I think you can reduce state. Rather than tracking maxElementReached per-element, maintain a single maxElementReached for the first n elements. March the first n elements forward in lockstep, and grow n by 1 whenever you exhaust all available combinations for that set

  1. Combine the first element with every next element until exhausted.
  2. Catch up the second element to where the first element got to.
  3. Combine the first two elements with every next element, until exhausted.
  4. Catch up the third element.
  5. Combine the first three elements with every next element
  6. etc.

In pseudocode...

  n = 1
  maxElementReached = -1
  
  while(n < totalElements()) {
    while(maxElementReached + 1 < totalElements()) {
      maxElementReached = maxElementReached + 1
      Combine each of the first n elements with element[maxElementReached]
    }

    // we've exhausted all possible combinations for the first n elements.
    // increase n by 1 and catch up the new element to keep going
    Combine element[n] with each element from n to maxElementReached
    n = n + 1
  }