forked from LeetCode-in-Dart/LeetCode-in-Dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.dart
31 lines (27 loc) · 1.03 KB
/
Solution.dart
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
// #Medium #Top_100_Liked_Questions #Array #Backtracking #Algorithm_II_Day_10_Recursion_Backtracking
// #Level_2_Day_20_Brute_Force/Backtracking #Udemy_Backtracking/Recursion
// #Big_O_Time_O(2^n)_Space_O(n+2^n) #2024_10_04_Time_316_ms_(96.88%)_Space_150_MB_(62.50%)
class Solution {
List<List<int>> combinationSum(List<int> coins, int amount) {
List<List<int>> ans = [];
List<int> subList = [];
combinationSumRec(coins.length, coins, amount, subList, ans);
return ans;
}
void combinationSumRec(int n, List<int> coins, int amount, List<int> subList, List<List<int>> ans) {
if (amount == 0 || n == 0) {
if (amount == 0) {
// Create a new list from subList and add to ans
ans.add(List.from(subList));
}
return;
}
if (amount - coins[n - 1] >= 0) {
subList.add(coins[n - 1]);
combinationSumRec(n, coins, amount - coins[n - 1], subList, ans);
// Remove the last element
subList.removeLast();
}
combinationSumRec(n - 1, coins, amount, subList, ans);
}
}