My Solution using MinHeap JAVA


  • 0
    A

    public class Solution {

    public int thirdMax(int[] nums) {
    
        PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
    
        int max = Integer.MIN_VALUE;
    
        for(int i=0;i<nums.length;i++){
    
            if(max<nums[i])
               max = nums[i];
    
            if(!minHeap.contains(nums[i]))
                minHeap.offer(nums[i]);
                
            if(minHeap.size()>3)
               minHeap.remove();
        }
        
        return minHeap.size()<=2?max:minHeap.peek();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.