首页 > 其他分享 >LeetCode/最大相等频率

LeetCode/最大相等频率

时间:2022-08-18 00:44:13浏览次数:49  
标签:tmp 频数 相等 nums dict 频率 maxFreq freq LeetCode

给你一个正整数数组 nums,请你帮忙从该数组中找出能满足下面要求的最长前缀,并返回该前缀的长度
从前缀中恰好删除一个元素后,剩下每个数字的出现次数都相同。

1. 双哈希表

一个记录每个值的频数,一个记录每个频数的个数

class Solution {
public:
    int maxEqualFreq(vector<int>& nums) {
        unordered_map<int, int> freq, count;
        int res = 0, maxFreq = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (count[nums[i]] > 0) 
                freq[count[nums[i]]]--;
            count[nums[i]]++;
            maxFreq = max(maxFreq, count[nums[i]]);
            freq[count[nums[i]]]++;
            bool ok = maxFreq == 1 ||
                    freq[maxFreq] * maxFreq + freq[maxFreq - 1] * (maxFreq - 1) == i + 1 && freq[maxFreq] == 1 ||
                    freq[maxFreq] * maxFreq + 1 == i + 1 && freq[1] == 1;
            if (ok) res = i+1;
        }
        return res;
    }
};

2. 数组作哈希表

class Solution {
public:
    int cnt[100007], dict[100007];//双哈希表
    int maxEqualFreq(vector<int>& nums) {
        memset(cnt, 0, sizeof(cnt));
        memset(dict, 0, sizeof(dict));
        int ans = 0, tmp = 0;
        for (int now = 0; auto& i : nums)//从前往后更新
        {
            now++;//当前前缀长度
            if (cnt[i]) dict[cnt[i]]--;//该值频数对应个数减一
            tmp = max(tmp, ++cnt[i]);//记录最大频数,同时更新当前值频数
            dict[cnt[i]]++;//更新后的频数个数加一
            //当前前缀最大频数为1
            //或者频数为1的数只有一个且其它数频数相同
            //或者最大频数的数只有一个且其它数频数相同且只比最大频数少一
            if (tmp == 1 || (dict[1] == 1 && dict[tmp] * tmp + 1 == now) || (dict[tmp] == 1 && tmp + dict[tmp - 1] * (tmp - 1) == now))
                ans = now;//满足条件进行更新前缀
        }
        return ans;
    }
};

标签:tmp,频数,相等,nums,dict,频率,maxFreq,freq,LeetCode
From: https://www.cnblogs.com/929code/p/16597342.html

相关文章

  • leetcode 303. Range Sum Query - Immutable 区域和检索 - 数组不可变(简单)
    一、题目大意https://leetcode.cn/problems/range-sum-query-immutable给定一个整数数组 nums,处理以下类型的多个查询:计算索引 left 和 right (包含left和righ......
  • leetcode45-跳跃游戏 II
    跳跃游戏II前向dp对于一个数i,从0到i-1进行遍历,如果在这个位置能跳跃到i,那么对i的dp值进行更新。这种方式时间复杂度为O(n^2),效率很低classSolution{publici......
  • LeetCode 347 Top K Frequent Elements
    Givenanintegerarraynumsandanintegerk,returnthekmostfrequentelements.Youmayreturntheanswerinanyorder.Solution此题要求时间复杂度低于\(O(......
  • [LeetCode] 1314. Matrix Block Sum 矩阵区域和
    Givena mxn matrix mat andaninteger k,return amatrix answer whereeach answer[i][j] isthesumofallelements mat[r][c] for:i-k<=r<=......
  • leetcode1302-层数最深叶子节点的和
    层数最深叶子节点的和BFS层序遍历树,返回最后一次计算的结果classSolution{publicintdeepestLeavesSum(TreeNoderoot){List<TreeNode>list=new......
  • leetcode91-解码方法
    解码方法dp如果当前位置的字符串不等于0,则表明这个字符可以被解码成新字符,dp[i]+=dp[i-1]如果上一个位置的字符串等于1或者2并且与当前字符拼接的数字小于等于26,则......
  • leetcode87-扰乱字符串
    扰乱字符串dpdp需要记录s1和s2的起始位置和长度,所以是一个三维dp。dp[i1][i2][len]表示s1从i1位置开始,s2从i2位置开始,长度为len的两个字符串是否和谐。分为以下几种情......
  • LeetCode 127 Word Ladder
    AtransformationsequencefromwordbeginWordtowordendWordusingadictionarywordListisasequenceofwordsbeginWord->s1->s2->...->sksuchthat:......
  • leetcode 热题100刷题-两数之和
    题题号:1题目:两数之和难度:简单链接:https://leetcode.cn/problems/two-sum/2022/08/16答案算法思路从第i个数字开始,之后的每个数字都与第i个数字相加,判断是否与目标值......
  • LeetCode 螺旋矩阵 II 算法题解 All In One
    LeetCode螺旋矩阵II算法题解AllInOnejs/ts生成螺旋矩阵螺旋矩阵原理图解动态赋值arr[i]//动态更新indexleti=0;while(left<=right&&t......