问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. C#LeetCode刷题-树状数组

    树状数组篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 ...

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

  3. leetCode刷题(找出数组里的两项相加等于定值)

    最近被算法虐了一下,刷一下leetcode,找找存在感 如题: Given an array of integers, return indices of the two numbers such t ...

  4. C#LeetCode刷题之#643-子数组最大平均数 I( Maximum Average Subarray I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3728 访问. 给定 n 个整数,找出平均数最大且长度为 k 的连 ...

  5. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  6. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  8. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  9. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

随机推荐

  1. ModuleNotFoundError: No module named 'phkit.pinyin'

    1 产生背景 在mac系统本地使用正常,在linux系统上phkit包缺少相应的python文件 2 解决方案 自己想出来,手动上传本地相关python代码到linux服务器 3 解决过程 首先通过项 ...

  2. 使用PowerShell自动编译部署前端

    前言 最近在开发一套管理系统,做了前后端分离. 后台使用的是Asp.Net Core 3.1 前端使用的是Vue+Ant Design 自己搞了一台云服务器,打算把系统部署到云服务器上.以供外网访问. ...

  3. Goorm永久免费的VPS

    简介 咱在LOC(某知名主机论坛)看到的,当个容器玩玩还是不错的,不过10分钟后会休眠,目前还没有大佬可以解决,可以使用SSH连接,适合折腾,不适合建站等生产环境操作,请注意.https://www. ...

  4. 【转载】基于dom的一些前端漏洞

    最直接的xss --dom xss function trackSearch(query) { document.write('<img src="/resources/images/ ...

  5. es6 Proxy简单使用

    es6的Proxy是什么? 可以理解为,是在访问对象前的一层拦截.只要访问的该对象,就要通过这个一层拦截.这一层的拦截,可以进行数据的过滤和更改 比如下面这个 var p = new Proxy({} ...

  6. Java线程的6种状态及切换

    Java中线程的状态分为6种. 1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法.2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running) ...

  7. spring学习(八)事务操作

    一.事务的概念: 事务是并发控制的单位,一系列操作组成的工作单元,该工作单元内的操作是不可分割的,也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做,所有操作必须成功完成,否则 ...

  8. 4.pandas的进阶查询

    简单的查询其实根本不能满足实际开发的需求 需求可能是让你查一下2018年的销售额啊,2019年温度超过30℃的天数啊等等的 这些需求都是有异曲同工的,就是带条件的查询 这里我们先自己设计一个表格,并将 ...

  9. Python定位模块_PYTHONPATH变量

    Python定位模块: 导入模块时,系统会根据搜索路径进行寻找模块: 1.在程序当前目录下寻找该模块 2.在环境变量 PYTHONPATH 中指定的路径列表寻找 3.在 Python 安装路径中寻找 ...

  10. PHP jdtounix() 函数

    ------------恢复内容开始------------ 实例 把格利高里历法的日期转换为儒略日计数,然后把儒略日计数转换为 Unix 时间戳: <?php$jd=gregoriantojd ...