HomeLeetcode414. Third Maximum Number - Leetcode Solutions

414. Third Maximum Number – Leetcode Solutions

Description:

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.

Examples:

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.

Solution in Python:

  1. Remove Duplicates: First, we need to remove duplicate values from the array to ensure we only work with distinct values.
  2. Sort the Array: Sort the array in descending order.
  3. Check the Length: Check if the length of the resulting array is at least 3. If it is, return the third maximum value. If not, return the maximum value.
Python
class Solution:
    def thirdMax(self, nums: List[int]) -> int:
        # Step 1: Remove duplicates by converting the list to a set
        nums = list(set(nums))
        
        # Step 2: Sort the list in descending order
        nums.sort(reverse=True)
        
        # Step 3: Check if there are at least 3 distinct numbers
        if len(nums) >= 3:
            # Return the third distinct maximum number
            return nums[2]
        else:
            # Return the maximum number (first element in the sorted list)
            return nums[0]

Solution in Javascript:

JavaScript
/**
 * @param {number[]} nums
 * @return {number}
 */
var thirdMax = function(nums) {
    // Step 1: Remove duplicates by converting the array to a Set and back to an array
    nums = [...new Set(nums)];
    
    // Step 2: Sort the array in descending order
    nums.sort((a, b) => b - a);
    
    // Step 3: Check if there are at least 3 distinct numbers
    if (nums.length >= 3) {
        // Return the third distinct maximum number
        return nums[2];
    } else {
        // Return the maximum number (first element in the sorted array)
        return nums[0];
    }
};

Solution in Java:

Java
import java.util.*;

class Solution {
    public int thirdMax(int[] nums) {
        // Step 1: Use a HashSet to remove duplicates
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        
        // Step 2: Convert the set to a list and sort it in descending order
        List<Integer> sortedList = new ArrayList<>(set);
        Collections.sort(sortedList, Collections.reverseOrder());
        
        // Step 3: Check if there are at least 3 distinct numbers
        if (sortedList.size() >= 3) {
            // Return the third distinct maximum number
            return sortedList.get(2);
        } else {
            // Return the maximum number (first element in the sorted list)
            return sortedList.get(0);
        }
    }
}

Solution in C#:

C#
using System;
using System.Collections.Generic;

public class Solution {
    public int ThirdMax(int[] nums) {
        // Step 1: Use a HashSet to remove duplicates
        HashSet<int> set = new HashSet<int>(nums);
        
        // Step 2: Convert the set to a list and sort it in descending order
        List<int> sortedList = new List<int>(set);
        sortedList.Sort((a, b) => b.CompareTo(a));
        
        // Step 3: Check if there are at least 3 distinct numbers
        if (sortedList.Count >= 3) {
            // Return the third distinct maximum number
            return sortedList[2];
        } else {
            // Return the maximum number (first element in the sorted list)
            return sortedList[0];
        }
    }
}

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments

Popular