[抄题]:

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. Javascript的异常捕获机制

    這個異常處理機制,之前也只是,但是沒有怎麼用過,用了一次后發現還可以, 和java,python的異常處理機制也很相似 Javascript的异常捕获机制 1.1 基本的try…catch语句 ES3 ...

  2. 20165222 预备作业3 Linux安装及学习

    一,安装Linux操作系统 一开始下载ubuntu的时候比较麻烦,然后用同学发给我的,才得以安装.根据教程,也没出现什么问题,只是下载的比较麻烦.  二,关于Linux的学习 1,Linux系统简介和 ...

  3. 剑指offer-第五章优化时间和空间效率(把数组排列成最小的数)

    题目:输入一个正整数数组,将所有的数,排列起来,组成一个最小的数.

  4. call、apply、bind用法区别

    call call() 方法调用一个函数, 其具有一个指定的 this 值和分别地提供的参数(参数的列表). <p class="danger"> 注意:该方法的作用和 ...

  5. minio  nginx 配置

    1. 参考配置  server { listen 80; server_name example.com; location / { proxy_set_header Host $http_host; ...

  6. android多渠道打包牛B工具

    http://www.orchidshell.com/ 兰贝壳儿:一个Eclipse插件,为Android开发提供了多渠道打包功能和一些工具类.

  7. mysql + unidac 使用事务例子

    //备注:mysql必须是使用innoDB引擎才支持事务功能,否则以下事务相关代码将失效.//SQL SERVER和Oracle还没试,明天回公司试了再发代码.procedure TForm1.btn ...

  8. Ubuntu-14.04-QT开发环境搭建-(一)

    Ubuntu 14.04 QT 开发环境搭建 一 . 软件:qt-creator-linux-x86-opensource-2.7.0.binqt-everywhere-opensource-src- ...

  9. java显示网格————————

    总结:看图 +---+---+ | | | | | | +---+---+ */ package com.aaa; //在屏幕上显如下网格 public class adga { public sta ...

  10. 杂项-公司-百科:华特·迪士尼-un

    ylbtech-杂项-公司-百科:华特·迪士尼 华特·迪士尼(Walt Disney,全名Walter Elias Disney,又译沃尔特·迪士尼,1901年12月5日—1966年12月15日),出 ...