[抄题]:

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]:

[思维问题]:

读题不懂

[一句话思路]:

先统计,再比较

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 数字一般初始化为整数中相反的最大或者最小值,忘了
  2. 忘了 检查key用的是containsKey方法

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

有很多指标,所以用多维数组+hashmap来实现

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 某数字 最大值的 最小覆盖范围, 有很多指标,所以用多维数组+hashmap来实现
  2. 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 频率最高元素的最小覆盖子数组的更多相关文章

  1. 【Leetcode_easy】697. Degree of an Array

    problem 697. Degree of an Array 题意:首先是原数组的度,其次是和原数组具有相同的度的最短子数组.那么最短子数组就相当于子数组的首末数字都是统计度的数字. solutio ...

  2. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  3. 697. Degree of an Array - LeetCode

    697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...

  4. 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 ...

  5. 【LeetCode】697. Degree of an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. phpcms v9 tags调用方法

    {loop $keywords $keyword} <a href="{APP_PATH}index.php?m=content&c=tag&catid={$catid ...

  2. 深入理解java虚拟机-第八章

    第8章 虚拟机字节码执行引擎 8.2 运行时栈帧结构 栈帧(Stack Frame)是用于支持虚拟机进行方法调用和方法执行的数据结构. 每一个栈帧包括了局部变量表.操作数栈.动态连接.方法返回地址和一 ...

  3. Cloudera Manager (centos)安装详细介绍

    文章全部来自:Cloudera Manager (centos)安装详细介绍http://www.aboutyun.com/thread-9190-1-1.html(出处: about云开发) 这里已 ...

  4. Java Web 高性能开发,前端的高性能

    Java Web 高性能开发,第 2 部分: 前端的高性能 Web 发展的速度让许多人叹为观止,层出不穷的组件.技术,只需要合理的组合.恰当的设置,就可以让 Web 程序性能不断飞跃.Web 的思想是 ...

  5. 分类(类别/Category)与 类扩展(Extension)

    一.分类(类别/Category) 1.适用范围      当你已经封装好了一个类(也可能是系统类.第三方库),不想在改动这个类了,可是随着程序功能的增加需要在类中增加一个方法,这时我们不必修改主类, ...

  6. Excel 2007 打开 UTF-8 编码 CSV 文件的乱码BUG

    http://blog.sina.com.cn/s/blog_6c3b65fd01018dgq.html 打开UTF-8编码的CSV方法: 1) 打开Excel 2007 2) 执行“数据”-> ...

  7. 创建工程常量 (OC中的宏定义)

    1.oc创建宏 文件 2.swift创建 常量文件 在swift中, 并非是预编译代码替换, 而是设置全局常量, 简单宏, 直接let 加常量名即可

  8. Tomcat下WebSocket最大连接数测试

    WebSocket现在很常用,想要测试tomcat的最大连接数,今天试了一个可行的办法和配置(之前是用全公司的设备一起来测试的,真机环境的测试收到网络的影响很大,其实真实环境应用中,网络才是webso ...

  9. mina中责任链模式的实现

    一.mina的框架回顾 责任链模式在mina中有重要的作用,其中Filter机制就是基于责任链实现的. 从上图看到消息的接受从IoService层先经过Filter层过滤处理后最后交给IoHander ...

  10. Mongodb时间问题

    Java保存到mongodb当前时间,使用RoboMongo查看数据显示时间比当前时间少8个小时,这是客户端的问题. MongoDB中的Date类型数据只保存绝对时间值,不保存时区信息,因此“显示的时 ...