697. Degree of an Array Easy
1/**
2 * [697] Degree of an Array
3 *
4 * 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.
5 * Your task is to find the smallest possible length of a (contiguous) subarray of nums, that has the same degree as nums.
6 *
7 * Example 1:
8 *
9 * Input: nums = [1,2,2,3,1]
10 * Output: 2
11 * Explanation:
12 * The input array has a degree of 2 because both elements 1 and 2 appear twice.
13 * Of the subarrays that have the same degree:
14 * [1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
15 * The shortest length is 2. So return 2.
16 *
17 * Example 2:
18 *
19 * Input: nums = [1,2,2,3,1,4,2]
20 * Output: 6
21 * Explanation:
22 * The degree is 3 because the element 2 is repeated 3 times.
23 * So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.
24 *
25 *
26 * Constraints:
27 *
28 * nums.length will be between 1 and 50,000.
29 * nums[i] will be an integer between 0 and 49,999.
30 *
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/degree-of-an-array/
35// discuss: https://leetcode.com/problems/degree-of-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40 pub fn find_shortest_sub_array(nums: Vec<i32>) -> i32 {
41 0
42 }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn test_697() {
53 }
54}
55
Back
© 2025 bowen.ge All Rights Reserved.