力扣1838.最高频的元素的频数

力扣1838.最高频的元素的频数

  • 首先排序 然后右指针遍历补成的数

    • 每次加**差值(num[i] - num[i-1]) * 位数(i - j)
cpp 复制代码
 class Solution {
 public:
     int maxFrequency(vector<int>& nums, int k) {
         int res=1,n = nums.size();
         sort(nums.begin(),nums.end());
         long long ans=0;
         for(int i=1,j=0;i<n;i++)
         {
             ans += (long long)(nums[i] - nums[i-1])*(i-j);
             while(ans > k)
             {
                 ans -= (nums[i] - nums[j]);
                 j ++;
             }
             res = max(res,i-j+1);
         }
         return res;
     }
 };
相关推荐
CN-Dust1 天前
【C++】while语句例题专题
数据结构·c++·算法
灵智实验室1 天前
PX4位置速度估计技术详解(四):LPE 激光雷达高度融合的实现错误
算法·无人机·px 4
CQU_JIAKE1 天前
【A】3742,3387,并查集
算法
gihigo19981 天前
CHAN时延估计算法(二维/三维定位实现)
算法
freexyn1 天前
Matlab自学笔记七十六:表达式的展开、因式分解、化简、合并同类项
笔记·算法·matlab
样例过了就是过了1 天前
LeetCode热题 不同路径
c++·算法·leetcode·动态规划
dog2501 天前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
Wadli1 天前
27.单调队列
算法
Navigator_Z1 天前
LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays
c语言·算法·leetcode
Wect1 天前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·typescript