LeetCode 414. Third Maximum Number

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

v2-601172630e8ee8609854f8bb50981440_1440w.jpg

Description



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).


Example 1:


Input: [3, 2, 1]
Output: 1
Explanation: The third maximum is 1.


Example 2:


Input: [1, 2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.


Example 3:


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.


描述



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


示例 1:


输入: [3, 2, 1]
输出: 1
解释: 第三大的数是 1.


示例 2:


输入: [1, 2]
输出: 2
解释: 第三大的数不存在, 所以返回最大的数 2 .


示例 3:


输入: [2, 2, 3, 1]
输出: 1
解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。
存在两个值为2的数,它们都排第二。


来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/third-maximum-number

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


思路



  • 使用一个有 3 个元素的 bucket,从大到小排序,初始化为最小值。
  • 遍历数组 nums,把数组中的数 num 与 bucket 中的数字 t 比较,如果 num 大于当前位置的数 t ,则 bucket 此位置的数(包括)向后移动,把 num 放到此位置。
  • 如果 num 已经出现在了 bucket 中,则跳过此数字。
  • 最后 bucket 的最后一个数就是所求。如果最后的一个数依然是 最小值,说明给定的数组中没有第三小的数。返回最大的数。


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-10-13 20:27:38
# @Last Modified by:   何睿
# @Last Modified time: 2019-10-13 20:55:02
from typing import List
class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        bucket = [float("-inf"), float("-inf"), float("-inf")]
        for num in nums:
            for i in range(3):
                if num > bucket[i]:
                    self._update(i, num, bucket)
                    break
                if num == bucket[i]:
                    break
        return bucket[-1] if bucket[-1] != float("-inf") else bucket[0]
    def _update(self, i, num, bucket):
        if i == 0:
            bucket[1], bucket[2] = bucket[0], bucket[1]
            bucket[0] = num
        elif i == 1:
            bucket[2] = bucket[1]
            bucket[1] = num
        elif i == 2:
            bucket[2] = num

源代码文件在 这里


目录
相关文章
|
5月前
Leetcode 53.Maximum Subarray
题意简单,给出一个数组,求出其中最大的子数组和。 这种简单题目背后蕴藏着很巧妙的解题方法。其实只需要遍历一次数组就可以求得解。 思路是这样的,你想想看,如果一段子数组的和是负数, 那么这一段子数组不可能是最大和数组的一部分,丢掉重新从下一个位置开始选。
27 0
|
5月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
64 1
|
5月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
21 0
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
|
30天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3