Advertisement

Degree of an Array - LeetCode 697 Solution

Degree of an Array - Complete Solution Guide

Degree of an Array is LeetCode problem 697, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: nums = [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,

Detailed Explanation

The problem asks us to find the shortest contiguous subarray within a given array `nums` that has the same degree as the entire array. The degree of an array is defined as the maximum frequency of any one of its elements. In simpler terms, we need to find the most frequent number(s) in the array and then find the shortest subarray that contains all occurrences of those most frequent number(s). The input is a non-empty array of non-negative integers. The output is the length of the shortest subarray with the same degree as the input array. The length of the array is between 1 and 50,000, and the values in the array are between 0 and 49,999.

Solution Approach

The solution involves using hash maps (or arrays, in the C implementation) to store the frequency, first occurrence, and last occurrence of each element in the array. First, iterate through the array and record these values. Next, find the degree of the array, which is the maximum frequency among all elements. Finally, iterate through the elements again and check which element has a frequency equal to the degree. For each of these elements, calculate the length of the subarray from its first occurrence to its last occurrence. Maintain the minimum length found so far and return it.

Step-by-Step Algorithm

  1. Step 1: Initialize hash maps (or arrays) to store the frequency (count), first occurrence (first), and last occurrence (last) of each element.
  2. Step 2: Iterate through the input array `nums`. For each element, update its frequency in the `count` map, record its first occurrence in the `first` map if it's the first time encountering it, and update its last occurrence in the `last` map.
  3. Step 3: Calculate the degree of the array by finding the maximum frequency among all elements in the `count` map.
  4. Step 4: Initialize a variable `minLength` to infinity (or nums.length in Java).
  5. Step 5: Iterate through the `count` map. For each element, if its frequency is equal to the degree, calculate the length of the subarray from its first occurrence to its last occurrence (last[element] - first[element] + 1). Update `minLength` with the minimum length found so far.
  6. Step 6: Return the `minLength`.

Key Insights

  • Insight 1: We need to find the degree of the array, which is the maximum frequency of any element.
  • Insight 2: We need to find the first and last occurrences of each element to determine the length of the subarray containing all occurrences.
  • Insight 3: We need to iterate through elements with the same degree as the entire array and find the minimum length among them.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table.

Companies

Asked at: ByteDance, Expedia, GE Digital, IBM, J.P. Morgan, PayPal, Paycom, Rivian, Salesforce, SoFi, Turing, Walmart Labs, ZScaler.