We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9238d51 commit f438244Copy full SHA for f438244
readuce_array_size_to_half.cpp reduce_array_size_to_half.cppreaduce_array_size_to_half.cpp renamed to reduce_array_size_to_half.cpp
@@ -33,3 +33,32 @@ class Solution {
33
return -1;
34
}
35
};
36
+
37
+//since I dont care about the actual values in array, it doesnt make sense to insert them in PQ.
38
+class Solution {
39
+public:
40
+ int minSetSize(vector<int>& arr) {
41
42
+ unordered_map<int, int> count;
43
+ for(auto ele : arr) {
44
+ count[ele]++;
45
+ }
46
47
+ priority_queue<int> Q;
48
+ for(auto &[k,v] : count) {
49
+ Q.emplace(v);
50
51
52
+ int acceptable = arr.size() / 2; //k
53
+ int currSize = 0, setSize = 0;
54
+ while(!Q.empty()) {
55
+ int frequency = Q.top();
56
+ setSize++;
57
+ currSize += frequency;
58
+ if(currSize >= acceptable)
59
+ return setSize;
60
+ Q.pop();
61
62
+ return -1;
63
64
+};
0 commit comments