【leetcode】1224. Maximum Equal Frequency
题目如下:
Given an array
numsof positive integers, return the longest possible length of an array prefix ofnums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences.If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0).
Example 1:
Input: nums = [2,2,1,1,5,3,3,5]
Output: 7
Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4]=5, we will get [2,2,1,1,3,3],
so that each number will appear exactly twice.Example 2:
Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
Output: 13Example 3:
Input: nums = [1,1,1,2,2,2]
Output: 5Example 4:
Input: nums = [10,2,8,9,3,8,1,5,2,3,7,6]
Output: 8Constraints:
2 <= nums.length <= 10^51 <= nums[i] <= 10^5
解题思路:从头开始遍历nums,很容易可以求出0~i区间内每个数字出现的次数,并用dic_count存储,key为数字的值,而value为数字出现的次数。同时再用dic记录dic_count中每个次数出现的次数。例如 [1,1,1,2,2,2,3,3,3,4,4,4,5] 区间,dic_count = {1: 3, 2: 3, 3: 3, 4: 3, 5: 1},而dic = {1: 1, 3: 4} ,这里1:1表示在dic_count中出现1次的数字有1个,出现3次的数字有四个。很显然,要判断0~i区间是否删除一个元素后可以使得所有元素出现的次数一样,需要满足以下条件:
1. 如果len(dic)为1,只要所有的元素都只出现一次,那么这个区间就是满足题目要求的;
2. 如果len(dic)为2,需要满足有且仅有一个元素出现一次,表示可以删除掉这个元素。或者是有且仅有一个元素出现的次数是其他元素出现的次数+1,表示删除掉这个元素后就使得其和其他元素出现次数一致。
代码如下:
class Solution(object):
def maxEqualFreq(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
dic_count = {}
dic = {}
for i in range(len(nums)):
if nums[i] in dic_count:
dic_count[nums[i]] += 1
count = dic_count[nums[i]]
dic[count] = dic.setdefault(count, 0) + 1
if count > 1 and count - 1 in dic:
dic[count - 1] -= 1
if dic[count - 1] == 0:
del dic[count - 1]
else:
dic_count[nums[i]] = 1
count = dic_count[nums[i]]
dic[count] = dic.setdefault(count, 0) + 1 if len(dic) == 2:
k1 = k2 = None
for key in dic.iterkeys():
if k1 == None:
k1 = key
continue
k2 = key
#if abs(k1-k2) == 1 and (dic[k1] == 1 or dic[k2] == 1):res = (i+1)
if (k1 - k2 == 1 and dic[k1] == 1) or (k2 - k1 == 1 and dic[k2] == 1):res = i+1
elif (k1 == dic[k1] == 1) or (k2 == 1 and dic[k2] == 1):res = i + 1
elif len(dic) == 1:
# key is 1 : ex, input is [1,0]
if 1 in dic and dic[1] > 0:res = i+1
else:
for val in dic.itervalues():
if val == 1:res = i+1
return res
【leetcode】1224. Maximum Equal Frequency的更多相关文章
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目如下: Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square ...
- 【leetcode】1255. Maximum Score Words Formed by Letters
题目如下: Given a list of words, list of single letters (might be repeating) and score of every charact ...
- 【leetcode】1208. Get Equal Substrings Within Budget
题目如下: You are given two strings s and t of the same length. You want to change s to t. Changing the ...
- 【leetcode】1189. Maximum Number of Balloons
题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...
随机推荐
- cocos2dx[3.2](10) 新回调函数std::bind
在2.x中处理事件需要用到委托代理(delegate),相信学过2.x的触摸事件的同学,都知道创建和移除的流程十分繁琐. 而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器Eve ...
- P2077 【红绿灯】
我tm真是想不出来还有什么好玩的东西了~~ 这题是一道纯模拟题,只需要用一个变量表示当前汽车行驶了多少分钟 不难发现,这个神奇的变量可以直接用m表示,还可以省去一个变量...(好像并没有什么卵用) 具 ...
- Win10成功安装IIS后浏览器打开localhost正确显示的页面
- jquery validate 自定义校验方法
1.引入JS jquery.min.js jquery.validate.min.js messages_zh.min.js 2.添加验证方法,第一个参数为验证方法的名称,第二个参数为验证方法. $. ...
- CentOS7 nginx 最简单的安装以及设置开机启动
1. 下载tar包. 2. 解压缩tar包 3. 安装必须的部分 yum包 yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd ...
- MySQL -2- 体系结构--随笔小记
简介与安装NoSQLRDBMS版本安装方式二进制安装,源码安装体系结构CS模型TCP/IPsocketmysql master thread 实例mysqld 的程序构成连接层 协议.验证.链接线程S ...
- 《剑指offer》面试题6 重建二叉树 Java版
(由一个二叉树的前序和中序序列重建一颗二叉树) 书中方法:我们要重建一棵二叉树,就要不断地找到根节点和根节点的左子结点和右子节点.注意前序序列, 它的第一个元素就是二叉树的根节点,后面的元素分为它的左 ...
- java 字符串锁
package com.example.demo.controller; public class StringLock { public void method(String p) { // new ...
- vim学习(一)之简介、安装、配置
vim简介 Vim是从 vi 发展出来的一个文本编辑器,是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面. 简单的来说, vi 是老式的文字处理器,不过功能已经很齐全了,但是还是 ...
- [转载]Ubuntu如何切换到命令行模式
来源:https://blog.csdn.net/lyy14011305/article/details/76325067 Ubuntu提供两种进入方式,一个是我们平常最熟悉的图形界面形式,还有一种是 ...