leetcode 414. Third Maximum Number

目录

一、问题分析

二、代码实现

1、排序

2、HashSet去重+小顶堆

3、TreeSet(去重+有序)

4、类似成绩等级划分的方法


 

https://leetcode.com/problems/third-maximum-number/

给定一个非空数组,返回第三大的数字,如果不存在则返回数组的最大数字。

 

一、问题分析

测试用例:

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、排序

class Solution {

    //排序
    public int thirdMax1(int[] nums) {
        Arrays.sort(nums);
        
        if (nums.length < 3) {
            return nums[nums.length - 1];
        }
        
        int curSize = 1;
        for (int i=nums.length-2; i>=0; i--) {
            if (nums[i] == nums[i+1]) {
                continue;
            }
            curSize++;
            if (curSize == 3) {
                return nums[i];
            }
        }
        
        return nums[nums.length - 1];
    }
}

2、HashSet去重+小顶堆

class Solution {

    //小顶堆(利用HashSet去重,利用小顶堆得到topK) O(n)+O(nlog3)
    public int thirdMax3(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        PriorityQueue<Integer> heap = new PriorityQueue<>();        
        int max = Integer.MIN_VALUE;
        for (int e : nums) {
            //去重
            if (set.contains(e)) {
                continue;
            }
            
            set.add(e);
            heap.add(e);
            max = Math.max(e, max);
            
            if (heap.size() > 3) {
                //heap.poll();
                set.remove(heap.poll());
            }
        }
        
        return heap.size()>=3 ? heap.peek() : max;
    }
    
    //小顶堆(利用HashSet去重,利用小顶堆得到topK) O(n)+O(nlog3)
    public int thirdMax2(int[] nums) {
        //去重处理
        HashSet<Integer> set = new HashSet<>();
        for (int e : nums) {
            set.add(e);
        }
        
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        int max = Integer.MIN_VALUE;
        //for (int key : set.keySet()) {
        //error: cannot find symbol  keySet()
        for (int key : set) {
            heap.add(key);
            max = Math.max(key, max);
            
            if (heap.size() > 3) {
                heap.poll();
            }
        }
        
        return heap.size()>=3 ? heap.peek() : max;
    }

}

3、TreeSet(去重+有序)

class Solution {
  
    //TreeSet(利用其去重性、有序性)  O(nlog3)  约等于O(n)
    public int thirdMax4(int[] nums) {
        TreeSet<Integer> set = new TreeSet<>();
        for(int num : nums) {
            set.add(num);
            if(set.size() > 3) {
                set.remove(set.first());
            }
        }
        
        return set.size() < 3 ? set.last() : set.first();
    }
    
}

4、类似成绩等级划分的方法

class Solution {
    
    //划分区间(缺点是无法像动态结构那样容易修改,比如改成第7,第8)
    public int thirdMax5(int[] nums) {
        //int firstMax = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE, thirdMax = Integer.MIN_VALUE;
        //[1,2,-2147483648]  输出2  期待-2147483648
        //Integer firstMax = null, secondMax = null, thirdMax = null;
        long firstMax = Long.MIN_VALUE, secondMax = Long.MIN_VALUE, thirdMax = Long.MIN_VALUE;
        for (int e : nums) {
            if (e > firstMax) {
                thirdMax = secondMax;
                secondMax = firstMax;
                firstMax = e;
            } else if (e > secondMax && e < firstMax) {
                thirdMax = secondMax;
                secondMax = e;
            } else if (e > thirdMax && e < secondMax) {
                thirdMax = e;
            }
        }
        
        return thirdMax == Long.MIN_VALUE ? (int)firstMax : (int)thirdMax;
    }
    
}

 

参考:

https://leetcode.com/problems/third-maximum-number/discuss/90190/Java-PriorityQueue-O(n)-%2B-O(1)

https://leetcode.com/problems/third-maximum-number/discuss/90202/Java-neat-and-easy-understand-solution-O(n)-time-O(1)-space

https://leetcode.com/problems/third-maximum-number/discuss/90209/Short-easy-C%2B%2B-using-set

https://leetcode.com/problems/third-maximum-number/discuss/90240/Short-Clear-C%2B%2B-solution-no-set-or-pq.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值