Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I didn't spend enough time to fully understand your code, but it feels off.

    I'd attack it with a recursive routine that recurses for each possible component (ordered from largest down to smallest). For each one try as many as possible, down to zero, for each recursive attempt. I'd also keep track of the solution (with a hash of component->count pairs), since returning the way of achieving the maximum seems as important as finding the maximum possible. Short circuit the return if the exact target is achived, to bypass the n**2 number of cases. Another short circuit would be to keep track of each total that had been seen and the number of remaining components that were available to be applied - if the same total is later seen, it can be skipped if there are the same or fewer components left. (I.e. with [9,7,4,3] we see 16 as [9,7] and analyse all ways of adding 4 and/or 3 to it; so when we later see 16 as [4,4,4,4] and only have the option of adding 3's to it, there is no need to investigate. On the alternative, with [9,6,5,3,2] we will see 12 as [9,3] with the possibility of adding 2's; then later see 12 as [6,6] with the possibility of adding 5's, 3's, or 2's - there may be alternatives there that have not yet been examined.)