本文主要是介绍1838.最高频元素的频数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我的双指针法:
class Solution:def maxFrequency(self, nums:list, k: int) -> int:nums.sort()print(nums)length = len(nums)if length == 1:return 1maxn = 0pleft,pright = 0, 0 # 左右指针curn = 0while pright<length:curNum = nums[pright]curSum = sum(nums[pleft:pright+1]) + kif curSum < curNum*(pright+1-pleft):pleft += 1curn = 0else:curn += 1print(pleft, pright, curn)pright += 1if curn > maxn:maxn = curnreturn maxnif __name__ == '__main__':nums = [9930,9923,9983,9997,9934,9952,9945,9914,9985,9982,9970,9932,9985,9902,9975,9990,9922,9990,9994,9937,9996,9964,9943,9963,9911,9925,9935,9945,9933,9916,9930,9938,10000,9916,9911,9959,9957,9907,9913,9916,9993,9930,9975,9924,9988,9923,9910,9925,9977,9981,9927,9930,9927,9925,9923,9904,9928,9928,9986,9903,9985,9954,9938,9911,9952,9974,9926,9920,9972,9983,9973,9917,9995,9973,9977,9947,9936,9975,9954,9932,9964,9972,9935,9946,9966,]k=3056print("len",len(nums))s = Solution()ans = s.maxFrequency(nums,k)print(ans)"""
3,6,9
"""
有个疑问,这个测试用例是不是有问题啊
[9930,9923,9983,9997,9934,9952,9945,9914,9985,9982,9970,9932,9985,9902,9975,9990,9922,9990,9994,9937,9996,9964,9943,9963,9911,9925,9935,9945,9933,9916,9930,9938,10000,9916,9911,9959,9957,9907,9913,9916,9993,9930,9975,9924,9988,9923,9910,9925,9977,9981,9927,9930,9927,9925,9923,9904,9928,9928,9986,9903,9985,9954,9938,9911,9952,9974,9926,9920,9972,9983,9973,9917,9995,9973,9977,9947,9936,9975,9954,9932,9964,9972,9935,9946,9966]3056
用这个测试用例的结果是72,结果是73。我私下里调试了一下
s = [9902, 9903, 9904, 9907, 9910, 9911, 9911, 9911, 9913, 9914, 9916, 9916, 9916, 9917, 9920, 9922, 9923, 9923, 9923, 9924, 9925, 9925, 9925, 9926, 9927, 9927, 9928, 9928, 9930, 9930, 9930, 9930, 9932, 9932, 9933, 9934, 9935, 9935, 9936, 9937, 9938, 9938, 9943, 9945, 9945, 9946, 9947, 9952, 9952, 9954, 9954, 9957, 9959, 9963, 9964, 9964, 9966, 9970, 9972, 9972, 9973, 9973, 9974, 9975, 9975, 9975, 9977, 9977, 9981, 9982, 9983, 9983, 9985, 9985, 9985, 9986, 9988, 9990, 9990, 9993, 9994, 9995, 9996, 9997, 10000]print(s[72]) # 9985print(sum(s[0:73])+3056)
print(9985*73)
结果是:
9985
728761
728905
这不是前面73个数相加,再加上补的数,是小于73个最大数的吗,怎么会有73个频数呢?请看到的大佬指点一下。
================2021/07/20更新============================================
今天重新修改了一下代码,重新提交通过了。用了前缀和计算[left:right]之间的和,问题可能出现在更新curn上。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/7/20 9:29
# @Author : wutiandeclass Solution:def maxFrequency(self, nums:list, k: int) -> int:nums.sort()print(nums)length = len(nums)if length == 1:return 1""" 计算前缀和 """s = [0 for _ in range(length)]s[0] = nums[0]for i in range(1,length):s[i] = s[i-1]+nums[i]left,right = 0,0maxn = 0while right < length:curn = right-left+1"""计算[left:right]之间的和lrsum"""if left > 0:lrsum = s[right]-s[left-1]else:lrsum = s[right]if lrsum+k>=curn*nums[right]: # 如果符合条件right +=1 # right右指if curn>maxn: # 如果right可以右指,说明可以更新maxn值maxn = curnelse:left +=1 # left右指return maxnif __name__ == '__main__':nums = [9930,9923,9983,9997,9934,9952,9945,9914,9985,9982,9970,9932,9985,9902,9975,9990,9922,9990,9994,9937,9996,9964,9943,9963,9911,9925,9935,9945,9933,9916,9930,9938,10000,9916,9911,9959,9957,9907,9913,9916,9993,9930,9975,9924,9988,9923,9910,9925,9977,9981,9927,9930,9927,9925,9923,9904,9928,9928,9986,9903,9985,9954,9938,9911,9952,9974,9926,9920,9972,9983,9973,9917,9995,9973,9977,9947,9936,9975,9954,9932,9964,9972,9935,9946,9966]k = 3056s = Solution()ans = s.maxFrequency(nums,k)print(ans)
执行结果:
通过
显示详情
添加备注执行用时:
540 ms
, 在所有 Python3 提交中击败了
45.97%
的用户
内存消耗:
26.1 MB
, 在所有 Python3 提交中击败了
6.68%
的用户
这篇关于1838.最高频元素的频数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!