Java Best Clean Solution 0ms Time O(n) Space O(1) using counting number


  • 0
    S
    //Solution 1: using counting number to deal with less than 3 max numbers
    public int thirdMax(int[] nums) {
        int[] max = new int[3];//max[2] is third max number, max[0] is first max number
        max[0] = max[1] = max[2] = Integer.MIN_VALUE;
        int count = 0;
        
        for(int n : nums) {
            if(n == max[0] || n == max[1]) continue;//skip duplicate first and second max numbers
            
            if(n >= max[2]) {
                count++;
                max[2] = n;
                if(max[2] > max[1]) {
                    swap(max, 2, 1);
                    if(max[1] > max[0]) {
                        swap(max, 1, 0);
                    }
                }
            }
        }
        
        return count >= 3 ? max[2] : max[0];
    }
    
    private void swap(int[] res, int a, int b) {
        int temp = res[b];
        res[b] = res[a];
        res[a] = temp;
    }
    
    
    
    //Solution 2: using null to deal with less than 3 max numbers
    public int thirdMax(int[] nums) {
        Integer[] max = new Integer[3];
        max[0] = max[1] = max[2] = null;
        
        for(int n : nums) {
            if((max[0] != null && n == max[0]) 
            		|| (max[1] != null && n == max[1])) continue;
            
            if(max[2] == (Integer) null || n >= max[2]) {
                max[2] = n;
                if(max[1] == (Integer) null || max[2] > max[1]) {
                    swap(max, 2, 1);
                    if(max[0] == (Integer) null ||max[1] > max[0]) {
                        swap(max, 1, 0);
                    }
                }
            }
        }
        
        return max[2] == null ? max[0] : max[2];
    }
    
    private void swap(Integer[] max, int a, int b) {
        Integer temp = max[b];
        max[b] = max[a];
        max[a] = temp;
    }

  • 0
    S

    Hi, friends, compared with solution 1, which one is better? What's your suggestion?

    //Solution 2: using null to deal with less than 3 max numbers
    public int thirdMax(int[] nums) {
        Integer[] max = new Integer[3];
        max[0] = max[1] = max[2] = null;
        
        for(int n : nums) {
            if((max[0] != null && n == max[0]) 
            		|| (max[1] != null && n == max[1])) continue;
            
            if(max[2] == (Integer) null || n >= max[2]) {
                max[2] = n;
                if(max[1] == (Integer) null || max[2] > max[1]) {
                    swap(max, 2, 1);
                    if(max[0] == (Integer) null ||max[1] > max[0]) {
                        swap(max, 1, 0);
                    }
                }
            }
        }
        
        return max[2] == null ? max[0] : max[2];
    }
    
    private void swap(Integer[] max, int a, int b) {
        Integer temp = max[b];
        max[b] = max[a];
        max[a] = temp;
    }

Log in to reply
 

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