Skip to content

Commit f438244

Browse files
Update and rename readuce_array_size_to_half.cpp to reduce_array_size_to_half.cpp
1 parent 9238d51 commit f438244

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,32 @@ class Solution {
3333
return -1;
3434
}
3535
};
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

Comments
 (0)