-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1338-ReduceArraySizeToTheHalf.cs
38 lines (33 loc) · 1.04 KB
/
1338-ReduceArraySizeToTheHalf.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
//-----------------------------------------------------------------------------
// Runtime: 332ms
// Memory Usage: 46.5 MB
// Link: https://leetcode.com/submissions/detail/363480535/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _1338_ReduceArraySizeToTheHalf
{
public int MinSetSize(int[] arr)
{
var counts = new Dictionary<int, int>();
foreach (var num in arr)
{
if (!counts.ContainsKey(num))
counts[num] = 1;
else
counts[num]++;
}
var countArr = counts.Values.OrderByDescending(num => num).ToArray();
int sum = 0, half = arr.Length / 2, result = 0;
foreach (var count in countArr)
{
sum += count;
result++;
if (sum >= half) return result;
}
return -1;
}
}
}