↓Skip to main content
  1. LeetCode/

LeetCode 697: Degree of an Array

·2 mins· ·
LeetCode Easy Array Hash-Table
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-30

  • Submissions: Wrong Answer ×3 → Runtime Error ×2 → Accepted ×5

Approach
#

A subarray with the full array’s degree must contain every occurrence of some most frequent value, so the shortest is last[x] - first[x] + 1. Several values may tie for the highest count, so take the minimum over all of them (Hash Table Template 1). One pass records counts and first and last indices.

My Attempts
#

This took 10 submissions:

  • The first three took one most frequent value with most_common()[0], never comparing others with the same count, and computed the length with negative indices, off by one: [2] returned -1 and 0.
  • After recording all positions per value there were more slips, d.values without parentheses and comparing a list against a length; comparing the lengths of the position lists passed.

Solution
#

class Solution:
    def findShortestSubArray(self, nums: List[int]) -> int:
        cnt, first, last = Counter(), {}, {}
        for i, x in enumerate(nums):
            cnt[x] += 1
            first.setdefault(x, i)
            last[x] = i
        degree = max(cnt.values())
        return min(last[x] - first[x] + 1 for x in cnt if cnt[x] == degree)

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Several values may share the highest count; compare all of them.
  • The length is last - first + 1.

Interview Explanation
#

The subarray must cover every occurrence of some most frequent value, so one pass records counts and first and last positions, and I take the shortest span among the most frequent values. Time O(n).

Related

LeetCode 645: Set Mismatch
·2 mins
LeetCode Easy Array Hash-Table Bit-Manipulation Sorting
LeetCode 645 note: use sums: missing = sum of 1..n - sum of the set, duplicate = array sum - sum of the set
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 575: Distribute Candies
·1 min
LeetCode Easy Array Hash-Table
LeetCode 575 note: at most n / 2 candies can be eaten and at most len(set) kinds exist; the answer is the smaller of the two
LeetCode 448: Find All Numbers Disappeared in an Array
·1 min
LeetCode Easy Array Hash-Table
LeetCode 448 note: use the array itself as the hash table, marking nums[x - 1] negative for each x; positions still positive are the missing numbers
LeetCode 268: Missing Number
·1 min
LeetCode Easy Array Hash-Table Math Binary-Search
LeetCode 268 note: the sum of 0..n minus the array’s sum is the missing number, which may be 0
LeetCode 217: Contains Duplicate
·1 min
LeetCode Easy Array Hash-Table Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate