[LeetCode] 697. Degree of an Array_Easy tag: Hash Table
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.lengthwill 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
随机推荐
- Java环境变量配置错误
1,由于Java的环境变量配置错误,导致用到Java的编译过程中出现错误: 改正办法: wget http://download.oracle.com/otn-pub/java/jdk/8u181-b ...
- ++ fatal error C1083: 无法打开预编译头文件:“.\Debug\router.pch”
一.出现此错误首先检查:stdafx.cpp文件上右键——属性,预编译头选“创建”,其它cpp选“使用”. 二.如果是采用这样的设置,还是有错误,重新生成解决方案,重新调试. 三.实在不行的话,步骤/ ...
- node.js发送邮件email
通常我们做node项目时,可能我们会碰到做一个简单的邮件反馈,那么我们今天就来讨论一下,其中遇到的各种坑. 总的来说做这个东西,我们可能需要node第三方依赖模块,来实现我们要达到的效果. 这里我推荐 ...
- ubuntu1304无法启动桌面系统的问题和解决
今天上班,从oracle官网下载个最新的virtual box,安装后重启电脑,进入桌面后竟然没有菜单栏和启动栏了(就是最上边的bar和左边的应用栏),而且所有启动的窗口都没有菜单栏,终端什么的也都没 ...
- 关于*** WARNING L15: MULTIPLE CALL TO SEGMENT
编写51程序的时候,有时候会在主函数和中断函数里面调用同一个函数,如果正的出现这种情况,编译器会提出 这种警告: *** WARNING L15: MULTIPLE CALL TO SEGMENT(重 ...
- 关于JavaScript转义字符('、 " 、\" 、\')【原创】
先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...
- python-django开发学习笔记三
1.简述 1.1 开发环境 该笔记所基于的开发环境为:windows8.python2.7.5.psycopg2-2.4.2.django1.5.4.pyCharm-2.7.3.以上所描述的软件.插件 ...
- mousedown\mouseup\click事件关系
背景分析: 如果用户在一个元素上点击,那么最少三个事件会被触发,事件发生顺序: 1.mousedown,当用户在这个元素上按下鼠标键的时候 2.mouseup,当用户在这个元素上松开鼠标键的时候 3. ...
- JavaScript之prototype对象
简述prototype: 在js中,每个构造函数都有一个原型属性prototype,因为这个属性的值通常是一个对象,又叫原型对象!你不需要显式的去定义原型对象,因为每个构造函数都会一个原型属性,通常在 ...
- uid列表来讲讲我是如何利用php数组进行排重的
经常接到要对网站的会员进行站内信.手机短信.email进行群发信息的通知,用户列表一般由别的同事提供,当中难免会有重复,为了避免重复发送,所以我在进行发送信息前要对他们提供的用户列表进行排重. 假如得 ...