C#LeetCode刷题之#697-数组的度( Degree of an Array)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3738 访问。
给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。
你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。
输入: [1, 2, 2, 3, 1]
输出: 2
解释:
输入数组的度是2,因为元素1和2的出现频数最大,均为2.
连续子数组里面拥有相同度的有如下所示:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
最短连续子数组[2, 2]的长度为2,所以返回2.
输入: [1,2,2,3,1,4,2]
输出: 6
注意:
nums.length 在1到50,000区间范围内。
nums[i] 是一个在0到49,999范围内的整数。
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.
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.
Input: [1,2,2,3,1,4,2]
Output: 6
Note:
nums.length will be between 1 and 50,000.
nums[i] will be an integer between 0 and 49,999.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3738 访问。
public class Statistics {
//统计频次
public int Count { get; set; }
//开始索引
public int StartIndex { get; set; }
//结束索引
public int EndIndex { get; set; }
}
public class Program {
public static void Main(string[] args) {
int[] nums = null;
nums = new int[] { 1, 2, 2, 3, 1 };
var res = FindShortestSubArray(nums);
Console.WriteLine(res);
Console.ReadKey();
}
private static int FindShortestSubArray(int[] nums) {
//哈希存放统计数据
var dic = new Dictionary<int, Statistics>();
for(int i = 0; i < nums.Length; i++) {
//如果有键,频次+1,记录结束索引
if(dic.ContainsKey(nums[i])) {
dic[nums[i]].Count++;
dic[nums[i]].EndIndex = i;
} else {
//如果没有键,赋初始值
dic.Add(nums[i], new Statistics {
Count = 1,
StartIndex = i,
EndIndex = i
});
}
}
//降序排列
var order = dic.OrderByDescending(s => s.Value.Count).ToList();
//记录数组的度
var maxCount = order[0].Value.Count;
//记录最小长度
var minLength = int.MaxValue;
//先筛选出所有与数组的度相同的统计结果
order.Where(s => s.Value.Count == maxCount)
//转换成List
.ToList()
//遍历所有List中的数据找出最小Length
.ForEach(s => minLength = Math.Min(
minLength,
s.Value.EndIndex - s.Value.StartIndex + 1
));
//返回最小Length
return minLength;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3738 访问。
2
分析:
以上解法在最坏的情况下,var order = dic.OrderByDescending(s => s.Value.Count).ToList() 执行所消耗的时间最多,因为原数组可能没有重复的元素。若OrderByDescending基于比较的先进算法,那么以上解法的时间复杂度为: ;若基于空间换时间的基数、计数或桶排序,那么以上解法的时间复杂度为:
。
C#LeetCode刷题之#697-数组的度( Degree of an Array)的更多相关文章
- C#LeetCode刷题-树状数组
树状数组篇 # 题名 刷题 通过率 难度 218 天际线问题 32.7% 困难 307 区域和检索 - 数组可修改 42.3% 中等 315 计算右侧小于当前元素的个数 31.9% 困难 ...
- [Swift]LeetCode697. 数组的度 | Degree of an Array
Given a non-empty array of non-negative integers nums, the degreeof this array is defined as the max ...
- leetCode刷题(找出数组里的两项相加等于定值)
最近被算法虐了一下,刷一下leetcode,找找存在感 如题: Given an array of integers, return indices of the two numbers such t ...
- C#LeetCode刷题之#643-子数组最大平均数 I( Maximum Average Subarray I)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3728 访问. 给定 n 个整数,找出平均数最大且长度为 k 的连 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
随机推荐
- Spring Bean前置后置处理器的使用
Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...
- OSCP Learning Notes - WebApp Exploitation(1)
Installing XSS&MySQL FILE Download the Pentester Lab: XSS and MySQL FILE from the following webs ...
- 用Tableau制作官网流量周报
好久没写博客了,上班摸鱼时间分享一下在工作中做的东西吧,先上图. 数据方面取自百度统计,身处传统类型公司,官网没有数据库,只好将就一下啦,反正是免费的,体验也还可以. 关于百度统计注册.添加管理站点和 ...
- JAVA四大名著(程序员必读)
JAVA四大名著: <JAVA语言程序设计(第4版)>-----初级 <JAVA核心技术>卷I----初级.<JAVA核心技术>卷II----中级 <JAVA ...
- 阿里云如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器?
阿里云如何使用二次验证码/虚拟MFA/两步验证/谷歌验证器? 见如上链接中视频
- python-多任务编程03-迭代器(iterator)
迭代器是一个可以记住遍历的位置的对象.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 可迭代对象(Iterable) 能够被循环遍历(迭代)的对象称为可迭代 ...
- flask中url_for使用endpoint和视图函数名
在flask中,使用url_for 进行路由反转时,需要传递一个endpoint的值,用法如下: @app.route('/', endpoint='my_index') def index(): r ...
- Mysql 的数据导入导出
一. mysqldump工具基本用法,不适用于大数据备份 1. 备份所有数据库: mysqldump -u root -p --all-databases > all_database_sq ...
- 如何系统地学习Excel?
人在江湖,生不由己.人在职场,Excel必备. 为了帮助更多人快速掌握职场必备技能Excel,我写了一个免费的教程<职场Excel>,能帮助你解决99%职场中遇到的问题. 对,你没看错,是 ...
- mac下高效安装 homebrew 及完美避坑姿势 (亲测有效)
世上无难事,只要找到 Homebrew 的正确安装方式. Homebrew 是什么 Homebrew是 mac的包管理器,仅需执行相应的命令,就能下载安装需要的软件包,可以省掉自己去下载.解压.拖拽( ...