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.length will 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的更多相关文章

  1. 【Leetcode_easy】697. Degree of an Array

    problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...

  2. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  3. 697. Degree of an Array - LeetCode

    697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...

  4. 【LeetCode】697. Degree of an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...

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

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

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

  8. 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 ...

  9. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

随机推荐

  1. 网页定位点击事件js响应函数教程(Chrome)

    一.背景说明 在前端页面调试或者渗透测试(尤其是XSS)时,我们经常想定位js函数位置:比如点击了某个位置弹出了一个对话框,这是哪个文件的哪个js函数在响应. 本文以Chrome浏览器定位点击事件响应 ...

  2. python(5)之集合

    集合是一个无序的,不重复的数据组合,它的主要作用如下: 1.去重,把一个列表变为集合就自动去重了 2.关系测试,测试两组数据之间的交集.并集.差集等 常用操作如下: #集合的操作 list_1={1, ...

  3. CRF(Conditional Random Field)

    条件随机场是近几年自然语言处理领域常用的算法之一,常用于句法分析.命名实体识别.词性标注等.在我看来,CRF就像一个反向的隐马尔可夫模型(HMM),两者都是用了马尔科夫链作为隐含变量的概率转移模型,只 ...

  4. LY.JAVA面向对象编程.final、多态、抽象类、接口

    2018-07-08    13:47:26 final关键字 多态 从右向前念 多态的成员访问特点及转型的理解 多态的问题理解: class 孔子爹 { public int age = 40; p ...

  5. NTT模板(无讲解)

    #include<bits/stdc++.h>//只是在虚数部分改了一下 using namespace std; typedef long long int ll; ; ; ; ; ll ...

  6. nginx:负载均衡实战(一)

    1.负载均衡说明 2.准备 我自己在电脑布置了两台虚拟机,两台都有nginx和tomcat,两台虚拟机布置的ip分别是37以及54,我在tomcat的首页动了点手脚,方便自己看是来自哪个ip的 接着在 ...

  7. It is never too late!

    整理着过去的学习笔记,零零碎碎的,偶尔夹杂着当时的心境. 泛泛的学着东西,不很系统,不很深入,倒像是在拾海,有时捡捡贝壳,有时抓抓螃蟹.叹服大海的神奇,还没来得及深钻某个领域. (以下内容写于2016 ...

  8. 【转载三】Grafana系列教程–Grafana的配置及运行

    本篇文章,就让我们来了解下,Grafana的简单配置及运行. 详细的配置我们后面会讲,本篇文章只对其配置做一个简单了解,主要是让大家了解Grafana的配置及启动的过程及方法. 更多Grafana问题 ...

  9. VS2015 C#调用C++ 托管代码无法调试问题排查

    C#引用C++ 部份代码编绎DLL,调试时,无法命中C++ 代码部份,解决方式: 1. 调试-> 选项->调试->常规 : 勾选 [使用托管兼容模式] (同时点击 同级目录 [符号] ...

  10. 打开和写入excel文件

    一.使用win32读取excel内容 # -*- coding: utf-8 -*- from win32com import client as wc def open_excel(): excel ...