LeetCode Degree of an Array
原题链接在这里: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:
nums.length
will be between 1 and 50,000.nums[i]
will be an integer between 0 and 49,999.
题解:
找出最大的frequency, 并标注对应的element出现过的左右index位置.
Time Complexity: O(n). n = nums.length.
Space: O(n).
AC Java:
- class Solution {
- public int findShortestSubArray(int[] nums) {
- HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();
- HashMap<Integer, Integer> leftInd = new HashMap<Integer, Integer>();
- HashMap<Integer, Integer> rightInd = new HashMap<Integer, Integer>();
- int degree = 0;
- for(int i = 0; i<nums.length; i++){
- count.put(nums[i], count.getOrDefault(nums[i], 0)+1);
- degree = Math.max(degree, count.get(nums[i]));
- if(leftInd.get(nums[i]) == null){
- leftInd.put(nums[i], i);
- }
- rightInd.put(nums[i], i);
- }
- int res = Integer.MAX_VALUE;
- for(int i = 0; i<nums.length; i++){
- if(count.get(nums[i]) == degree){
- res = Math.min(res, rightInd.get(nums[i]) - leftInd.get(nums[i]) + 1);
- }
- }
- return res;
- }
- }
LeetCode Degree of an Array的更多相关文章
- [LeetCode] 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 the ma ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- 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
题目: Given a non-empty array of non-negative integers nums, the degree of this array is defined as th ...
- [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 之 Degree of an Array
1.题目描述 Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
随机推荐
- undefined symbol: PyUnicodeUCS4_AsUTF8String
python 默认是ucs2编码进行编译,重新编译使用ucs4. python: ./configure --enable-unicode=ucs4 make && ...
- uiautomator--图像处理
一.图像处理在自动化中使用场景 1)效果类截图 图像处理技术在自动化的场景中很容易使用到.自动化不是万能的,有时候效果类的是无法进行验证的,但是效果类一般会有图像显示,我们可以通过截图对比实现. 2 ...
- hadoop入门小知识点
注意各个主机之间的通信 文件的复制 scp指令 scp /etc/profile acm03:/etc 所有历史版本: archive.apache.org hdfs://acm01:9000 ...
- EYES组——软件体系结构上机规划
EYES组——软件体系结构上机规划 考勤助手 第九周: 需求分析的详细撰写,并在此基础上探讨与完善,讨论软件的体系结构风格,画出初步的UML类图. 第十周: 选择合适的软件构架风格(3层C/S架构风格 ...
- 比较好的SQL语句
批次导数据表头 SELECT [运单号] , [运单号] AS [订单号] , [运单号] AS [订单号] , [运单号] , SUM([price] * [ProductNum]) AS [订单总 ...
- NumPy数组属性
NumPy - 数组属性 这一章中,我们会讨论 NumPy 的多种数组属性. ndarray.shape 这一数组属性返回一个包含数组维度的元组,它也可以用于调整数组大小. 示例 1 import n ...
- scala学习手记30 - 闭包
首先要弄白闭包的概念. 教材中的说法是:闭包是一种特殊的函数值,闭包中封闭或绑定了在另一个作用域或上下文中定义的变量.这里说闭包是一种特殊的函数值. 维基百科中的说法是:在计算机科学中,闭包(英语:C ...
- 技术分享:HBase架构
Zookeeper,作为分布式的协调.RegionServer也会把自己的信息写到ZooKeeper中. HDFS是Hbase运行的底层文件系统 RegionServer,理解为数据节点,存储数据的. ...
- ActiveMQ 性能调优
本章重点 学习普通的性能调优技巧 怎么优化生产者和消费者 调优实例 简介 ActiveMQ 的性能高度依赖于多种不同的因素,包括:网络代理拓扑架构,传输层,底层网络的服务质量和速度,硬件,操作系统和 ...
- 设置 Mysql中的datetime的默认值
如果在navicat下操作,将字段类型设置为timestamp,然后默认值写上CURRENT_TIMESTAMP即可