697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode
Question
697. Degree of an Array - LeetCode

Solution
理解两个概念:
数组的度:[1,2,2,3,1]这个数组,去重后的元素为[1,2,3],每个元素在原数组中重复的次数分别是221,数组的度就是元素最大重复次数,这个数组中元素最大重复次数就是2
连续子数组:和字符串中的了串概念类似,数组元素顺序保持不变,取其中一部分,该题就是求在度相同的情况下最小连续子数组的长度
思路:数组的度是以元素为对象,每个元素都有一个度(degree),第一次出现的坐标(start),最后一次出现的坐标(end),求出degree最大,end-start最小的那个情况,end-start+1就是该数组最小连续子数组的长度了。
public int findShortestSubArray(int[] nums) {
int start = 0;
int end = 0;
int degree = 0;
Map<Integer, Element> numElementMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
Element element = numElementMap.get(num);
if (element == null) {
element = new Element(i, 0);
numElementMap.put(num, element);
}
element.degree++;
int elementEnd = i;
// degree最大 end-start最小
boolean change = i == 0 ? true : false; // 第一个元素要初始化
if (element.degree >= degree) {
change = true;
if (element.degree == degree && (end - start < elementEnd - element.start)) {
change = false;
}
}
if (change) {
start = element.start;
end = elementEnd;
degree = element.degree;
}
}
return end - start + 1;
}
class Element {
int start;
int degree;
public Element(int start, int degree) {
this.start = start;
this.degree = degree;
}
}
Reference
697. Degree of an Array - LeetCode的更多相关文章
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【Leetcode_easy】697. Degree of an Array
problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
- 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 ...
- 697. Degree of an Array 频率最高元素的最小覆盖子数组
[抄题]: Given a non-empty array of non-negative integers nums, the degree of this array is defined as ...
- 697. Degree of an Array@python
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
随机推荐
- 用一个文件,实现迷你 Web 框架
当下网络就如同空气一样在我们的周围,它以无数种方式改变着我们的生活,但要说网络的核心技术变化甚微. 随着开源文化的蓬勃发展,诞生了诸多优秀的开源 Web 框架,让我们的开发变得轻松.但同时也让我们不敢 ...
- 定常系统(时不变系统)和时变系统&& 动态系统和静态系统
根据系统是否含有参数随时间变化的元件,自动控制系统可分为时变系统与定常系统两大类. 定常系统又称为时不变系统,其特点是:系统的自身性质(所研究物体的本质属性例如:质量.转动惯量等)不随时间而变化.具体 ...
- 用纯RUST手撸一个开源流媒体服务(RTMP/HTTPFLV/HLS)XIU
作者工作目前在音视频流媒体行业,用了大概一年的业余时间学习Rust,并且实现了一个简单的音视频流媒体服务,虽然据说Rust已经连续多年被评为最受程序员喜欢的语言,但是在国内还是比较冷门,作者比较看好R ...
- mysql实现in子句的limit查询 (转)
在supesite里面执行一个SQL语句: select * from supe_spaceitems where catid=98 and itemid not in(select itemid f ...
- canvas 1px 出现模糊解决方法及原理
关于canvas绘制1像素出现模糊的原因及解决方法 canvas是html5中非常强大的功能,但是在绘制的时候如果出现1像素,例如画一条1像素的线可能出现模糊情况. 一.解决方法 网上比较常见的解决方 ...
- c++实现职责链模式--财务审批
内容: 某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20 ...
- 在 M1 Mac 上开发 .NET MAUI (iOS)
在 M1 Mac 上开始使用 .NET MAUI 开发 (iOS) 的最简单方法. .NET 多平台应用程序 UI (.NET MAUI) 是一个跨平台框架,用于使用 C# 和 XAML 创建本机移动 ...
- docker下将容器按照端口号分配
问题情境:现在有一个服务器主机,安装了docker,想给成员分配各自的容器,但不想成员通过宿主机进入容器.那么成员如何直接访问容器呢? 成员可以通过ip加端口号访问 因此,需要生成一个容器,将容器的2 ...
- LC-202
编写一个算法来判断一个数 n 是不是快乐数. 「快乐数」 定义为: 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和. 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 ...
- nfs客户端的一次处理
为什么要说这个呢,由于节点环境不一致,导致在重建pod时,我们暂且叫该pod为 cxpod,cxpod所在宿主机出现了问题现象如下:一.cxpod始终处于创建中 ContainerCreating [ ...