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 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 """ # use pos_map to record num's start pos and end pos in nums pos_map = {} for i,n in enumerate(nums): if n not in pos_map: pos_map[n] = [i, i] else: pos_map[n][1] = i # find max_freq number (maybe have many) cnt = collections.Counter(nums) max_freq = 0 max_freq_num = [] for k,v in cnt.iteritems(): if v > max_freq: max_freq = v max_freq_num = [k] elif v == max_freq: max_freq_num.append(k) # find min degree return min(pos_map[n][1]-pos_map[n][0]+1 for n in max_freq_num)
精简代码:
class Solution: def findShortestSubArray(self, nums): """ :type nums: List[int] :rtype: int """ pos_map = {} for i,n in enumerate(nums): if n not in pos_map: pos_map[n] = [i, i] else: pos_map[n][1] = i cnt = collections.Counter(nums) max_freq_num = max(cnt.values()) return min(pos_map[n][1]-pos_map[n][0]+1 for n in cnt if cnt[n] == max_freq_num)
更精简的:
class Solution: def findShortestSubArray(self, nums): """ :type nums: List[int] :rtype: int """ c = collections.Counter(nums) first, last = {}, {} for i, v in enumerate(nums): first.setdefault(v, i) last[v] = i degree = max(c.values()) return min(last[v] - first[v] + 1 for v in c if c[v] == degree)
注意:
Python 字典(Dictionary) setdefault()方法
描述
Python 字典 setdefault() 函数和get() 方法类似, 如果键不存在于字典中,将会添加键并将值设为默认值。
语法
setdefault()方法语法:
dict.setdefault(key, default=None)
参数
- key -- 查找的键值。
- default -- 键不存在时,设置的默认键值。
返回值
如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。
实例
以下实例展示了 setdefault() 函数的使用方法:
实例(Python 2.0+)
#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {'runoob': '菜鸟教程', 'google': 'Google 搜索'} print "Value : %s" % dict.setdefault('runoob', None) print "Value : %s" % dict.setdefault('Taobao', '淘宝')
以上实例输出结果为:
Value : 菜鸟教程
Value : 淘宝
标签:
leetcode
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2017-04-01 信息检索导论的课件——http://home.ustc.edu.cn/~zhufengx/ir/pdf/
2017-04-01 通过Mesos、Docker和Go,使用300行代码创建一个分布式系统
2017-04-01 varint算法——本质上是牺牲最高位作为标识数据结束位,达到变长编码,说白了就是贪心的分割位