diff options
Diffstat (limited to 'other/reduce_array_size_half.cpp')
-rw-r--r-- | other/reduce_array_size_half.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/other/reduce_array_size_half.cpp b/other/reduce_array_size_half.cpp new file mode 100644 index 0000000..fd922dc --- /dev/null +++ b/other/reduce_array_size_half.cpp @@ -0,0 +1,30 @@ +#include "leetcode.h" + +class Solution { +public: + int minSetSize(vector<int> &arr) { + int ans = 0, n = arr.size(), n1 = 0; + vector<int> j; + unordered_map<int, int> um; + for (int i = 0; i < n; i++) + um[arr[i]]++; + for (auto x : um) + j.push_back(x.second); + if (j.size() == 1) + return 1; + sort(j.begin(), j.end()); + for (int i = j.size() - 1; i >= 0; i--) { + if (ans >= n / 2) + return n1; + n1++; + ans = ans + j[i]; + } + return 0; + } +}; + +int main() { + Solution obj; + vector<int> arr = {3, 3, 3, 3, 5, 5, 5, 2, 2, 7}; + cout << obj.minSetSize(arr); +} |