LeetCode //C - 1224. Maximum Equal Frequency

1224. Maximum Equal Frequency

Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences.

If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0).

Example 1:

Input: nums = 2,2,1,1,5,3,3,5

Output: 7

Explanation: For the subarray 2,2,1,1,5,3,3 of length 7, if we remove nums4 = 5, we will get 2,2,1,1,3,3, so that each number will appear exactly twice.

Example 2:

Input: nums = 1,1,1,2,2,2,3,3,3,4,4,4,5

Output: 13

Constraints:
  • 2 < = n u m s . l e n g t h < = 10 5 2 <= nums.length <= 10^5 2<=nums.length<=105
  • 1 < = n u m s i < = 10 5 1 <= numsi <= 10^5 1<=numsi<=105

From: LeetCode

Link: 1224. Maximum Equal Frequency


Solution:

Ideas:

We can solve this in one pass by tracking two things: each value's frequency, and how many values currently have each frequency. The key is checking when the current prefix can be fixed by deleting exactly one number.

Code:
c 复制代码
int maxEqualFreq(int* nums, int numsSize) {
    int count[100001] = {0};   // count[x] = frequency of number x
    int freq[100002] = {0};    // freq[f] = how many numbers appear f times

    int ans = 0;
    int maxFreq = 0;

    for (int i = 0; i < numsSize; i++) {
        int x = nums[i];

        if (count[x] > 0) {
            freq[count[x]]--;
        }

        count[x]++;
        freq[count[x]]++;

        if (count[x] > maxFreq) {
            maxFreq = count[x];
        }

        int len = i + 1;

        /*
            Valid cases:

            1. maxFreq == 1
               Every number appears once.
               Remove any one element.

            2. One number appears maxFreq times,
               all others appear maxFreq - 1 times.
               Remove one occurrence from that number.

            3. One number appears once,
               all others appear maxFreq times.
               Remove that single-occurrence number.
        */
        if (maxFreq == 1 ||
            freq[maxFreq] == 1 &&
            freq[maxFreq] * maxFreq + freq[maxFreq - 1] * (maxFreq - 1) == len ||
            freq[1] == 1 &&
            freq[maxFreq] * maxFreq + 1 == len) {
            ans = len;
        }
    }

    return ans;
}
相关推荐
lisw0510 小时前
稀疏算法概述!
算法·机器人·无人机
YLCHUP12 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴13 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
FlightYe15 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
geovindu17 小时前
python:Logistic Regression
开发语言·后端·python·算法
番茄味城市甜菜18 小时前
二叉树刷题(C语言)
c语言·开发语言
geovindu18 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
.道阻且长.19 小时前
Linux基础IO:理解文件以及回顾C文件接口
linux·运维·服务器·c语言·开发语言·职场和发展
derek199719 小时前
软考软件设计师:主键、传递依赖和冗余,三问其实是一件事
c语言·c++·计算机网络·软件工程·学习方法
凌冰_19 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程