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 ...
随机推荐
- SGU 507 Treediff
这个题目 其实可以暴力 用两个 set 合并: 每次放进去一个元素只要找到这个元素第一个比他大的元素和最后一个比他小的元素:然后更新最优值: 证明为什么不会超时: 假如最后集合的小的为 S1,大 ...
- js错误Cannot set property 'action' of null
Cannot set property 'action' of null [自己解决问题答案] 应该放到form里面 [网上答案]是页面无法加载完毕执行代码.可以把获取元素等一系列的操作放在 wind ...
- UVA11168 Airport
题意 PDF 分析 首先发现距离最短的直线肯定在凸包上面. 然后考虑直线一般方程\(Ax+By+C=0\),点\((x_0,y_0)\)到该直线的距离为 \[ \frac{|Ax_0+By_0+C|} ...
- jmeter --- 测试计划里的元件
1.线程组 线程组元件是任何一个测试计划的开始点.在一个测试计划中的所有元件都必须在某个线程组下.顾名思义,线程组元件控制JMeter执行你的测试计划时候使用的线程数量.对线程组的控制允许你: 设置线 ...
- Oracle记录(三) Scott用户的表结构
在Oracle的学习之中,重点使用的是SQL语句,而所有的SQL语句都要在scott用户下完成,这个用户下一共有四张表,可以使用: SELECT * FROM tab; 查看所有的数据表的名称,如果现 ...
- Internet上的网络层
TCP/IP协议栈第三层是网络层,网络层的目的是实现两个系统之间的数据透明传送,具体功能包括寻址和路由选择.连接和建立.保持和终止等. TCP/IP协议给internet上的每台主机和路由分配一个地址 ...
- 【转】配置Jmeter的自定义参数
配置Jmeter的自定义参数 User Defined Variables 在这个控件中,定义你所需要的参数,如 在对应的需要使用参数的位置,使用${host}替代. 应用场景: 当测试环境变化时,我 ...
- appium控制Android按键
按键的keycode,如下: KEYCODE_HOME Home键 3 KEYCODE_BACK 返回键 4 KEYCODE_CALL 拨号键 5 KEYCODE_ENDCALL 挂机键 6 KEYC ...
- 高并发场景下System.currentTimeMillis()的性能问题的优化 以及SnowFlakeIdWorker高性能ID生成器
package xxx; import java.sql.Timestamp; import java.util.concurrent.*; import java.util.concurrent.a ...
- python's sixth day for me
---恢复内容开始--- # == 比较的是数值 a = 1000 b = 1000 print(a == b) #True # is 比较的是内存地址 >>> a = 100 ...