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.

这个题目思路就是用Counter去计算数量, 然后用d1, d2 分别存每个element存在的最左端和最右端, 然后min(ans, d2[k] - d1[k] + 1), 这里ans初始化为len(ans).

Code

class Solution:
def degreeArray(self, nums):
d = collections.Counter(nums)
fre = max(d.values())
d1, d2, ans = {}, {}, len(nums)
for index, num in enumerate(nums):
if num not in d1:
d1[num] = index
d2[num] = index
for k in d.keys():
if d[k] == fre:
ans = min(ans, d2[k] - d1[k] + 1)
return ans

[LeetCode] 697. Degree of an Array_Easy tag: Hash Table的更多相关文章

  1. [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  2. [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  3. [LeetCode] 1. Two Sum_Easy tag: Hash Table

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

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

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

  7. [LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table

    We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word co ...

  8. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

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

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

随机推荐

  1. 【大数据系列】MapReduce示例好友推荐

    package org.slp; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import ...

  2. Linux系统启动内幕

    经过对Linux系统有了一定了解和熟悉后,想对其更深层次的东西做进一步探究.这当中就包括系统的启动流程.文件系统的组成结构.基于动态库和静态库的程序在执行时的异同.协议栈的架构和原理.驱动程序的机制等 ...

  3. 为Docker容器中运行的gitlab添加ssh的一些问题记录

    最近做的一个东西,是将gitlab10.x的汉化版本,从源码编译(在源码中自己定制一些东西),然后制作成Docker镜像,作为Docker容器来运行 在启用容器中的gitlab的ssh的时候,遇到了一 ...

  4. docker link 过时不再用了?那容器互联、服务发现怎么办?

    在 1-2 年前,Docker 所有容器都连接于默认的桥接网络上,也就是很多老文章鼓捣的 docker0 桥接网卡.因此实际上默认情况下所有容器都是可以互联的,没有隔离,当然这样安全性不好.而服务发现 ...

  5. 23种设计模式之观察者模式(Observer)

    观察者模式又称为发布—订阅模式.模型—视图模式.源-监听器模式或从属者(dependents)模式,是一种对象的行为型模式.它定义了对象之间的一种一对多的依赖关系,使得每当一个对象状态发生改变时,其相 ...

  6. Unity3D笔记十六 输入输出-键盘事件、鼠标事件

    输入与控制操作Unity为开发者提供了Input类库,其中包括键盘事件.鼠标事件和触摸事件等一切跨平台所需要的控制事件. 一.键盘事件 1.按下事件 Input.GetKeyDown():如果按键被按 ...

  7. 利用 background 和 filter 模糊指定区域

    背景知识:background-size: cover;,background-attachment:fixed;,filter:blur() 难题: 通常,我们会通过filter:blur()去实现 ...

  8. Nginx通过header转发

    假设添加自定义头 "my-header",当"my-header"等于test时,转发到192.168.1.113 请求如下 wget --header=&qu ...

  9. The end of the world

    这首63年乡村及流行排行榜双料亚军的歌曲,(Cashbox 年终冠军)据说原是作者为怀念亡父写就,虽是流行曲风,但由Skeeter Davis演唱,却赋予其不同的生命,在键盘连绵的三连音中,少女诉说着 ...

  10. Java-字符串加密

    1设计思想: 改程序是对小写的a到z进行加密,输入一段字符串str,输入加密的密匙k,判断录入的的字符与 ‘z’-k+1的大小,比其小的直接加上密匙转化为新的字符,大于的加(k-26)将最后几位字符转 ...