[LeetCode&Python] Problem 697. Degree of an Array
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.
Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
Example 1:
Input: [1, 2, 2, 3, 1]
Output: 2
Explanation:
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.
Example 2:
Input: [1,2,2,3,1,4,2]
Output: 6
Note:
nums.lengthwill be between 1 and 50,000.nums[i]will be an integer between 0 and 49,999.
class Solution(object):
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count={}
left={}
right={}
for i,x in enumerate(nums):
if x not in left: left[x]=i
right[x]=i
count[x]=count.get(x,0)+1 degree=max(count.values())
ans=len(nums) for x in count:
if count[x]==degree:
if ans>right[x]-left[x]+1:
ans=right[x]-left[x]+1
return ans
[LeetCode&Python] Problem 697. Degree of an Array的更多相关文章
- 【Leetcode_easy】697. Degree of an Array
problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- [LeetCode] 697. Degree of an Array 数组的度
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- LeetCode 697. Degree of an Array (数组的度)
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- leetcode 697. Degree of an Array
题目: Given a non-empty array of non-negative integers nums, the degree of this array is defined as th ...
- 697. Degree of an Array@python
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
随机推荐
- 从线程模型的角度看Netty的高性能
转载:Netty(二) 从线程模型的角度看 Netty 为什么是高性能的? 传统 IO 在 Netty 以及 NIO 出现之前,我们写 IO 应用其实用的都是用 java.io.* 下所提供的包. 比 ...
- linux重命名所有find查找到的文件/文件夹
一.说明 在某些时候我们想要将所有find命令查找到的文件或文件夹全都重命名,比如都加上.bak后辍 二.操作命令 find /dir -name "*pattern*" -exe ...
- docker安装使用教程(Kali2.0)
一.apt安装 apt直接安装是最好的,因为apt源中的其他docker相关组件,也是与docker匹配的版本. apt-get install docker docker-compose 二.手动安 ...
- spring cloud jwt用户鉴权及服务鉴权
用户鉴权 客户端请求服务时,根据提交的token获取用户信息,看是否有用户信息及用户信息是否正确 服务鉴权 微服务中,一般有多个服务,服务与服务之间相互调用时,有的服务接口比较敏感,比如资金服务,不允 ...
- forget sus,syn sym semi word out~s
1★ sus 在~下面 2★ syn 3★ sym 共同 4★ semi 半
- oracle中查询当前系统时间用到的dual是什么?
oracle我们查询当前系统时间的时候,经常用到dual,这个是什么东西呢? -- 查询系统时间 结果:2018/04/17 15:00:48 -- select sysdate from dual; ...
- matlab作图 latex插图
推荐用saveas eps,再用eps2pdf转成pdf.这样可以之间pdflatex编译. if result.savepic saveas(gcf,[ pwd '/picture/right_' ...
- log4j不输出日志的解决方案
参考:http://blog.csdn.net/qq994406030/article/details/53433159 主要是log4j.properties log权限和log输出方式没配好.
- Oracle 12c新特性
转载自:Oracle 12c新特性(For DBA) 一: Multitenant Architecture (12.1.0.1) 多租户架构是Oracle 12c(12.1)的新增重磅特性 ...
- C++模板类之pair
Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型,第一个元素是int型的 ...