Class Solution

java.lang.Object
g0601_0700.s0697_degree_of_an_array.Solution

public class Solution extends Object
697 - Degree of an Array.<p>Easy</p> <p>Given a non-empty array of non-negative integers <code>nums</code>, the <strong>degree</strong> of this array is defined as the maximum frequency of any one of its elements.</p> <p>Your task is to find the smallest possible length of a (contiguous) subarray of <code>nums</code>, that has the same degree as <code>nums</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,2,3,1]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <pre><code> 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. </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,2,3,1,4,2]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <pre><code> The degree is 3 because the element 2 is repeated 3 times. So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>nums.length</code> will be between 1 and 50,000.</li> <li><code>nums[i]</code> will be an integer between 0 and 49,999.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findShortestSubArray

      public int findShortestSubArray(int[] nums)