自学内容网 自学内容网

LeetCode //C - 414. Third Maximum Number

414. Third Maximum Number

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
 

Example 1:

Input: nums = [3,2,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.

Example 2:

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

Example 3:

Input: nums = [2,2,3,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2’s are counted together since they have the same value).
The third distinct maximum is 1.

Constraints:
  • 1 < = n u m s . l e n g t h < = 1 0 4 1 <= nums.length <= 10^4 1<=nums.length<=104
  • − 2 31 < = n u m s [ i ] < = 2 31 − 1 -2^{31} <= nums[i] <= 2^{31} - 1 231<=nums[i]<=2311

From: LeetCode
Link: 414. Third Maximum Number


Solution:

Ideas:

1. Three Variables for Maximums:

  • We use three variables firstMax, secondMax, and thirdMax initialized to LONG_MIN (the minimum possible value for a long) to store the three distinct maximum values.
  • Using LONG_MIN ensures we can handle the minimum integer value -2^{31} in the array.

2. Iterate Through the Array:

  • For each element in the array, we check if it is equal to any of the current max values (to avoid duplicates).
  • If the current element is greater than firstMax, we update all three max values by shifting them down (i.e., thirdMax = secondMax, secondMax = firstMax, and firstMax = current element).
  • If the current element is greater than secondMax but not greater than firstMax, we update thirdMax and secondMax.
  • If the current element is greater than thirdMax but less than secondMax, we update only thirdMax.

3. Return the Result:

  • If thirdMax is still LONG_MIN, that means there was no third distinct maximum, so we return the value of firstMax (the maximum number).
  • Otherwise, return thirdMax.
Code:
int thirdMax(int* nums, int numsSize) {
    long firstMax = LONG_MIN, secondMax = LONG_MIN, thirdMax = LONG_MIN;

    for (int i = 0; i < numsSize; i++) {
        if (nums[i] == firstMax || nums[i] == secondMax || nums[i] == thirdMax) {
            continue; // Skip duplicates
        }

        if (nums[i] > firstMax) {
            thirdMax = secondMax;
            secondMax = firstMax;
            firstMax = nums[i];
        } else if (nums[i] > secondMax) {
            thirdMax = secondMax;
            secondMax = nums[i];
        } else if (nums[i] > thirdMax) {
            thirdMax = nums[i];
        }
    }

    // If thirdMax is still LONG_MIN, it means there was no third distinct maximum
    return thirdMax == LONG_MIN ? (int)firstMax : (int)thirdMax;
}

原文地址:https://blog.csdn.net/navicheung/article/details/142933558

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!