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:

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.

原题地址: Degree of an Array

难度: Easy

题意: 先找出包含数量最多的值的子数组,返回最短的子数组长度.如上面例子,数量最多的数是1和2,包含1数字的子数组长度为5,包含2数字的子数组长度为2,所以返回2

思路:

(1)遍历数组,记录每个数值的数量已经最左边和最右边的索引值

(2)找出数量最多的数,返回长度

class Solution(object):
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l, r = {}, {} # 记录数字最左边和最右边的索引
d = {} # 记录每个数字的数量
for i, num in enumerate(nums):
if num not in l:
l[num] = i
r[num] = i
d[num] = d.get(num, 0) + 1
degree = max(d.values()) # 找出值最大的数量
res = len(nums)
for i in d:
if d[i] == degree:
res = min(res, r[i] - l[i] + 1)
return res

时间复杂度: O(n)

空间复杂度: O(n)

697. Degree of an Array@python的更多相关文章

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

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

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

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

  9. 697. Degree of an Array 频率最高元素的最小覆盖子数组

    [抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...

随机推荐

  1. 2014-10-23 NOIP模拟赛

    NOIP2014模拟赛 -----lwher 时限均为1s,内存 256MB 1.Jams倒酒(pour) Jams是一家酒吧的老板,他的酒吧提供2种体积的啤酒,a ml 和 b ml,分别使用容积为 ...

  2. Spring Cloud:使用Ribbon实现负载均衡详解(下)

    在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何 ...

  3. 结合 webpack 使用 vue-router(七)

    结合 webpack 使用 vue-router: 首先安装路由包vue-router: cnpm install vue-router 使用模块化工具导入 vue-router 后,必须手动调用 V ...

  4. C 语言实例 - 二进制与十进制相互转换

    C 语言实例 - 二进制与十进制相互转换 C 语言实例 C 语言实例 二进制转与十进制相互转换. 实例 - 二进制转换为十进制 #include <stdio.h> #include &l ...

  5. idea | 设置支持java8的lambda表达式

    修改配置: setting:       project structure:

  6. BZOJ1218(线段树+扫描线)

    1.扫描线扫描x轴,线段树维护y轴. 2.坐标+1,线段树是从1开始维护.然后让边长--,这样就能包含边上的点了. 3.为了保证点在正方形内:在x轴上利用差分的思想,在x出Add(val),在x+r( ...

  7. ecshop如何增加多个产品详细描述的编辑器

    在做商产品详情的时候,经常会有选项卡类似的几个产品说明,如:商品详情,商品规格,参数列表,售后服务等. Ecshop后台里面默认只有一个编辑框(器),那么我们还得自己添加几个,以下是ecshop如何增 ...

  8. js filter用法比较

    讲解一个很实用的JS小语法 filter 就是从数组中找到适合条件的元素(比如说大于某一个元素的值) var arr=[1,23,5,78,34,55,13]; 如何才能找到大于23的所有元素呢, 1 ...

  9. Java创建对象的过程

    Java创建对象的过程 Java是一门面向对象的编程语言,在Java程序运行过程中每时每刻都有对象被创建出来.在语言层面上,创建对象通常仅仅是一个new关键字而已,而在虚拟机中,对象的创建又是怎样一个 ...

  10. 海康威视采集卡结合opencv使用(两种方法)-转

    (注:第一种方法是我的原创 ^_^. 第二种方法是从网上学习的.) 第一种方法:利用 板卡的API:  GetJpegImage 得到 Jpeg 格式的图像数据,然后用opencv里的一个函数进行解码 ...