问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3710 访问。

给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。

输入: [3, 2, 1]

输出: 1

解释: 第三大的数是 1.

输入: [1, 2]

输出: 2

解释: 第三大的数不存在, 所以返回最大的数 2 .

输入: [2, 2, 3, 1]

输出: 1

解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。

存在两个值为2的数,它们都排第二。


Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).

Input: [3, 2, 1]

Output: 1

Explanation: The third maximum is 1.

Input: [1, 2]

Output: 2

Explanation: The third maximum does not exist, so the maximum (2) is returned instead.

Input: [2, 2, 3, 1]

Output: 1

Explanation: Note that the third maximum here means the third maximum distinct number.

Both numbers with value 2 are both considered as second maximum.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3710 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 3, 2, 1 };
var res = ThirdMax(nums);
Console.WriteLine(res); nums = new int[] { 3, 2, 1 };
res = ThirdMax2(nums);
Console.WriteLine(res); Console.ReadKey();
} private static int ThirdMax(int[] nums) {
Array.Sort(nums);
int count = 0;
for(int i = nums.Length - 1; i >= 1; i--) {
if(nums[i] != nums[i - 1]) count++;
if(count == 2) {
return nums[i - 1];
}
}
return nums[nums.Length - 1];
} private static int ThirdMax2(int[] nums) {
var sorted = new SortedSet<int>();
for(int i = 0; i < nums.Length; i++) {
sorted.Add(nums[i]);
if(sorted.Count > 3) {
sorted.Remove(sorted.ElementAt(0));
}
}
return sorted.Count == 3 ? sorted.ElementAt(0) : sorted.ElementAt(sorted.Count - 1);
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3710 访问。

1
1

分析:

ThirdMax的时间复杂度取决于 Array.Sort() 方法所使用的排序算法:

  • 若使用基于比较的先进排序算法,其时间复杂度为:  ,加上一个线性的分析过程,其时间复杂度仍为:  ;
  • 若不使用基于比较的算法,而是使用计数、基数、桶排序等空间换时间的线性排序算法,那么ThirdMax的时间复杂度为2倍的线性,结果仍然为线性,即为:  。

ThirdMax2的时间复杂度不能直接认定是  ,因为使用了不重复的有序集合,每次添加数据都会导致sorted被重新排序。但是因为sorted中最多只包含3个数字(到达4时被删除1个),所以即便使用了糟糕的  的冒泡,其复杂度也仅为常数 9,即为常量时间内完成排序。在这种情况下ThirdMax2的时间复杂度应该为9倍的线性,其结果仍然为线性,即为:  。

具体的时间复杂度的分析应该具体对待,如果把冒泡的内循环封装成一个方法,便认为冒泡是  那就贻笑大方了。

C#LeetCode刷题之#414-第三大的数(Third Maximum Number)的更多相关文章

  1. LeetCode 414. 第三大的数(Third Maximum Number) 3

    414. 第三大的数 414. Third Maximum Number 题目描述 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是 O(n). 每 ...

  2. [Swift]LeetCode414. 第三大的数 | Third Maximum Number

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

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

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

  4. C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...

  5. C#LeetCode刷题之#16-最接近的三数之和(3Sum Closest)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3620 访问. 给定一个包括 n 个整数的 ...

  6. C#LeetCode刷题-数组

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

  7. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  8. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  9. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

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

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

随机推荐

  1. 随笔java面试基础

    转:http://blog.csdn.net/wenwen360360/article/details/54969418 Application ―Java应用程序”是可以独立运行的Java程序.由J ...

  2. Ethical Hacking - GAINING ACCESS(2)

    Server Side Attacks - INFORMATION GATHERING Need an IP address. Very simple if target is on the same ...

  3. 2020年20个Flutter最漂亮的UI库和项目

    Best-Flutter-UI-Templates 地址:https://github.com/mitesh77/Best-Flutter-UI-Templates The History of Ev ...

  4. 题解 UVA501 【Black Box】

    思路与中位数一题,解决方案比较像,使用对顶堆来解决. 具体实现为,使用两个堆,大根堆维护较小的值,小根堆维护较大的值,即小根堆的堆顶是较大的数中最小的,大根堆的堆顶是较小的数中最大的. 将大于大根堆堆 ...

  5. ajax快速入门

    一.ajax简单入门 1.Ajax的实现步骤 // 1.创建ajax对象var xhr = new XMLHttpRequest();// 2.高数ajax请求地址及请求方式//第一个参数就是请求方式 ...

  6. ASP.NET WebAPI2复杂请求跨域设置

    ASP.Net Core的跨域设置比较简单  官方都整合了 具体的参见微软官方文档: https://docs.microsoft.com/zh-cn/aspnet/core/security/cor ...

  7. 微型计算机系统实验总结(学习性实验:IO地址译码,可编程并行接口8255,交通灯控制实验 + 自主设计实验:汽车信号灯控制系统,电风扇控制器,洗衣机控制系统,霓虹灯,电梯控制系统)

    实验配套软件: https://download.csdn.net/download/qq_39932172/11221584 实验指导用书: 教师版: https://download.csdn.n ...

  8. Alink漫谈(十四) :多层感知机 之 总体架构

    Alink漫谈(十四) :多层感知机 之 总体架构 目录 Alink漫谈(十四) :多层感知机 之 总体架构 0x00 摘要 0x01 背景概念 1.1 前馈神经网络 1.2 反向传播 1.3 代价函 ...

  9. 浅析MySQL中change与modify的区别

    MySQL版本 show variables like 'version'; 表结构 desc student; 修改表 例如:修改表student的name字段,将varchar(10)修改为var ...

  10. java并发包提供的三种常用并发队列实现

    java并发包中提供了三个常用的并发队列实现,分别是:ConcurrentLinkedQueue.LinkedBlockingQueue和ArrayBlockingQueue. ConcurrentL ...