leetcode-158周赛-5225-最大相等频率
题目描述:
方法:
class Solution(object):
def maxEqualFreq(self, A):
count = collections.Counter()
freqs = collections.Counter()
ans = 1
for i, x in enumerate(A):
count[x] += 1
c = count[x]
freqs[c] += 1
if c-1 > 0: freqs[c-1] -= 1
if freqs[c-1] == 0:
del freqs[c-1]
L = len(freqs)
#print freqs
if L <= 2:
if L <= 1:
k, = freqs.keys()
fk = freqs[k]
#print '.', i+1, ';', k, freqs[k]
if k == 1 or fk == 1:
ans = i + 1
else:
a, b = freqs.keys()
fa, fb = freqs[a], freqs[b]
# remove a
#print i+1,a,fa,';',b,fb
if fa-1 == 0 and a-1 == b:
ans = i + 1
if fb-1 == 0 and b-1 == a:
ans = i + 1
if a == 1 and fa == 1 or b == 1 and fb == 1:
ans = i + 1
return ans
另:倒推
from collections import Counter
class Solution(object):
def maxEqualFreq(self, nums):
cnt = Counter(nums)
l = len(cnt)
s = n = len(nums)
if n==1:
return 1
for i in range(n-1,0,-1):
if (s-1) % l == 0:
k = (s-1)//l
if all(x==k or x==k+1 for x in cnt.values()):
return i+1
if l!=1 and (s-1) % (l-1) == 0:
k = (s-1)//(l-1)
if all(x==k or x==1 for x in cnt.values()):
return i+1
num = nums[i]
s-=1
cnt[num]-=1
if cnt[num]==0:
del cnt[num]
l-=1
return 1
leetcode-158周赛-5225-最大相等频率的更多相关文章
- [LeetCode] 895. Maximum Frequency Stack 最大频率栈
Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...
- 【LeetCode】451-根据字符出现频率排序
题目描述 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和' ...
- leetcode.排序.451根据字符出现频率排序-Java
1. 具体题目 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r ...
- ✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- leetcode 双周赛9 进击的骑士
一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里. 骑士的走法和中国象棋中的马相似,走 “日” 字:即先向左(或右 ...
- leetcode 双周赛9 找出所有行中最小公共元素
给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,] ...
- [每日一题2020.06.16] leetcode双周赛T3 5423 找两个和为目标值且不重叠的子数组 DP, 前缀和
题目链接 给你一个整数数组 arr 和一个整数值 target . 请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target .可能会有多种方案,请你返回满足要求的两个子数组长度和的 ...
随机推荐
- 2018-2-13-win10-安装Mpi
title author date CreateTime categories win10 安装Mpi lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23: ...
- 升级python后yum命令出错
执行yum命令报错: yum list Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 epel | 5.3 kB 00:00:00 extr ...
- 聚合函数 -AVG/MAX/MIN/STDDEV/VARIANCE/SUM/COUNT/MEDIAN
------------------------------------------聚合函数--------------------------------------------- --1: AVG ...
- dubbo-源码阅读之容器启动
dubbo Main 例子 public class Start { public static void main(String[] args) throws Exception { com.ali ...
- jieba分词单例模式及linux权限不够情况下tmp_dir自定义
在linux环境下,没有root权限的情况下,有时会碰到如下问题: Building prefix dict from the default dictionary ... Loading model ...
- 怎么部署TFS
https://vsalm-hols.readthedocs.io/zh_CN/latest/sysadmin/tfs-installation-ad.html 根据以上网站的步骤一字不差的进行部署肯 ...
- 解决mybatisplus saveBatch 或者save 无法插入主键问题
解决mybatisplus saveBatch 或者save 无法插入主键问题 通过跟踪源码后得出结论,由于插入的表的主键不是自增的,而是手动赋值的,所以在调用saveBatch 执行的sql语句是没 ...
- nodejs模块——fs模块 读取文件
readFile读取文件 fs.readFile(filename,[option],callback) 方法读取文件. 参数说明: filename String 文件名 option Object ...
- AngularJS 项目开发实战
目录 啰嗦一下 你问我答 目录结构 压缩 其他 啰嗦一下 最近在开发一个项目时,调研了一下AngularJS,发现这个框架功能很丰富,而且用起来也很方便,所以深入了解了一下,在此分享一下我的感悟. A ...
- 如何将项目中的package.json文件中的模块更新到最新版本
有时候自己之前开发的项目回头运行的时候发行好多模块的版本不是最新的,此时想更为为最新版本,使用如下命令即可: npm i -g npm-check-updates ncu -u npm install