forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
71 lines (66 loc) · 2.33 KB
/
Solution.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public IList<IList<int>> CombinationSum2(int[] candidates, int target)
{
var dict = new SortedDictionary<int, int>(candidates.GroupBy(c => c).ToDictionary(g => g.Key, g => g.Count()));
var paths = new List<Tuple<int, int>>[target + 1];
paths[0] = new List<Tuple<int, int>>();
foreach (var pair in dict)
{
for (var j = target; j >= 0; --j)
{
for (var k = 1; k <= pair.Value && j - pair.Key * k >= 0; ++k)
{
if (paths[j - pair.Key * k] != null)
{
if (paths[j] == null)
{
paths[j] = new List<Tuple<int, int>>();
}
paths[j].Add(Tuple.Create(pair.Key, k));
}
}
}
}
var results = new List<IList<int>>();
if (paths[target] != null) GenerateResults(results, new Stack<int>(), paths, target, paths[target].Count - 1);
return results;
}
private void GenerateResults(IList<IList<int>> results, Stack<int> result, List<Tuple<int, int>>[] paths, int remaining,
int maxIndex)
{
if (remaining == 0)
{
results.Add(new List<int>(result));
return;
}
for (var i = maxIndex; i >= 0; --i)
{
var path = paths[remaining][i];
for (var j = 0; j < path.Item2; ++j)
{
result.Push(path.Item1);
}
var nextMaxIndex = paths[remaining - path.Item1 * path.Item2].BinarySearch(Tuple.Create(path.Item1, int.MinValue), Comparer.Instance);
nextMaxIndex = ~nextMaxIndex - 1;
GenerateResults(results, result, paths, remaining - path.Item1 * path.Item2, nextMaxIndex);
for (var j = 0; j < path.Item2; ++j)
{
result.Pop();
}
}
}
}
class Comparer : IComparer<Tuple<int, int>>
{
public int Compare(Tuple<int, int> x, Tuple<int, int> y)
{
if (x.Item1 < y.Item1) return -1;
if (x.Item1 > y.Item1) return 1;
return x.Item2.CompareTo(y.Item2);
}
public static Comparer Instance = new Comparer();
}