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 ...
随机推荐
- Python中的WebSocket
一.Websockets介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信 ...
- Java结对编程四则运算一周小结
Java结对编程四则运算一周小结 需求分析 对于四则运算来说最主要的就是要计算出产生的式子(字符串的形式). 设计思路 总体可将这个项目分解为几个部分:产生式子,计算式子,判断对错并记录: 具体的思路 ...
- Book Review of “The practice of programming” (Ⅲ)
The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...
- idea通过springboot初始化器新建项目
1.通过初始化器新建项目,勾选后 对应生成的pom文件 以及生成的包路径 2.生成项目后点击稍后弹出的自动自动导入maven工程的改变,当pom中有依赖改变时会自动刷新导入依赖 3.删除自动生成项目的 ...
- 《大型网站系统与JAVA中间件实践》读书笔记-消息中间件
消息中间件 1.消息中间件的价值 1.1 透过示例看消息中间件对应用的解耦 1.1.1.通过服务调用让其他系统感知事件发生的方式 假设我们要做一个用户登录系统,其中需要支持的一个功能是,用户登录成功 ...
- 高亮显示UILabel中的子串
I. 用户在搜索框中,输入关键字进行检索时,APP对搜索结果进行显示,有以下两种情况: 1. 匹配一次,如检索关键字为人名 这种情况,实现比较容易.写一个UILabel的category, 用rang ...
- eclipse向上/下复制一行(或者多行)的快捷键失效的基本解决方法
在eclipse中,快捷键Ctrl+Alt+↓是向下复制选中的行,快捷键Ctrl+Alt+↑是向上复制选中的行. 这两个快捷键也是我常用的快捷键之一,以前也遇到失效. 所以现在记录一个解决的方法: 在 ...
- JMS-activeMq发布订阅模式
上一篇对点对点模式进行总结,这一篇都发布订阅模式进行总结,代码都差不多,唯一区别就是创建队(session.createQueue(目的地))列改为创建主题(session.createTopic(目 ...
- LeetCode第[66]题(Java):Plus One
题目:数组加一 难度:Easy 题目内容: Given a non-empty array of digits representing a non-negative integer, plus ...
- mysql学习笔记(window下简单使用+Navict)
之前安装过mysql.最近刚好要重新翻出来看看 发现又忘记了那些命令.还是要百度.所以不如自己整理下 尽管网上有很多相关的介绍.当时想对的不如自己整理出来的舒服 首先是下载安装.网上有很多就不一一罗列 ...