[Likou] 216. Combination Sum III <Backtracking, Backtracking Pruning>

【Rich Button】216. Combination Sum III

Find all combinations of k numbers that add up to n such that the following conditions are met:
only the digits 1 through 9 are used, each digit is used at most once, and a list of all possible valid combinations is returned. The list cannot contain the same combination twice, and the combinations may be returned in any order.

Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There is no other matching combination.

Example 2:
Input: k = 3, n = 9
Output: [[1,2,6], [1,3,5], [2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There is no other matching combination.

Example 3:
Input: k = 4, n = 1
Output: []
Explanation: No valid combination exists.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10, since 10 > 1, there is no valid combination.

提示:
2 <= k <= 9
1 <= n <= 60

answer

backtrace:
insert image description here

import java.util.*;

class Solution {
    
    
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
    
    
        backTracking(n, k, 1, 0);
        return result;
    }

    private void backTracking(int targetSum, int k, int startIndex, int sum) {
    
    

        if (path.size() == k) {
    
    
            if (sum == targetSum) {
    
    
                result.add(new ArrayList<>(path));
            }
            return;
        }

        for (int i = startIndex; i <= 9 ; i++) {
    
    
            path.add(i);
            sum += i;
            backTracking(targetSum, k, i + 1, sum);
            //回溯
            path.removeLast();
            //回溯
            sum -= i;
        }
    }
}

pruning

  • If Sum is greater than TargetSum, there is no need to backtrack.
  • In terms of quantity, if k numbers are required, there is no need to backtrack if there are not enough k numbers. k - path.size()How many numbers are needed in the current layer?
    insert image description here
import java.util.*;

class Solution {
    
    
    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
    
    
        backTracking(n, k, 1, 0);
        return result;
    }

    private void backTracking(int targetSum, int k, int startIndex, int sum) {
    
    
        // 减枝
        if (sum > targetSum) {
    
    
            return;
        }

        if (path.size() == k) {
    
    
            if (sum == targetSum) {
    
    
                result.add(new ArrayList<>(path));
            }
            return;
        }

        // 减枝 9 - (k - path.size()) + 1
        for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
    
    
            path.add(i);
            sum += i;
            backTracking(targetSum, k, i + 1, sum);
            //回溯
            path.removeLast();
            //回溯
            sum -= i;
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132499644