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 ...
随机推荐
- 深入理解JVM内存回收机制(不包含垃圾收集器)
目录 垃圾回收发生的区域 如何判断对象是否可以被回收 HotSpot实现 垃圾回收算法 JVM中使用的垃圾收集算法 GC的分类 总结 参考资料 垃圾回收发生的区域 堆是java创建对象的区域(Stri ...
- 小白从零开始阿里云部署react项目+node服务接口(二:node服务+web)
我们用极简的方式来创建服务,没有任何附加功能 1 新建一个server文件夹 2 使用npm init 或者yarn init 一路enter 3 yarn add express cors ...
- Ethical Hacking - GAINING ACCESS(4)
SERVER SIDE ATTACKS - METASPLOIT Metasploit is an exploit development and execution tool. It can als ...
- 集训 T3-难题
大致题意: 求方程 \(a_1+a_2+...+a_n=m\) 的非负整数解有几个. 基本思路: 深搜,虽然看起来可能会超时,但是对于100%的数据,0<=n,m<32767,结果< ...
- springboot application.yml配置学习
一.背景 为了更好的使用springboot,所以看一下application.yml配置这块.主要是看数据绑定这块. 主要参考:https://www.hangge.com/blog/cache/d ...
- PyQt5布局管理器
布局分类 绝对定位:使用move方法将空间直接定死在某个坐标,不会随着窗口大小的改变而改变 可变布局:使用各种布局管理器,实现组件的位置和大小随着窗口的变化而变化 布局管理器 QHBoxLayout: ...
- linux nginx 部署多套服务(以react包为例)
前言 今天我特地写下笔记,希望可以完全掌握这个东西,也希望可以帮助到任何想对学习这个东西的同学. 本文用nginx部署服务为主要内容,基于CentOs 7.8系统. 文档版本:1.0.1 更新时间:2 ...
- 清晰架构(Clean Architecture)的Go微服务—重大升级
去年,我创建了一个清晰架构(Clean Architecture)微服务框架,它功能强大,但有些重.我写了一个系列文章来讲述它,请参阅"清晰架构(Clean Architecture)的Go ...
- 简单理解:数据库的一致性与四种隔离级别(+MySQL实现)
并行数据库存在着几种常见不一致问题: 1.更新丢失:两个并发的写进程同时修改某内容,一个没修改完提交之后另一个又提交,导致其覆盖了第一个提交的写进程内容. 2.脏读:一个操作读到了另外一个操作没有提交 ...
- 一个文本框的各项说明,包括类似html的table
https://www.cnblogs.com/zhqiux/archive/2013/09/03/3298654.html