题目描述:

方法:

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-最大相等频率的更多相关文章

  1. [LeetCode] 895. Maximum Frequency Stack 最大频率栈

    Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack ...

  2. 【LeetCode】451-根据字符出现频率排序

    题目描述 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r'和' ...

  3. leetcode.排序.451根据字符出现频率排序-Java

    1. 具体题目 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r ...

  4. ✡ 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 ...

  5. leetcode[158] Read N Characters Given Read4 II - Call multiple times

    想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...

  6. [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 ...

  7. leetcode 双周赛9 进击的骑士

    一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里. 骑士的走法和中国象棋中的马相似,走 “日” 字:即先向左(或右 ...

  8. leetcode 双周赛9 找出所有行中最小公共元素

    给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,] ...

  9. [每日一题2020.06.16] leetcode双周赛T3 5423 找两个和为目标值且不重叠的子数组 DP, 前缀和

    题目链接 给你一个整数数组 arr 和一个整数值 target . 请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target .可能会有多种方案,请你返回满足要求的两个子数组长度和的 ...

随机推荐

  1. 2018-2-13-win10-uwp-资源字典

    title author date CreateTime categories win10 uwp 资源字典 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  2. JNI intArray

    JNIDemo.java public class JNIDemo { static { /* 1. load */ System.loadLibrary("native"); / ...

  3. 聚合函数 -AVG/MAX/MIN/STDDEV/VARIANCE/SUM/COUNT/MEDIAN

    ------------------------------------------聚合函数--------------------------------------------- --1: AVG ...

  4. Failed to bind properties under '' to com.zaxxer.hikari.Hikari DataSource Spring Boot解决方案

    Description: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource: Property: dri ...

  5. cf期望概率专题

    cf1009E:求到第i段期望和的比较困难,但是单独求每段的期望是比较容易的,所以单独对每段求和,然后累计总和 E[i]=1/2*a1+1/4*a2+...+1/2^(i-1)*ai-1+1/2^(i ...

  6. RRT路径规划算法(matlab实现)

    基于快速扩展随机树(RRT / rapidly exploring random tree)的路径规划算法,通过对状态空间中的采样点进行碰撞检测,避免了对空间的建模,能够有效地解决高维空间和复杂约束的 ...

  7. Android Runnable 运行在那个线程

    Runnable 并不一定是新开一个线程,比如下面的调用方法就是运行在UI主线程中的: Handler mHandler=new Handler(); mHandler.post(new Runnab ...

  8. (转)OpenFire源码学习之六:用户注册

    转:http://blog.csdn.net/huwenfeng_2011/article/details/43413509 用户注册 注册流程: 1.客户端进行握手给服务端发送连接消息: <s ...

  9. AntiPlug

    反插件工程 #pragma once #ifndef __ENHANFUNC_H__ #define __ENHANFUNC_H__ #include <iostream> #includ ...

  10. Java-Class-@I:io.swagger.annotation.Api

    ylbtech-Java-Class-@I:io.swagger.annotation.Api 1.返回顶部   2.返回顶部 1. package com.ylbtech.api.controlle ...