-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCombination.java
More file actions
184 lines (163 loc) · 5.7 KB
/
Combination.java
File metadata and controls
184 lines (163 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.*;
/*
使用回溯生成组合 (Java Implementation)
算法说明:
- 生成从1到n中选取k个元素的所有组合 (C(n,k))
- 使用回溯系统地探索所有选择
- At each step, only consider elements >= current start position
- This prevents duplicate combinations
- When we've selected k elements, add to result
时间复杂度:O(C(n,k) * k),其中C(n,k)是组合数
空间复杂度:O(k),用于递归深度和当前组合
示例:
combine(4, 2) 返回 [[1,2], [1,3], [1,4], [2,3], [2,4], [3,4]]
*/
public class Combination {
/*
*
* 生成从1到n中选取k个元素的所有组合.
*
* @param n 元素的总数(1到n)
*
* @param k 每个组合的大小
*
* @return List of all combinations
*/
public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), 1, n, k);
return result;
}
/*
*
* Helper method for backtracking.
*
* @param result List to store all combinations
*
* @param current 当前要构建的组合
*
* @param start Starting number to consider
*
* @param n Total number of elements
*
* @param k Target combination size
*/
private static void backtrack(List<List<Integer>> result,
List<Integer> current,
int start,
int n,
int k) {
// 基本情况:组合已完成
if (current.size() == k) {
result.add(new ArrayList<>(current));
return;
}
// 尝试从start到n的每个数字
for (int num = start; num <= n; num++) {
// 选择:将num添加到当前组合
current.add(num);
// 探索:构建组合的其余部分
// 仅数字 >= num+1 以避免重复
backtrack(result, current, num + 1, n, k);
// 取消选择:移除num以进行回溯
current.remove(current.size() - 1);
}
}
/*
*
* Optimized combination with early termination.
* 剪枝不可能产生k个元素的分支.
*
* Time Complexity: O(C(n,k) * k)
* Space Complexity: O(k)
*/
public static List<List<Integer>> combineOptimized(int n, int k) {
List<List<Integer>> result = new ArrayList<>();
backtrackOptimized(result, new ArrayList<>(), 1, n, k);
return result;
}
/*
*
* Helper method for optimized backtracking with pruning.
*/
private static void backtrackOptimized(List<List<Integer>> result,
List<Integer> current,
int start,
int n,
int k) {
// 基本情况:组合已完成
if (current.size() == k) {
result.add(new ArrayList<>(current));
return;
}
// 优化:提前终止
// Remaining slots needed: k - current.size()
// 可用的元素:n - start + 1
// 如果没有足够的可用元素,则停止
int remaining = k - current.size();
int available = n - start + 1;
if (available < remaining) {
return;
}
// 尝试从start到n的每个数字
for (int num = start; num <= n; num++) {
current.add(num);
backtrackOptimized(result, current, num + 1, n, k);
current.remove(current.size() - 1);
}
}
/*
*
* Test cases for combination generation
*/
public static void main(String[] args) {
System.out.println("=== Combination Backtracking Test Cases ===\n");
// 测试用例 1: combine(4, 2)
System.out.println("Test 1: combine(4, 2)");
List<List<Integer>> result1 = combine(4, 2);
System.out.println("Result (count=" + result1.size() + "):");
for (List<Integer> combo : result1) {
System.out.println(" " + combo);
}
System.out.println();
// 测试用例 2: combine(3, 1)
System.out.println("Test 2: combine(3, 1)");
List<List<Integer>> result2 = combine(3, 1);
System.out.println("Result (count=" + result2.size() + "):");
for (List<Integer> combo : result2) {
System.out.println(" " + combo);
}
System.out.println();
// 测试用例 3: combine(3, 3)
System.out.println("Test 3: combine(3, 3)");
List<List<Integer>> result3 = combine(3, 3);
System.out.println("Result (count=" + result3.size() + "):");
for (List<Integer> combo : result3) {
System.out.println(" " + combo);
}
System.out.println();
// 测试用例 4: combine(5, 3)
System.out.println("Test 4: combine(5, 3)");
List<List<Integer>> result4 = combine(5, 3);
System.out.println("Result (count=" + result4.size() + "):");
for (List<Integer> combo : result4) {
System.out.println(" " + combo);
}
System.out.println();
// 测试用例 5: combineOptimized(6, 2)
System.out.println("Test 5: combineOptimized(6, 2)");
List<List<Integer>> result5 = combineOptimized(6, 2);
System.out.println("Result (count=" + result5.size() + "):");
for (List<Integer> combo : result5) {
System.out.println(" " + combo);
}
System.out.println();
// 测试用例 6: combineOptimized(5, 4)
System.out.println("Test 6: combineOptimized(5, 4)");
List<List<Integer>> result6 = combineOptimized(5, 4);
System.out.println("Result (count=" + result6.size() + "):");
for (List<Integer> combo : result6) {
System.out.println(" " + combo);
}
}
}