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

时间:2021-07-24 08:23:12

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

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/degree-of-an-array/description/

题目描述:

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:

  1. nums.length will be between 1 and 50,000.
  2. nums[i] will be an integer between 0 and 49,999.

Ways

方法一:

题目大意:
给定非空非负整数数组,数组的度是指元素的最大出现次数。

寻找最大连续区间,使得区间的度与原数组的度相同。

想法很粗暴,直接求出整个数组的degree,然后找出所有的度等于该degree的数,找出最小度的数。

import collections
class Solution(object):
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == len(set(nums)):
return 1
counter = collections.Counter(nums)
degree_num = counter.most_common(1)[0]
most_numbers = [num for num in counter if counter[num] == degree_num[1]]
scale = 100000000
for most_number in most_numbers:
appear = [i for i,num in enumerate(nums) if num == most_number]
appear_scale = max(appear) - min(appear) + 1
if appear_scale < scale:
scale = appear_scale
return scale

上面使用了Counter,下面的直接数,速度有一点提高。

import collections
class Solution(object):
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_set = set(nums)
if len(nums) == len(nums_set):
return 1
degree = max([nums.count(num) for num in nums_set])
most_numbers = [num for num in nums_set if nums.count(num) == degree]
scale = 100000000
for most_number in most_numbers:
appear = [i for i,num in enumerate(nums) if num == most_number]
appear_scale = max(appear) - min(appear) + 1
if appear_scale < scale:
scale = appear_scale
return scale

上面的不够快是因为重复计算了多次的nums.count(num),避免重复计算可以使用字典进行保存。这个方法超出了96.7%的提交。

import collections
class Solution(object):
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_set = set(nums)
if len(nums) == len(nums_set):
return 1
num_dict = {num:nums.count(num) for num in nums_set}
degree = max(num_dict.values())
most_numbers = [num for num in nums_set if num_dict[num] == degree]
scale = 100000000
for most_number in most_numbers:
appear = [i for i,num in enumerate(nums) if num == most_number]
appear_scale = max(appear) - min(appear) + 1
if appear_scale < scale:
scale = appear_scale
return scale

还能更快吗?可以。把能压缩的列表表达式拆开,这样迭代一次就可以了。最后用了个提前终止,如果scale==degree说明这段子列表里没有其他元素了,一定是最短的。

这个方法超过了99.91%的提交。

import collections
class Solution(object):
def findShortestSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_set = set(nums)
if len(nums) == len(nums_set):
return 1
num_dict = {}
degree = -1
for num in nums_set:
_count = nums.count(num)
num_dict[num] = _count
if _count > degree:
degree = _count
most_numbers = [num for num in nums_set if num_dict[num] == degree]
scale = 100000000
for most_number in most_numbers:
_min = nums.index(most_number)
for i in xrange(len(nums)-1, -1, -1):
if nums[i] == most_number:
_max = i
break
appear_scale = _max - _min + 1
if appear_scale < scale:
scale = appear_scale
if scale == degree:
break
return scale

Date

2018 年 1 月 23 日

【LeetCode】697. Degree of an Array 解题报告的更多相关文章

  1. 【LeetCode】697&period; Degree of an Array 解题报告(Python)

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

  2. LeetCode 697&period; Degree of an Array (数组的度)

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  3. LeetCode&colon; Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  4. &lbrack;LeetCode&rsqb; 697&period; 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&period; Degree of an Array

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

  6. 【LeetCode】912&period; Sort an Array 解题报告&lpar;C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  7. 【LeetCode】941&period; Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】88&period; Merge Sorted Array 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...

  9. 【LeetCode】384&period; Shuffle an Array 解题报告(Python & C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...

随机推荐

  1. jQuery&sol;js 正则收集(邮件验证、)

    var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; //验证邮箱的正则表达式if( ...

  2. 关于用netbeans和xdebug调试php的配置

    之前用过一段时间在apache,netbeans下通过xdebug调试.感觉不错,最近事情不多想从新配置下,是基于最新版本的php5.4做的,后来参考了下xdebug的官网说明完成的.官网地址:htt ...

  3. linux中的sticky bit

    今天看到有个目录的权限是rwxrwxrwt 很惊讶这个t是什么,怎么不是x或者-呢?搜了下发现: 这个t代表是所谓的sticky bit. sticky bit: 该位可以理解为防删除位. 一个文件是 ...

  4. Java基础知识强化之集合框架笔记43:Set集合之TreeSet存储Integer类型的元素并遍历

    1. TreeSet类概述: • 能够对元素按照某种规则进行排序. • 或者根据创建set时提供的Comparator进行排序 • 具体取决于使用的构造方法 2. 代码示例: package cn.i ...

  5. &lbrack;转&rsqb;php 在各种web服务器的运行模式

    一.php在apache中运行模式 php在apache中一共有三种工作方式:CGI模式.FastCGI模式.Apache 模块DLL) 以下分别比较: 1. CGI模式与模块模式比较: php在ap ...

  6. 关于Tarjan&lpar;2&rpar;

    Tarjan有第二个神奇的用法,求强连通分量!!!!!!!!!!!!!!!!!!! 同样利用了dfn:dfs序,low:能回到的最早祖先的dfn: 废话少说 上板子 #include<iostr ...

  7. poj 1562 dfs

    http://poj.org/problem?id=1562 #include<iostream> using namespace std; ,m=,sum=; ][]; ][]={-,, ...

  8. MFC新建工程中目录包含中文,资源文件打开失败

    ※尽量不适用中文,各种未知错误,嘿嘿 此方法临时解决问题,可以使程序运行,后续是否还有错误是未知数 需要修改3处位置: 1.资源文件中.rc 右键,点击“查看代码”,找到带中文的资源ID,把中文修改掉 ...

  9. spark中的combineByKey函数的用法

    一.函数的源码 /** * Simplified version of combineByKeyWithClassTag that hash-partitions the resulting RDD ...

  10. windows系统中hosts文件位置

    C:\Windows\System32\drivers\etc\hosts 10.0.0.213 mr1.bic.zte.com 10.0.0.2 mr2.bic.zte.com 10.0.0.102 ...