Skip to content

Commit d667226

Browse files
committed
leetcode
1 parent 4b9fa0e commit d667226

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

Diff for: CombinationSumIV/combination_sum_iv.dart

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
3+
-* 377. Combination Sum IV *-
4+
5+
Given an array of distinct integers nums and a target integer target, return the number of possible combinations that add up to target.
6+
7+
The test cases are generated so that the answer can fit in a 32-bit integer.
8+
9+
10+
11+
Example 1:
12+
13+
Input: nums = [1,2,3], target = 4
14+
Output: 7
15+
Explanation:
16+
The possible combination ways are:
17+
(1, 1, 1, 1)
18+
(1, 1, 2)
19+
(1, 2, 1)
20+
(1, 3)
21+
(2, 1, 1)
22+
(2, 2)
23+
(3, 1)
24+
Note that different sequences are counted as different combinations.
25+
Example 2:
26+
27+
Input: nums = [9], target = 3
28+
Output: 0
29+
30+
31+
Constraints:
32+
33+
1 <= nums.length <= 200
34+
1 <= nums[i] <= 1000
35+
All the elements of nums are unique.
36+
1 <= target <= 1000
37+
38+
39+
Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
40+
41+
42+
*/
43+
44+
class Solution {
45+
int combinationSum4(List<int> nums, int target) {
46+
final List<int> dp = List.filled(nums.length, target + 1);
47+
dp[0] = 1;
48+
49+
for (int i = 1; i <= target; i++) {
50+
for (int number in nums) {
51+
if (i - number >= 0) {
52+
dp[i] += dp[i - number];
53+
}
54+
}
55+
}
56+
57+
return dp[target];
58+
}
59+
}

Diff for: CombinationSumIV/combination_sum_iv.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "sort"
4+
5+
func helper(nums []int, n int, memo map[int]int) int {
6+
if val, found := memo[n]; found {
7+
return val
8+
}
9+
if n == 0 {
10+
return 1
11+
}
12+
if n < nums[0] {
13+
return 0
14+
}
15+
16+
count := 0
17+
for _, num := range nums {
18+
if n-num < 0 {
19+
break
20+
}
21+
count += helper(nums, n-num, memo)
22+
}
23+
24+
memo[n] = count
25+
return count
26+
}
27+
28+
func combinationSum4(nums []int, target int) int {
29+
sort.Ints(nums)
30+
memo := make(map[int]int)
31+
return helper(nums, target, memo)
32+
}

Diff for: CombinationSumIV/combination_sum_iv.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
3+
This code is a Go implementation of a dynamic programming approach to solve the "Combination Sum IV" problem. The problem statement is to count the number of possible combinations of the given `nums` array that add up to a specific `target` value. Combinations are counted without considering the order of elements in the combination.
4+
5+
Here's a step-by-step explanation of the code:
6+
7+
1. Import the "sort" package, which is used later to sort the `nums` array.
8+
2. Define a helper function called `helper`. This function takes three parameters: `nums` (the input array of numbers), `n` (the target value to reach), and `memo` (a memoization map to store computed results for subproblems).
9+
10+
3. Check if the result for the current target `n` is already computed and stored in the `memo` map. If so, return the stored value to avoid redundant calculations.
11+
12+
4. If `n` equals 0, it means a valid combination is found, so return 1 to count it.
13+
14+
5. If `n` is less than the smallest number in the `nums` array, return 0, as it's impossible to form the target value with the given numbers.
15+
16+
6. Initialize a `count` variable to 0, which will be used to count the valid combinations.
17+
18+
7. Iterate through the `nums` array, and for each number `num`, recursively call the `helper` function with the updated target `n - num`. Add the result to the `count` variable. This step calculates the number of combinations that include the current number.
19+
20+
8. Store the `count` in the `memo` map for the current `n` to avoid recomputation.
21+
22+
9. Return the final `count` as the result.
23+
24+
10. Define the `combinationSum4` function, which is the entry point for solving the problem. Inside this function:
25+
- Sort the `nums` array in ascending order to optimize the algorithm.
26+
- Create an empty memoization map `memo`.
27+
- Call the `helper` function with `nums`, `target`, and `memo` as arguments and return the result.
28+
29+
The time complexity of this solution depends on the input values. In the worst case, it can be exponential, but memoization is used to store previously computed results, which can significantly reduce redundant calculations. Therefore, the average time complexity is much better than the worst case, and it can be considered O(N * T), where N is the length of the `nums` array, and T is the target value.
30+
31+
The space complexity is O(T), where T is the target value, due to the space required for the `memo` map.

Diff for: FindCriticalAndPseudoCriticalEdgesInMinimumSpanningTree/find_critical_and_pseudocritical_edges_in_minimum_spanning_tree.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ class Solution {
144144
final List<bool> mstEdgeSet,
145145
final List<List<int>> minimumSpanningTree,
146146
final List<List<List<int>>> graph) {
147-
int weight = 0;
148147
final DisjointSet ds = DisjointSet(n);
148+
int weight=0;
149149

150150
for (int i = 0; i < edges.length; i++) {
151151
if (ds.union(edges[i][0], edges[i][1])) {

0 commit comments

Comments
 (0)