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 ...
随机推荐
- JMeter并发性测试
JMeter并发性测试 一.JMeter简介 JMeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因为jmet ...
- No module named bz2
yum install bzip* python2.6 import bz2 python2.7 import bz2 error 解决:sudo cp /usr/lib64/python2.6/li ...
- viewport大白话
以下所有内容均是我自己理解的,可能有误,懂得大佬希望指点一下我.. 首先,写一个简单的页面.里面只有1个200*200的div <html lang="en"> < ...
- Boostnote:适合程序员的笔记软件【转】
本文转载自:https://blog.csdn.net/u013553529/article/details/70306899 Boostnote:适合程序员的笔记软件 注意: Boostnote正在 ...
- 读取Excel复杂的数据
涉及到合并单元格的数据读取: package com.util; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util ...
- JQ input标签限制输入数字或字母
<input type="text" maxlength="20" class="input5" onkeyup="val ...
- Java 如何解析由String类型拼接的XML格式
String xml = new String(a);打印的xml 的值是 <?xml version= 1.0 encoding=gb2312?><weighData>< ...
- 使用mybatis报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.String]
报错constructor found in com.atguigu.mybatis.bean.Department matching [java.lang.Integer, java.lang.St ...
- 报错 IllegalArgumentException occurred calling getter of cn.itcast.entity.Customer.cid
我碰到这个问题的时候,没有数据类型不匹配的情况,也没有表达无法向action传递数据的情况,一直报这样的错误,意思就是无法使用Customer中的get方法来赋值.完整报错如下所示: HTTP Sta ...
- ActiveMQ 的管理和监控
本章重点 理解 JMX 和 ActiveMQ 使用告警消息来监控 ActiveMQ 管理 ActiveMQ ActiveMQ 的日志配置 额,这本书终于读完了,虽然看到后面都是云里雾里的,但是总算是对 ...