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;

    }

}

原文地址:https://www.cnblogs.com/codingyangmao/p/11555980.html

时间: 2024-11-09 05:11:12

697. Degree of an Array - LeetCode的相关文章

697. Degree of an Array

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 de

LeetCode # Array # Easy # 697 Degree of an Array

题意:给定一个数组,数组的度是其中出现最多次数的元素.求,最小连续子数组的度和原数组一致. 思路:(参考最佳答案) 遍历数组,找到数组最大值,然后根据最大值设置三个长度为max+1的数组left[],right[],counts[],分别用于存储一个数第一次出现的索引.最后一次出现的索引.出现次数.然后,根据counts找到数组的度,再根据right-left求出最小的子数组长度. 1 public class Solution { 2 public int findShortestSubArr

leetcode 697.Degree of an Array

题意是指在数组中取得出现频率最高的数,然后计算数之间的间隔的. 那么这里可以简单的使用map映射关系来求解的,并且统计出现的次数以及对应的位置关系的. class Solution { public: int findShortestSubArray(vector<int>& nums) { map<int,int> m; map<int,pair<int,int>> pos; int degree=0,res=INT_MAX; for(int i=

[leetcode]Array-697. Degree of an Array

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 de

leetcode 之 Degree of an Array

1.题目描述 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

Find Minimum in Rotated Sorted Array leetcode

原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) { 4 if (nums.empty()) 5 return -1; 6 int res = find(nums, 0, nums.size()-1);//好神奇,第二个参数无论减或者不减1,都不影响该题的结果 7 if (res == -

Median of Two Sorted Array leetcode java

题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 题解: 首先我们先明确什么是median,即中位数. 引用Wikipedia对中位数的定义: 计算有限个数的数据的中位数的方法是:把所有的同类数据按照大小的顺序排列

Search in Rotated Sorted Array leetcode java

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B a