[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 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的更多相关文章
- 【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】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 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 ...
- 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 ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
随机推荐
- 手机验证码JQUERY实现
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- windows配置教程
1.卸载预装软件 2.卸载非安装的预装软件 有些软件被改成了“绿色版”软件不能通过软件列表卸载,一般在C:\Program Files (x86)目录下 可以直接删除其文件夹,如果提示文件夹无法删除则 ...
- Ubuntu安装openssh-server并通过xshell连接
#安装ssh sudo apt-get install openssh-server openssh-client sudo apt-get update sudo apt-get upgrade 查 ...
- 字符与字符串3——char 的大小
字符变量占用内存的大小,也就是char类型声明的变量,这个变量占多少字节. 一字节 char c = 'A'; printf("%d,%d\n", sizeof(c),sizeof ...
- C++的string类型和继承C语言风格的字符串的区别与注意事项
1.尽可能地在C++程序中使用string,不要使用继承而来的C语言风格的字符串,会出现许多安全问题. 2.C语言的字符串风格,是以空字符结束的,在C++的头文件cstring中定义了C语言风格的字符 ...
- 从R-CNN到FAST-RCNN再到Faster R-CNN
(Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks) R-CNN: (1)输入测试图像: ...
- L1-055 谁是赢家
某电视台的娱乐节目有个表演评审环节,每次安排两位艺人表演,他们的胜负由观众投票和 3 名评委投票两部分共同决定.规则为:如果一位艺人的观众票数高,且得到至少 1 名评委的认可,该艺人就胜出:或艺人的观 ...
- Java的第一个晞月自己打的程序
1.编写一个程序,求1!+2!+…+10!的值. package xxx; public class abc { public static void main(String args[]) { in ...
- IDEA中自动生成serialVersionUID
File >> Setting >> Inspections >> serializable 勾选上后,光标放在实现Serializable接口的类名上 ...