-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
75 lines (67 loc) · 2.04 KB
/
Solution.cs
File metadata and controls
75 lines (67 loc) · 2.04 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
public class Solution
{
public int MaxScore(IList<IList<int>> grid)
{
var hashSet = new HashSet<int>();
foreach (var rows in grid)
{
foreach (var num in rows)
{
hashSet.Add(num);
}
}
var storage = new Dictionary<int, List<int>>();
foreach (var val in hashSet)
{
storage[val] = [];
}
for (int i = 0; i < grid.Count; i++)
{
for (int j = 0; j < grid[i].Count; j++)
{
var val = grid[i][j];
storage[val].Add(i);
}
}
var sortedList = new List<int>();
foreach (var val in hashSet)
{
sortedList.Add(val);
}
sortedList.Sort((a, b) => b - a);
var maxValueRecords = new int[1025][];
for (var i = 0; i < 1025; i++)
{
maxValueRecords[i] = new int[101];
Array.Fill(maxValueRecords[i], 0);
}
// foreach (var key in storage.Keys)
// {
// Console.WriteLine($"{key}: {string.Join(", ", storage[key])}");
// }
// Console.WriteLine($"SortedList: {string.Join(", ", sortedList)}");
return Solve(0, 0, sortedList, storage, maxValueRecords);
}
private int Solve(
int index,
int mask,
List<int> sortedList,
Dictionary<int, List<int>> storage,
int[][] maxValueRecords)
{
if (index >= sortedList.Count) return 0;
if (maxValueRecords[mask][index] != 0) return maxValueRecords[mask][index];
int ans = 0;
int val = sortedList[index];
foreach (var i in storage[val])
{
if ((mask & (1 << i)) == 0)
{
ans = Math.Max(ans, val + Solve(index + 1, mask | (1 << i), sortedList, storage, maxValueRecords));
}
}
ans = Math.Max(ans, Solve(index + 1, mask, sortedList, storage, maxValueRecords));
maxValueRecords[mask][index] = ans;
return ans;
}
}