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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
读题不懂
[一句话思路]:
先统计,再比较
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 数字一般初始化为整数中相反的最大或者最小值,忘了
- 忘了 检查key用的是containsKey方法
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
有很多指标,所以用多维数组+hashmap来实现
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 某数字 最大值的 最小覆盖范围, 有很多指标,所以用多维数组+hashmap来实现
- hashmap可以用for(: .values())冒号表达式把所有值取出来
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
put中直接写数值 不能再赋值,所以new int[]{直接跟数组即可}
class Solution {
public int findShortestSubArray(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return 0;
}
//ini
HashMap<Integer, int[]> map = new HashMap<>();
//collect into hashmap
for (int i = 0; i < nums.length; i++) {
if (!map.containsKey(nums[i])) {
map.put(nums[i], new int[]{1, i, i});//val, first index, last index
}else {
int temp[] = map.get(nums[i]);
temp[0] += 1;
temp[2] = i;
map.put(nums[i], temp);
}
}
//compare
//bigger degree
//same degree but smaller range
//ini to the extreme
int degree = Integer.MIN_VALUE;
int result = Integer.MAX_VALUE;
for (int[] val : map.values()) {
if (val[0] > degree) {
degree = val[0];
result = val[2] - val[1] + 1;
}else {
if (val[0] == degree) {
result = Math.min(result, val[2] - val[1] + 1);
}
}
}
//return
return result;
}
}
697. Degree of an Array 频率最高元素的最小覆盖子数组的更多相关文章
- 【Leetcode_easy】697. Degree of an Array
problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 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】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 the ma ...
- [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@python
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 ...
随机推荐
- rebar安装及创建项目
rebar作为erlang开发中编译,构建,发布,打包,动态升级的常用工具,下面我记录下rebar工具的安装及使用 从源码安装rebar 1. 建立文件 install_rebar.sh 2. 拷贝如 ...
- JS 隔行变色
<script type="text/javascript"> window.onload=function(){ var oUl= ...
- ACM学习历程—TopCoder SRM691 Div2
这是我的第一次打TC,感觉打的一般般吧.不过TC的题目确实挺有意思的. 由于是用客户端打的,所以就不发题目地址了. 300分的题: 这题大意是有一段序列只包含+和数字0~9. 一段序列的操作是,从头扫 ...
- 提示“load System.Core failed”
Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec8 ...
- VS 2015 开发Android底部导航条----[实例代码,多图]
1.废话背景介绍 在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...
- 从如何优化SQL入手,提高数据仓库的ETL效率
1 引言数据仓库建设中的ETL(Extract, Transform, Load)是数据抽取.转换和装载到模型的过程,整个过程基本是通过控制用SQL语句编写的存储过程和函数的方式来实现对 ...
- 设置ubantu的软件源地址
查看所用的源 $ sudo vim /etc/apt/sources.list 由于安装的Ubuntu Server 16.04.1 LTS是英文版的,软件源就默认都是 us.archive.ubun ...
- centos7 & ubuntu14.02安装sublime 3
Centos7安装Sublime Text 3.0正式版 1.安装 GPG 公钥rpm -v --import https://download.sublimetext.com/sublimehq-r ...
- Source Insight中文字体设置
Source Insight是一个面向项目开发的程序编辑器和代码阅读工具,它拥有内置的对C/C++, C#和Java等程序的分析,分析你的源代 码并在你工作的同时动态维护它自己的符号数据库,并自动为你 ...
- git diff的使用
有时候可能睡觉时候忘记关电脑了,然后不小心触碰到键盘上某个神秘的按钮了,然后自己也不知道就提交了 就可能很悲剧 那么有时候不知道自己是否改变了哪些文件的内容 这时候就需要用到git diff git ...