697. Degree of an Array - LeetCode

Posted codingyangmao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了697. Degree of an Array - LeetCode相关的知识,希望对你有一定的参考价值。

Description:

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.

Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.

Example 1:

Input: [1, 2, 2, 3, 1]
Output: 2
Explanation: 
The input array has a degree of 2 because both elements 1 and 2 appear twice.
Of the subarrays that have the same degree:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
The shortest length is 2. So return 2.

 

Example 2:

Input: [1,2,2,3,1,4,2]
Output: 6

 

Note:

  • nums.length will be between 1 and 50,000.
  • nums[i] will be an integer between 0 and 49,999.

 

Accepted
58,524
Submissions
113,812

Solution:

First Attempt: All test cases passed, but time limit exceeded. 

技术图片

 

 

class Solution 
    public int findShortestSubArray(int[] nums) 
     
        int start = 0; 
        int end = 0; 
        
        int max = Integer.MIN_VALUE;
        int val = 0;
                
         int len = Integer.MAX_VALUE; 
        
        
        int prev= Integer.MIN_VALUE;
        int prev2= Integer.MIN_VALUE;
        
        for(int i =0; i<nums.length; i++)
            
            if(prev2!=nums[i]&&Count(nums, nums[i])>max)
                
                max = Count(nums, nums[i]);
                  val = nums[i]; 
            
            
            prev2= nums[i];
        
        
       // System.out.println("val = "+ val+ " max = "+ max);
        
        for(int i = 0; i<nums.length; i++)
            
            if(nums[i]!=prev && Count(nums, nums[i])==max)
            int tmp1_start = 0; 
            int tmp2_end = 0;
                for(int j = 0; j<nums.length; j++)
                    
                    if(nums[j]==nums[i])
                        tmp1_start = j;   
                        break;
                    
                    // System.out.println(nums[i]+" "+ "j = "+ j);
                  
                
                
                for(int k = nums.length-1; k>=0; k--)
                    if(nums[k]==nums[i])
                        tmp2_end = k;
                        break;
                    
                     //System.out.println("  k  "+ k );
                    
                
                
                if((tmp2_end - tmp1_start)+1 <len)
                    len = (tmp2_end - tmp1_start)+1; 
                
            
            prev = nums[i];
        
        return len;
    
    
    static int Count(int[] nums, int cur)
        int count = 0; 
        for(int i = 0; i < nums.length; i++)
            
            if(nums[i] == cur)
                count++;
            
        
        
        return count;

    
    

 

 

以上是关于697. Degree of an Array - LeetCode的主要内容,如果未能解决你的问题,请参考以下文章

697. Degree of an Array

697. Degree of an Array

697. Degree of an Array

[leetcode]Array-697. Degree of an Array

leetcode-697-Degree of an Array

697. Degree of an Array