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 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的更多相关文章
- 【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&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 ...
- 【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 th ...
- 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 ...
- 697. Degree of an Array 频率最高元素的最小覆盖子数组
[抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
随机推荐
- bzoj 3876: [Ahoi2014&Jsoi2014]支线剧情【有上下界有源汇最小费用最大流】
每条边流量有下界有费用,很显然是有上下界有源汇最小费用最大流 连边(s,1,(0,inf),0),(i,t,(0,inf),0),表示从1出发inf次从每个点结束inf次 连边(i,j,(1,inf) ...
- Hadoop概念学习系列之Hadoop 生态系统
当下 Hadoop 已经成长为一个庞大的生态体系,只要和海量数据相关的领域,都有 Hadoop 的身影.下图是一个 Hadoop 生态系统的图谱,详细列举了在 Hadoop 这个生态系统中出现的各种数 ...
- JS异常捕获和抛出
try...catch 用来异常捕获(主要适用于IE5以上内核的浏览器,也是最常用的异常捕获方式) 使用onerror时间捕获异常,这种捕获方式是比较古老的一中方式,目前一些主流的浏览器暂不支持这种 ...
- 118 Pascal's Triangle 帕斯卡三角形 杨辉三角形
给定 numRows, 生成帕斯卡三角形的前 numRows 行.例如, 给定 numRows = 5,返回[ [1], [1,1], [1,2,1], [1,3,3,1], [1 ...
- 115 Distinct Subsequences 不同子序列
给定一个字符串 S 和一个字符串 T,求 S 的不同的子序列中 T 出现的个数.一个字符串的一个子序列是指:通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串.(譬如," ...
- 关于IO模拟时序(SPI)的注意事项
原则:有硬件I2C.SPI时尽量用硬件操作,省去IO模拟繁琐的时序调试.但在内部资源不够时就要用IO模拟总线了. 关于短延时:模拟时序时是否需要延时要看MCU与device的相对速度.比如I2C如果4 ...
- Function ereg() is deprecated in
PHP 5.3 ereg() 无法正常使用,提示"Function ereg() is deprecated Error".问题根源是php中有两种正则表示方法,一个是posix, ...
- [转]用NPOI操作EXCEL--通过NPOI获得公式的返回值
本文转自:http://www.cnblogs.com/atao/archive/2009/10/12/1582085.html 前面我们学习了通过NPOI向Excel中设置公式,那么有些读者可能会问 ...
- Log4j日志框架小记
人啊,总是在学习中发现不足,不足中学习,学习中成长. 今天来系统记录一下对于常用日志组件的理解.配置.使用. 仅供参考, 错误之处请各路好汉不吝笔墨批评指正. 转载请注明出处 Log4j日志框架是Ap ...
- kickstart_2018_round_H_C Let Me Count The Ways
思路: 容斥. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ; ; ll f[MAXN ...