LeetCode 414. Third Maximum Number. C++

本文介绍了一种在O(n)时间复杂度内找到数组中第三大数的算法,使用三个变量跟踪最大、次大和第三大的数,同时避免了重复元素的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

414. Third Maximum Number

题目

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.

解法

用m1、m2、m3分别来存放最大、第二大、第三大,遍历数组,比较大小。注意这里需要剔除重复元素,如果遇到m1、m2、m3中已经有的元素就不需要再比较大小了,直接下一个。

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long long m=LLONG_MIN;
        long long m1=m,m2=m,m3=m;
        for(int i=0;i<nums.size();i++){
            if(nums[i]==m1 || nums[i]==m2 || nums[i]==m3) continue;
            if(nums[i]>m1){
                m3=m2;
                m2=m1;
                m1=nums[i];
            }
            else if(nums[i]>m2){
                m3=m2;
                m2=nums[i];
            }
            else if(nums[i]>m3){
                m3=nums[i];
            }
        }
        if(m3==m) return m1;
        else return m3;
    }
};

经验

第一次写的时候用的是int

int m=INT_MIN;

结果,有一条测试没通过,哈哈哈。预期是-2147483648,结果输出是2,因为INT_MIN被当成重复元素剔除了。

[1,2,-2147483648]

改成long或者long long(C++11)就好了。

C. Need More Arrays time limit per test2 seconds memory limit per test256 megabytes Given an array a and n integers. It is sorted in non-decreasing order, that is, ai≤ai+1 for all 1≤i<n . You can remove any number of elements from the array (including the option of not removing any at all) without changing the order of the remaining elements. After the removals, the following will occur: a1 is written to a new array; if a1+1<a2 , then a2 is written to a new array; otherwise, a2 is written to the same array as a1 ; if a2+1<a3 , then a3 is written to a new array; otherwise, a3 is written to the same array as a2 ; ⋯ For example, if a=[1,2,4,6] , then: a1=1 is written to the new array, resulting in arrays: [1] ; a1+1=2 , so a2=2 is added to the existing array, resulting in arrays: [1,2] ; a2+1=3 , so a3=4 is written to a new array, resulting in arrays: [1,2] and [4] ; a3+1=5 , so a4=6 is written to a new array, resulting in arrays: [1,2] , [4] , and [6] . Your task is to remove elements in such a way that the described algorithm creates as many arrays as possible. If you remove all elements from the array, no new arrays will be created. Input The first line of input contains one integer t (1≤t≤104 ) — the number of test cases. The first line of each test case contains one integer n (1≤n≤2⋅105 ) — the length of the array. The second line of each test case contains n integers a1,a2,…,an (1≤ai≤106 , ai≤ai+1 ) — the elements of the array. It is guaranteed that the sum of n across all test cases does not exceed 2⋅105 . Output For each test case, output one integer — the maximum number of arrays that can be obtained by removing any (possibly zero) number of elements. Example InputCopy 6 6 1 2 3 4 5 6 3 1 2 3 4 1 2 2 4 1 2 3 1 4 8 2 1 1 OutputCopy 3 2 2 1 3 1 Note In the first example, you can remove a3 and a5 , then a=[1,2,4,6] , the process of forming arrays for it is shown in the statement. In the second example, you need to remove a2 , after which a=[1,3] , and the arrays [1] and [3] will be written. In the third example, no removals are needed; for a=[1,2,2,4] , the arrays [1,2,2] and [4] will be written.c++
05-27
B. Array Recoloring time limit per test2 seconds memory limit per test256 megabytes You are given an integer array a of size n . Initially, all elements of the array are colored red. You have to choose exactly k elements of the array and paint them blue. Then, while there is at least one red element, you have to select any red element with a blue neighbor and make it blue. The cost of painting the array is defined as the sum of the first k chosen elements and the last painted element. Your task is to calculate the maximum possible cost of painting for the given array. Input The first line contains a single integer t (1≤t≤103 ) — the number of test cases. The first line of each test case contains two integers n and k (2≤n≤5000 ; 1≤k<n ). The second line contains n integers a1,a2,…,an (1≤ai≤109 ). Additional constraint on the input: the sum of n over all test cases doesn't exceed 5000 . Output For each test case, print a single integer — the maximum possible cost of painting for the given array. Example InputCopy 3 3 1 1 2 3 5 2 4 2 3 1 3 4 3 2 2 2 2 OutputCopy 5 10 8 Note In the first example, you can initially color the 2 -nd element, and then color the elements in the order 1,3 . Then the cost of painting is equal to 2+3=5 . In the second example, you can initially color the elements 1 and 5 , and then color the elements in the order 2,4,3 . Then the cost of painting is equal to 4+3+3=10 . In the third example, you can initially color the elements 2,3,4 , and then color the 1 -st element. Then the cost of painting is equal to 2+2+2+2=8 . 用cpp解决
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值