题目描述

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.

解法一

思路

既然要找出最短的子串使得其包含出现次数最多的元素,那么首先就是确定元素最多出现的次数,遍历一次就好了。接下来注意题目只要求包含任一出现次数最多元素的子串,所以对于有多个出现次数最多元素的情况,只要找到最短的、包含全部任一出现次数最多元素(抱歉这句话太绕)就好了。

给定一个值,求包含全部与给定值相等的元素的最短子串该怎么求?其实很简单,记录下第一次出现和最后一次出现的位置就好了,两者相减就是最短长度,为什么?因为不会再有子串比它更短了(否则就是未包含全部元素)。对于有多个出现次数最多元素的情况,只需要找出这些元素的最短子串中最小的就好了。

Python

class Solution:
    def findShortestSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        counts = {}
        starts = {}
        ends = {}
        max_count = -sys.maxsize
        for i, num in enumerate(nums):
            if num not in starts:
                starts[num] = i
                counts[num] = 0
            counts[num] += 1
            ends[num] = i
            max_count = max(max_count, counts[num])
        result = sys.maxsize
        for num, count in counts.items():
            if count == max_count:
                result = min(result, ends[num] - starts[num] + 1)
        return result

Java

class Solution {
    public int findShortestSubArray(int[] nums) {
        Map<Integer, Integer> counts = new HashMap<>();
        Map<Integer, Integer> starts = new HashMap<>();
        Map<Integer, Integer> ends = new HashMap<>();
        int maxCount = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            if (!starts.containsKey(num)) {
                starts.put(num, i);
                counts.put(num, 0);
            }
            counts.put(num, counts.get(num) + 1);
            ends.put(num, i);
            maxCount = Math.max(maxCount, counts.get(num));
        }
        int result = Integer.MAX_VALUE;
        for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
            if (entry.getValue() == maxCount) {
                result = Math.min(result, ends.get(entry.getKey()) - starts.get(entry.getKey()) + 1);
            }
        }
        return result;
    }
}

C++

class Solution {
public:
    int findShortestSubArray(vector<int> &nums) {
        unordered_map<int, int> counts;
        unordered_map<int, int> starts;
        unordered_map<int, int> ends;
        int maxCount = INT_MIN;
        for (int i = 0; i < nums.size(); i++) {
            int num = nums[i];
            if (starts.count(num) == 0) {
                starts[num] = i;
                counts[num] = 0;
            }
            counts[num]++;
            ends[num] = i;
            maxCount = max(maxCount, counts[num]);
        }
        int result = INT_MAX;
        for (auto &item : counts) {
            if (item.second == maxCount) {
                result = min(result, ends[item.first] - starts[item.first] + 1);
            }
        }
        return result;
    }
};