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 .可能会有多种方案,请你返回满足要求的两个子数组长度和的 ...
随机推荐
- Mysql差集
记录一个去差集的SQL 今天用sql去同步部分历史数据,需要用到一个求差集的sql 两张表简单结构如下: 有一个会员表 一个会员账户表 获取没有账户的会员 SELECT m.pkMember FROM ...
- nginx 和keepalived的使用
今天看了培训视频,看到这俩玩意,挺有意思,先粘贴一下,别等到时候忘了. 官方网站 www.nginx.org nginx的特点 稳定版本是用偶数来做标记,测试版本使用奇数作为标记 通过yum来安装 安 ...
- MHA + proxysql 高可用以及读写分离
环境 vip 192.168.1.101 slave 192.168.1.16 5.7.17 3306 master 192.168.1.135 5.7.17 3306 proxysql 192.16 ...
- TCP 三次握手和四次挥手中的ACK 为什么总是SYN + 1 或者 FIN +1 而不是+ 其他数值?
TCP 三次握手的时候 1.客户端 向服务端发起连接请求,这个时候客户端将发送一个SYN分节(假设其值为J),它告诉服务端我发送数据的初始序列号将是J. 2.服务端收到这个请求后,必须确认(ACK) ...
- python制作坦克对战
创建子弹类 import pygame class Bullet(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__i ...
- 【leetcode】538. Convert BST to Greater Tree
题目如下: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the orig ...
- MySQL - primary key PK unique key,key PK index
primary key PK unique key 总结 primary key = unique + not null 主键不能为空每个字段值都不重复,unique可以为空,非空字段不重复 uniq ...
- 浏览器表单默认记忆功能input的 autocomplete="off"属性
一般情况下浏览器会有自动记录密码等的功能,但是有时候我们不需要这样的功能,下面有两种情况下关闭记忆功能有效: 1:在form中,如果有input[type=password],autocomplete ...
- DGIM算法
/***************************************************** copyright (C), 2014-2015, Lighting Studio. Co ...
- springboot接口:CommandLineRunner
springBoot接口:CommandLineRunner 一.作用: 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Com ...