[leetcode] 5225. Maximum Equal Frequency
Posted fish1996
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] 5225. Maximum Equal Frequency相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/maximum-equal-frequency/
class Solution { public: int maxEqualFreq(vector<int>& nums) { unordered_map<int, int> count; unordered_map<int, int> frequency; int n = nums.size(); int res = 0; for (int i = 0;i < n; i++) { int old_count = count[nums[i]]; if (frequency.find(old_count) != frequency.end()) { frequency[old_count]--; if (frequency[old_count] == 0) { frequency.erase(old_count); } } count[nums[i]]++; frequency[count[nums[i]]]++; if(frequency.size() == 1) { int num; int time; for (auto& data : frequency) { num = data.first; time = data.second; } if(num == 1 || time == 1) { res = max(res, i + 1); } } else if (frequency.size() == 2) { int max_num = INT_MIN; int min_num = INT_MAX; int max_time; int min_time; for (auto& data : frequency) { if (data.first > max_num) { max_time = data.second; } if (data.first < min_num) { min_time = data.second; } max_num = max(data.first, max_num); min_num = min(data.first, min_num); } // cout << min_num << " " << min_time << " " << max_num << " " << max_time << endl; if (max_num == min_num + 1 && max_time == 1) { res = max(res, i + 1); } if(min_num == 1 && min_time == 1) { res = max(res, i + 1); } } } return res; } };
以上是关于[leetcode] 5225. Maximum Equal Frequency的主要内容,如果未能解决你的问题,请参考以下文章
#Leetcode# 53. Maximum Subarray