-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
45 lines (42 loc) · 1.05 KB
/
Solution.cs
File metadata and controls
45 lines (42 loc) · 1.05 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
public class Solution
{
public int[] FindXSum(int[] nums, int k, int x)
{
int n = nums.Length;
int[] ans = new int[n - k + 1];
for (int i = 0; i < n - k + 1; i++)
{
ans[i] = Calc(nums, i, x, k);
}
return ans;
}
int Calc(int[] nums, int pos, int x, int k)
{
int[] count = new int[100];
for (int i = pos; i < pos + k; i++)
{
count[nums[i]]++;
}
PriorityQueue<int, (int, int)> pq = new(Comparer<(int, int)>.Create((a, b) =>
{
if (a.Item1 == b.Item1)
{
return b.Item2 - a.Item2;
}
return b.Item1 - a.Item1;
}));
for (int i = 0; i < 100; i++)
{
if (count[i] > 0) pq.Enqueue(i, (count[i], i));
}
int ans = 0;
int loop = Math.Min(x, k);
while (pq.Count > 0 && loop > 0)
{
int num = pq.Dequeue();
ans += num * count[num];
loop--;
}
return ans;
}
}