/** * File: subset_sum_ii.cs * Created Time: 2023-06-25 * Author: hpstory (hpstory1024@163.com) */ namespace hello_algo.chapter_backtracking; public class subset_sum_ii { /* Backtracking: subset sum II */ void Backtrack(List state, int target, int[] choices, int start, List> res) { // Record a solution when the subset reaches the original target (remaining target is 0) if (target == 0) { res.Add(new List(state)); return; } // Iterate over all choices // Pruning 2: iterate from start to avoid generating duplicate subsets // Pruning 3: start after the previously chosen index to avoid choosing the same element again for (int i = start; i < choices.Length; i++) { // Pruning 1: exit the loop if this choice would exceed the remaining target // The array is sorted, so later elements are at least as large and would also exceed the remaining target if (target - choices[i] < 0) { break; } // Pruning 4: if this element equals its left neighbor at the same search depth, the branch is a duplicate; skip it if (i > start && choices[i] == choices[i - 1]) { continue; } // Try: make a choice and update target and start state.Add(choices[i]); // Proceed to the next choice Backtrack(state, target - choices[i], choices, i + 1, res); // Backtrack: undo the choice and restore the previous state state.RemoveAt(state.Count - 1); } } /* Solve subset sum II */ List> SubsetSumII(int[] nums, int target) { List state = []; // State (subset) Array.Sort(nums); // Sort nums int start = 0; // Starting index for iteration List> res = []; // Result list (list of subsets) Backtrack(state, target, nums, start, res); return res; } [Test] public void Test() { int[] nums = [4, 4, 5]; int target = 9; List> res = SubsetSumII(nums, target); Console.WriteLine("Input array nums = " + string.Join(", ", nums) + ", target = " + target); Console.WriteLine("All subsets with sum " + target + " : res = "); foreach (var subset in res) { PrintUtil.PrintList(subset); } } }