Finding all possible combinations of numbers to reach a given sum

前端 未结 30 3034
一个人的身影
一个人的身影 2020-11-21 06:39

How would you go about testing all possible combinations of additions from a given set N of numbers so they add up to a given final number?

A brief exam

相关标签:
30条回答
  • 2020-11-21 06:59

    I did not like the Javascript Solution I saw above. Here is the one I build using partial applying, closures and recursion:

    Ok, I was mainly concern about, if the combinations array could satisfy the target requirement, hopefully this approached you will start to find the rest of combinations

    Here just set the target and pass the combinations array.

    function main() {
        const target = 10
        const getPermutationThatSumT = setTarget(target)
        const permutation = getPermutationThatSumT([1, 4, 2, 5, 6, 7])
    
        console.log( permutation );
    }
    

    the currently implementation I came up with

    function setTarget(target) {
        let partial = [];
    
        return function permute(input) {
            let i, removed;
            for (i = 0; i < input.length; i++) {
                removed = input.splice(i, 1)[0];
                partial.push(removed);
    
                const sum = partial.reduce((a, b) => a + b)
                if (sum === target) return partial.slice()
                if (sum < target) permute(input)
    
                input.splice(i, 0, removed);
                partial.pop();
            }
            return null
        };
    }
    
    0 讨论(0)
  • 2020-11-21 07:00

    I was doing something similar for a scala assignment. Thought of posting my solution here:

     def countChange(money: Int, coins: List[Int]): Int = {
          def getCount(money: Int, remainingCoins: List[Int]): Int = {
            if(money == 0 ) 1
            else if(money < 0 || remainingCoins.isEmpty) 0
            else
              getCount(money, remainingCoins.tail) +
                getCount(money - remainingCoins.head, remainingCoins)
          }
          if(money == 0 || coins.isEmpty) 0
          else getCount(money, coins)
        }
    
    0 讨论(0)
  • 2020-11-21 07:02

    Recommended as an answer:

    Here's a solution using es2015 generators:

    function* subsetSum(numbers, target, partial = [], partialSum = 0) {
    
      if(partialSum === target) yield partial
    
      if(partialSum >= target) return
    
      for(let i = 0; i < numbers.length; i++){
        const remaining = numbers.slice(i + 1)
            , n = numbers[i]
    
        yield* subsetSum(remaining, target, [...partial, n], partialSum + n)
      }
    
    }
    

    Using generators can actually be very useful because it allows you to pause script execution immediately upon finding a valid subset. This is in contrast to solutions without generators (ie lacking state) which have to iterate through every single subset of numbers

    0 讨论(0)
  • 2020-11-21 07:03

    PHP Version, as inspired by Keith Beller's C# version.

    bala's PHP version did not work for me, because I did not need to group numbers. I wanted a simpler implementation with one target value, and a pool of numbers. This function will also prune any duplicate entries.

    /**
     * Calculates a subset sum: finds out which combinations of numbers
     * from the numbers array can be added together to come to the target
     * number.
     * 
     * Returns an indexed array with arrays of number combinations.
     * 
     * Example: 
     * 
     * <pre>
     * $matches = subset_sum(array(5,10,7,3,20), 25);
     * </pre>
     * 
     * Returns:
     * 
     * <pre>
     * Array
     * (
     *   [0] => Array
     *   (
     *       [0] => 3
     *       [1] => 5
     *       [2] => 7
     *       [3] => 10
     *   )
     *   [1] => Array
     *   (
     *       [0] => 5
     *       [1] => 20
     *   )
     * )
     * </pre>
     * 
     * @param number[] $numbers
     * @param number $target
     * @param array $part
     * @return array[number[]]
     */
    function subset_sum($numbers, $target, $part=null)
    {
        // we assume that an empty $part variable means this
        // is the top level call.
        $toplevel = false;
        if($part === null) {
            $toplevel = true;
            $part = array();
        }
    
        $s = 0;
        foreach($part as $x) 
        {
            $s = $s + $x;
        }
    
        // we have found a match!
        if($s == $target) 
        {
            sort($part); // ensure the numbers are always sorted
            return array(implode('|', $part));
        }
    
        // gone too far, break off
        if($s >= $target) 
        {
            return null;
        }
    
        $matches = array();
        $totalNumbers = count($numbers);
    
        for($i=0; $i < $totalNumbers; $i++) 
        {
            $remaining = array();
            $n = $numbers[$i];
    
            for($j = $i+1; $j < $totalNumbers; $j++) 
            {
                $remaining[] = $numbers[$j];
            }
    
            $part_rec = $part;
            $part_rec[] = $n;
    
            $result = subset_sum($remaining, $target, $part_rec);
            if($result) 
            {
                $matches = array_merge($matches, $result);
            }
        }
    
        if(!$toplevel) 
        {
            return $matches;
        }
    
        // this is the top level function call: we have to
        // prepare the final result value by stripping any
        // duplicate results.
        $matches = array_unique($matches);
        $result = array();
        foreach($matches as $entry) 
        {
            $result[] = explode('|', $entry);
        }
    
        return $result;
    }
    
    0 讨论(0)
  • 2020-11-21 07:06

    C++ version of the same algorithm

    #include <iostream>
    #include <list>
    void subset_sum_recursive(std::list<int> numbers, int target, std::list<int> partial)
    {
            int s = 0;
            for (std::list<int>::const_iterator cit = partial.begin(); cit != partial.end(); cit++)
            {
                s += *cit;
            }
            if(s == target)
            {
                    std::cout << "sum([";
    
                    for (std::list<int>::const_iterator cit = partial.begin(); cit != partial.end(); cit++)
                    {
                        std::cout << *cit << ",";
                    }
                    std::cout << "])=" << target << std::endl;
            }
            if(s >= target)
                return;
            int n;
            for (std::list<int>::const_iterator ai = numbers.begin(); ai != numbers.end(); ai++)
            {
                n = *ai;
                std::list<int> remaining;
                for(std::list<int>::const_iterator aj = ai; aj != numbers.end(); aj++)
                {
                    if(aj == ai)continue;
                    remaining.push_back(*aj);
                }
                std::list<int> partial_rec=partial;
                partial_rec.push_back(n);
                subset_sum_recursive(remaining,target,partial_rec);
    
            }
    }
    
    void subset_sum(std::list<int> numbers,int target)
    {
        subset_sum_recursive(numbers,target,std::list<int>());
    }
    int main()
    {
        std::list<int> a;
        a.push_back (3); a.push_back (9); a.push_back (8);
        a.push_back (4);
        a.push_back (5);
        a.push_back (7);
        a.push_back (10);
        int n = 15;
        //std::cin >> n;
        subset_sum(a, n);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 07:06

    I thought I'd use an answer from this question but I couldn't, so here is my answer. It is using a modified version of an answer in Structure and Interpretation of Computer Programs. I think this is a better recursive solution and should please the purists more.

    My answer is in Scala (and apologies if my Scala sucks, I've just started learning it). The findSumCombinations craziness is to sort and unique the original list for the recursion to prevent dupes.

    def findSumCombinations(target: Int, numbers: List[Int]): Int = {
      cc(target, numbers.distinct.sortWith(_ < _), List())
    }
    
    def cc(target: Int, numbers: List[Int], solution: List[Int]): Int = {
      if (target == 0) {println(solution); 1 }
      else if (target < 0 || numbers.length == 0) 0
      else 
        cc(target, numbers.tail, solution) 
        + cc(target - numbers.head, numbers, numbers.head :: solution)
    }
    

    To use it:

     > findSumCombinations(12345, List(1,5,22,15,0,..))
     * Prints a whole heap of lists that will sum to the target *
    
    0 讨论(0)
提交回复
热议问题